-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
403 lines (317 loc) · 17.2 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import aiohttp
from flask import Flask, render_template, send_from_directory, request, make_response, redirect
from pathlib import Path
from babel import dates
import datetime
import asyncio
import tool
import os
import sitemapper as sitemapper
from user_manager import UserHandler
app = Flask(__name__, template_folder='website')
SHORT_WORDS = [
"b", "list", "h1", "h2", "h3", "h4", "h5", "h6", "*", "u", "url"
]
js_datetime = "%Y-%m-%d %H:%M:%S"
@app.route('/')
@app.route('/index')
@app.route('/index.html')
@app.route('/about')
@app.route('/about.html')
@app.route('/apis')
@app.route('/apis.html')
@app.route('/legal/cookies')
@app.route('/legal/cookies.html')
@app.route('/legal/license')
@app.route('/legal/license.html')
@app.route('/legal/site-rules')
@app.route('/legal/site-rules.html')
@app.route('/legal/copyright')
@app.route('/legal/copyright.html')
@app.route('/legal/privacy-policy')
@app.route('/legal/privacy-policy.html')
async def unified_route():
url = request.path
if url == '/': url = '/index'
if not url.endswith('.html'): url += '.html'
async with UserHandler() as handler:
page_html = handler.render(url[1:], catalog=(url=='/index.html'))
return handler.finish(page_html)
@app.route('/mod/<int:mod_id>')
@app.route('/mod/<int:mod_id>.html')
@app.route('/mod/<int:mod_id>/edit')
@app.route('/mod/<int:mod_id>/edit.html')
async def mod_view_and_edit(mod_id):
launge = "ru"
## TODO ЗАТЫЧКА!! ПОТОМ УБРАТЬ!!! ##
if mod_id > 60000:
async with aiohttp.ClientSession() as session:
async with session.get(f'https://openworkshop.su/api/manager/list/mods/?primary_sources=["steam"]&allowed_sources_ids=[{mod_id}]') as response:
tor = await response.json()
if len(tor['results']) > 0:
return redirect(
"/mod/" + str(tor['results'][0]['id']),
code=308
)
## / ЗАТЫЧКА!! ПОТОМ УБРАТЬ!!! ##
async with UserHandler() as handler:
# Определяем запросы
api_urls = {
"info": f"/info/mod/{mod_id}?dependencies=true&description=true&short_description=true&dates=true&general=true&game=true&authors=true",
"resources": f"/list/resources/mods/[{mod_id}]?page_size=30",
"tags": f"/list/tags/mods/[{mod_id}]"
}
# Запрашиваем
info_result, resources_result, tags_result = await asyncio.gather(
handler.fetch(api_urls["info"]),
handler.fetch(api_urls["resources"]),
handler.fetch(api_urls["tags"])
)
# Первичная распаковка данных
info_code, info = info_result
resources_code, resources = resources_result
tags_code, tags = tags_result
# Проверка результатов
if type(info) is str:
# Сервер ответил на информацию о моде ошибкой (возврашаем ошибку пользователю)
return handler.finish(handler.render("error.html", error=info, error_title='Ошибка')), info_code
else:
# Вторичная (косметическая на самом деле) распаковка
tags = tags[str(mod_id)]
user_is_author = False
user_is_owner = False
authors = []
if len(info['authors']) > 0:
authors_info = await asyncio.gather(
*[handler.fetch(f'/profile/info/{author}') for author in info['authors']])
for status_code, author in authors_info:
author_to_add = author['general']
author_to_add['owner'] = info['authors'][str(author_to_add['id'])]['owner']
if handler.profile:
if author_to_add['id'] == handler.profile['id']:
user_is_author = True
user_is_owner = author_to_add['owner']
authors.append(author_to_add)
right_edit_mod = handler.access_to_mod(my_mod=user_is_author, owner_mod=user_is_owner)
edit_page = '/edit' in request.path
if False and edit_page and not right_edit_mod['edit']:
if not handler.profile:
page = handler.render("error.html", error='Войдите или создайте аккаунт', error_title='Не авторизован')
elif right_edit_mod['in_mute']:
page = handler.render("error.html", error='Вы во временном муте', error_title='В муте')
else:
page = handler.render(
"error.html",
error='Вы не имеете прав на редактирование чужих модов' if right_edit_mod['is_my_mod'] == 2 else 'Вы не имеете прав на редактирование своих модов',
error_title='Отказано в доступе'
)
return handler.finish(page), 403
info['result']['size'] = await tool.size_format(info['result']['size']) # Преобразовываем кол-во байт в читаемые человеком форматы
for image in resources["results"]: # Ищем логотип мода
if image and image["type"] == "logo":
info["result"]["logo"] = image["url"] # Фиксируем, что нашли его
if len(resources["results"]) > 1: # Если по мимо него есть скриншоты - выбиваем из окна скриншотов (по принципу либо это(лого), либо того(скриншоты))
resources["results"].remove(image)
break
else:
info["result"]["logo"] = ''
info["no_many_screenshots"] = len(resources["results"]) <= 1 # bool переменная для рендера шаблона, указка показывать ли меню навигации
for key in ["date_creation", "date_update_file"]: # Форматируем (обрабатываем) даты
input_date = datetime.datetime.fromisoformat(info['result'][key])
info['result'][f'{key}_js'] = input_date.strftime(js_datetime)
info['result'][key] = dates.format_date(input_date, locale=launge)
info['result']['id'] = mod_id # Фиксируем для рендера шаблона id мода
dependencies = {}
if info['dependencies_count'] > 0: # Чекаем, есть ли зависимости
# Формируем запрос на получение зависимостей
dependencies_urls = [
f'/list/mods/?page_size=50&allowed_ids={info["dependencies"]}',
f'/list/resources/mods/{info["dependencies"]}?page_size=30'
]
# Запрашиваем
dependencies_info, dependencies_resources = await asyncio.gather(*[handler.fetch(url) for url in dependencies_urls])
# Распаковка данных
dependencies_info_code, dependencies_info = dependencies_info
dependencies_resources_code, dependencies_resources = dependencies_resources
# Добавляем зависимости
for dependency in dependencies_info['results']:
dependencies[dependency['id']] = {
'id': dependency['id'],
'img': '',
'name': dependency['name']
}
# Добавляем логотипы зависимостям
for resource in dependencies_resources['results']:
dependencies[resource['owner_id']]['img'] = resource['url']
page_html = handler.render(
"mod-edit.html" if edit_page else "mod.html",
info=info,
tags=tags,
resources=resources,
dependencies=dependencies,
right_edit=right_edit_mod,
authors=authors
)
return handler.finish(page_html)
@app.route('/mod/add')
@app.route('/mod/add.html')
async def add_mod():
async with UserHandler() as handler:
access = handler.access_to_mod()
if not access['add']:
if not handler.profile:
page = handler.render("error.html", error='Войдите или создайте аккаунт', error_title='Не авторизован')
elif access['in_mute']:
page = handler.render("error.html", error='Вы во временном муте', error_title='В муте')
else:
page = handler.render("error.html", error='Вы не можете публиковать моды', error_title='Отказано в доступе')
return handler.finish(page), 403
page = handler.render("mod-add.html")
return handler.finish(page)
@app.route('/user/<int:user_id>')
@app.route('/user/<int:user_id>.html')
async def user(user_id):
launge = "ru"
async with UserHandler() as handler:
profile_info, user_mods = await asyncio.gather(
handler.fetch(f"/profile/info/{user_id}"),
handler.fetch(f"/list/mods/?user={user_id}&page_size=4")
)
profile_info_code, profile_info = profile_info
user_mods_code, user_mods = user_mods
if profile_info_code != 200:
return handler.finish(handler.render("error.html", error=profile_info, error_title=f'Ошибка ({profile_info_code})')), profile_info_code
profile_info['delete_user'] = profile_info['general']['username'] is None
if profile_info['delete_user']:
return handler.finish(handler.render("error.html", error="Профиль удален", error_title="Этот профиль удален!")), 404
if profile_info["general"]["mute"]:
input_date = datetime.datetime.fromisoformat(profile_info["general"]["mute"])
profile_info["general"]["mute_js"] = profile_info["general"]["mute"]
profile_info["general"]["mute"] = dates.format_datetime(input_date, format="short", locale=launge)
input_date = datetime.datetime.fromisoformat(profile_info['general']['registration_date'])
profile_info['general']['registration_date_js'] = input_date.strftime(js_datetime)
profile_info['general']['registration_date'] = dates.format_date(input_date, locale=launge)
if profile_info['general']['about'] is None or len(profile_info['general']['about']) <= 0:
profile_info['general']['about_enable'] = False
profile_info['general']['about'] = f"Социальная сеть для модов! Зарегистрируйся и добавь {profile_info['general']['username']} в друзья! 🤪"
else:
profile_info['general']['about_enable'] = True
if profile_info['general']['avatar_url'] is None or len(profile_info['general']['avatar_url']) <= 0:
profile_info['general']['avatar_url'] = "/assets/images/no-avatar.jpg"
elif profile_info['general']['avatar_url'].startswith("local"):
profile_info['general']['avatar_url'] = f"/api/manager/profile/avatar/{user_id}"
if len(user_mods['results']) > 0:
resources_mods_code, resources_mods = await handler.fetch(f'/list/resources/mods/{[i["id"] for i in user_mods["results"]]}?page_size=10&page=0&types_resources=["logo"]')
mods_data = [
{
'id': int(i['id']),
'name': i['name'],
'img': ''
}
for i in user_mods['results']
]
print(resources_mods)
for resource in resources_mods['results']:
mods_data[resource['owner_id']]['img'] = resource['url']
user_mods = {
'not_show_all': len(user_mods['results']) > 3,
'mods_data': mods_data
}
else:
user_mods = False
profile_info['general']['editable'] = handler.access_to_mod()
page = handler.render("user.html", user_data=profile_info, user_mods=user_mods)
return handler.finish(page)
@app.route('/user/<int:user_id>/settings')
@app.route('/user/<int:user_id>/settings.html')
async def user_settings(user_id):
launge = "ru"
async with UserHandler() as handler:
editable = handler.access_to_mod()
editable['my'] = handler.profile and user_id==handler.profile.get('id', -1)
if not editable['any']:
return handler.finish(handler.render("error.html", error='Вы не имеете прав редактировать этот профиль!', error_title='Отказано в доступе!')), 403
info_profile_code, info_profile = await handler.fetch(
f"/profile/info/{user_id}?general=true"+("&rights=true&private=true" if editable["admin"] or editable['my'] else "")
)
if info_profile_code != 200:
return handler.finish(handler.render(
"error.html",
error=info_profile,
error_title=f'Ошибка ({info_profile_code})')
), info_profile_code
if info_profile["general"]["mute"]:
input_date = datetime.datetime.fromisoformat(info_profile["general"]["mute"])
info_profile["general"]["mute_js"] = info_profile["general"]["mute"]
info_profile["general"]["mute"] = dates.format_datetime(input_date, format="short", locale=launge)
if info_profile['general']['about'] is None or len(info_profile['general']['about']) <= 0:
info_profile['general']['about_enable'] = False
info_profile['general']['about'] = f"Социальная сеть для модов! Зарегистрируйся и добавь {info_profile['general']['username']} в друзья! 🤪"
else:
info_profile['general']['about_enable'] = True
input_date = datetime.datetime.fromisoformat(info_profile['general']['registration_date'])
info_profile['general']['registration_date_js'] = input_date.strftime(js_datetime)
info_profile['general']['registration_date'] = dates.format_date(input_date, locale=launge)
if info_profile['general']['avatar_url'] is None or len(info_profile['general']['avatar_url']) <= 0:
info_profile['general']['avatar_url'] = "/assets/images/no-avatar.jpg"
elif info_profile['general']['avatar_url'].startswith("local"):
info_profile['general']['avatar_url'] = f"/api/manager/profile/avatar/{user_id}"
info_profile['delete_user'] = info_profile['general']['username'] is None
return handler.finish(handler.render("user-settings.html", user_data=info_profile, user_access=editable))
@app.route('/user/<int:user_id>/mods')
@app.route('/user/<int:user_id>/mods.html')
async def user_mods(user_id):
return await tool.error_page(
error_title='Зайдите попозже...',
error_body=f'Эта страница вскоре будет доступна! ({user_id})'
)
@app.route('/api/login-popup')
async def login_popup():
return render_template("login-popup.html", link=request.args.get('f'), russia=not bool(request.cookies.get('fromRussia')))
@app.route('/<path:filename>')
async def serve_static(filename):
if filename.startswith("/html-partials/") or filename.startswith("html-partials/"):
return await page_not_found()
return send_from_directory("website", filename)
@app.errorhandler(404)
async def page_not_found(_error = -1):
return await tool.error_page(
error_title='Not Found (404)',
error_body='404 страница не найдена',
error_code=404
)
@app.errorhandler(500)
async def internal_server_error(_error = -1):
return await tool.error_page(
error_title='Internal Server Error (500)',
error_body='На сервере произошла внутренняя ошибка, и он не смог выполнить ваш запрос. Либо сервер перегружен, либо в приложении ошибка.',
error_code=500
)
@app.route('/sitemap.xml')
async def sitemap():
file_path = "website/sitemaps/"
if "www." in request.url_root:
file_path += "www."
file_path += "sitemap.xml"
now = datetime.datetime.now()
if Path(file_path).exists():
created_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
diff = now - created_time
if diff > datetime.timedelta(hours=5):
print("sitemap.xml regenerate")
page = await sitemapper.generate(file_path)
else:
print("sitemap.xml generate")
page = await sitemapper.generate(file_path)
if "page" not in locals():
print("sitemap.xml relevant")
with open(file_path, "r") as file:
page = file.read()
page_ret = make_response(page)
page_ret.headers["Content-Type"] = "application/rss+xml"
page_ret.mimetype = "application/xml"
return page_ret
if __name__ == '__main__':
#app.run()
from waitress import serve
serve(app, host="0.0.0.0", port=6060, threads=100)