-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·277 lines (207 loc) · 7.52 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
RANDOM IMAGE SITE.
:copyright: (c) 2017 Jakeoid.
:license: MIT, see LICENSE.md for details.
"""
# HOUSEKEEPING
import json
import asyncio
# RANDOM
from random import choice
from os import walk, path
# KYOUKAI
from kyoukai import Kyoukai, util
from werkzeug import Response
# ############################
app = Kyoukai(__name__)
# Storing our files.
global files
# Make our files an empty list.
files = []
# Run through all the files in the img folder.
for (dirpath, dirnames, filenames) in walk('img/'):
# Get all the filenames
files.extend(filenames)
# Beep
break
# ############################
# CLASS FOR SETTINGS
class JSONFile:
"""Instance of a configuration JSON File."""
def __init__(self, filename: str, interval: int = 900, loop=None):
"""Representative of a JSONFile Object.
You call this when creating a configuration file.
* filename - The name of the file in which JSON runs from.
* interval - The interval in which we call back and reload the file (seconds).
* loop - The asynchronous loop that the file will be called through.
"""
self.filename = filename
self.interval = interval
self._reload()
loop = loop or asyncio.get_event_loop()
loop.create_task(self._task())
def _reload(self):
with open(self.filename) as f:
self.cache = json.load(f)
async def _task(self):
await asyncio.sleep(900)
self._reload()
def __getitem__(self, i):
"""Return the cached item."""
return self.cache[i]
# ############################
SETTINGS = JSONFile("settings.json", loop=app.loop)
# SERVER
IP = SETTINGS['server']['ip'] or "0.0.0.0"
PORT = SETTINGS['server']['port'] or 8080
# ############################
# Site Index.
@app.route("/")
async def index(ctx):
"""Display the homepage of the website."""
try:
file_directory = 'templates/' + settings['pages']['index']
except:
file_directory = 'templates/index.html'
with open(file_directory) as file:
content = file.read()
content = content.replace(
'{{AMOUNT}}', str(len(files)))
content = content.replace(
'{{NAME}}', settings['meta']['name'])
content = content.replace(
'{{DESCRIPTION}}', settings['meta']['description'])
content = content.replace(
'{{TYPE}}', settings['meta']['type'])
content = content.replace(
'{{TYPE-PLURAL}}', settings['meta']['plural'])
content = content.replace(
'{{SITEURL}}', settings['meta']['siteurl'])
content = content.replace(
'{{ENDPOINT-MAIN}}', settings['endpoints']['main']['name'])
content = content.replace(
'{{ENDPOINT-JSON}}', settings['endpoints']['json']['name'])
content = content.replace(
'{{ENDPOINT-RANDOM}}', settings['endpoints']['random']['name'])
return util.as_html(content)
# Assets Route.
@app.route('/<folder>/<filename>')
async def assets(ctx, folder, filename):
"""Serve the images of the /img/ and other folders."""
location = folder + '/' + filename
with open(location, mode='rb') as file:
stream = file.read()
header = {
'Content-Type': mimetypes.guess_type(location)[0]
}
return util.Response(stream, status=200, headers=header)
# Text API Endpoint.
@app.route(settings['endpoints']['main']['name'])
async def tweet(ctx):
"""Host the API in which you can get random birbs from."""
if not settings['endpoints']['main']['enabled']:
return
url = randint(0, len(files) - 1)
header = {
'Content-Type': 'text/plain'
}
return util.as_html("%s" % files[url])
# JSON API Endpoint.
@app.route(settings['endpoints']['json']['name'])
async def tweetjson(ctx):
"""Host the API in which you can get random birbs from."""
if not settings['endpoints']['json']['enabled']:
return
url = randint(0, len(files) - 1)
header = {
'Content-Type': 'application/javascript'
}
return util.Response(json.dumps({'file': files[url]}), status=200, headers=header)
# Text API Endpoint.
@app.route(settings['endpoints']['random']['name'])
async def tweetrandom(ctx):
"""Host the API in which you can get random birbs from."""
if not settings['endpoints']['random']['enabled']:
return
url = randint(0, len(files) - 1)
location = 'img/' + files[url]
with open(location, mode='rb') as file:
stream = file.read()
header = {
'Content-Type': mimetypes.guess_type(location)[0]
}
return util.Response(stream, status=200, headers=header)
# Image Hoster.
@app.route('/img/<filename>')
async def image(ctx, filename):
"""Serve the images of the /img/ and other folders."""
location = 'img/' + filename
filetype = filename.split('.')[1]
with open(location, mode='rb') as file:
stream = file.read()
header = {
'Content-Type': 'image/' + filetype
}
return util.Response(stream, status=200, headers=header)
# ############################
# Handle 404.
@app.root.errorhandler(404)
async def handle_404(ctx, exc):
"""Serve the 404 (Not Found) Page."""
try:
file_directory = 'templates/' + settings['pages']['notfound']
except:
file_directory = 'templates/index.html'
with open(file_directory) as file:
content = file.read()
content = content.replace(
'{{AMOUNT}}', str(len(files)))
content = content.replace(
'{{NAME}}', settings['meta']['name'])
content = content.replace(
'{{DESCRIPTION}}', settings['meta']['description'])
content = content.replace(
'{{TYPE}}', settings['meta']['type'])
content = content.replace(
'{{TYPE-PLURAL}}', settings['meta']['plural'])
content = content.replace(
'{{SITEURL}}', settings['meta']['siteurl'])
content = content.replace(
'{{ENDPOINT-MAIN}}', settings['endpoints']['main']['name'])
content = content.replace(
'{{ENDPOINT-JSON}}', settings['endpoints']['json']['name'])
content = content.replace(
'{{ENDPOINT-RANDOM}}', settings['endpoints']['random']['name'])
return util.as_html(content)
# Handle 404.
@app.root.errorhandler(500)
async def handle_500(ctx, exc):
"""Serve the 500 (Server Error) Page."""
try:
file_directory = 'templates/' + settings['pages']['servererror']
except:
file_directory = 'templates/index.html'
with open(file_directory) as file:
content = file.read()
content = content.replace(
'{{AMOUNT}}', str(len(files)))
content = content.replace(
'{{NAME}}', settings['meta']['name'])
content = content.replace(
'{{DESCRIPTION}}', settings['meta']['description'])
content = content.replace(
'{{TYPE}}', settings['meta']['type'])
content = content.replace(
'{{TYPE-PLURAL}}', settings['meta']['plural'])
content = content.replace(
'{{SITEURL}}', settings['meta']['siteurl'])
content = content.replace(
'{{ENDPOINT-MAIN}}', settings['endpoints']['main']['name'])
content = content.replace(
'{{ENDPOINT-JSON}}', settings['endpoints']['json']['name'])
content = content.replace(
'{{ENDPOINT-RANDOM}}', settings['endpoints']['random']['name'])
return util.as_html(content)
# ############################
# Run our App.
app.run(IP, PORT)