TelegramIndex-Fork/app/routes.py

78 lines
2.3 KiB
Python
Raw Normal View History

2020-08-13 05:58:55 +02:00
import random
import string
2020-08-17 09:44:02 +02:00
import logging
2020-08-13 05:58:55 +02:00
2020-08-10 09:27:52 +02:00
from aiohttp import web
2020-08-17 09:44:02 +02:00
from .config import index_settings, alias_ids, chat_ids
2020-08-12 12:39:29 +02:00
2020-08-10 09:27:52 +02:00
2020-08-17 09:44:02 +02:00
log = logging.getLogger(__name__)
def generate_alias_id(chat):
chat_id = chat.id
title = chat.title
while True:
alias_id = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(len(str(chat_id)))])
if alias_id in alias_ids:
continue
alias_ids.append(alias_id)
chat_ids.append({
'chat_id': chat_id,
'alias_id': alias_id,
'title': title
})
return alias_id
async def setup_routes(app, handler):
2020-08-10 09:27:52 +02:00
h = handler
2020-08-17 09:44:02 +02:00
client = h.client
2020-09-20 18:38:52 +02:00
p = r"/{chat:[^/]+}"
2020-08-12 12:39:29 +02:00
routes = [
web.get('/', h.home),
2020-09-20 18:38:52 +02:00
web.post('/otg', h.dynamic_view),
web.get('/otg', h.otg_view),
web.get(p, h.index),
web.get(p + r"/logo", h.logo),
web.get(p + r"/{id:\d+}/view", h.info),
web.get(p + r"/{id:\d+}/download", h.download_get),
web.head(p + r"/{id:\d+}/download", h.download_head),
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
web.view(r'/{wildcard:.*}', h.wildcard)
2020-08-12 12:39:29 +02:00
]
2020-08-17 09:44:02 +02:00
index_all = index_settings['index_all']
index_private = index_settings['index_private']
index_group = index_settings['index_group']
index_channel = index_settings['index_channel']
exclude_chats = index_settings['exclude_chats']
include_chats = index_settings['include_chats']
if index_all:
async for chat in client.iter_dialogs():
alias_id = None
if chat.id in exclude_chats:
continue
2020-09-20 18:38:52 +02:00
2020-08-17 09:44:02 +02:00
if chat.is_user:
if index_private:
alias_id = generate_alias_id(chat)
2020-09-20 18:38:52 +02:00
elif chat.is_channel:
if index_channel:
2020-08-17 09:44:02 +02:00
alias_id = generate_alias_id(chat)
else:
2020-09-20 18:38:52 +02:00
if index_group:
2020-08-17 09:44:02 +02:00
alias_id = generate_alias_id(chat)
2020-09-20 18:38:52 +02:00
2020-08-17 09:44:02 +02:00
if not alias_id:
2020-08-13 05:58:55 +02:00
continue
2020-08-17 09:44:02 +02:00
log.debug(f"Index added for {chat.id} :: {chat.title} at /{alias_id}")
2020-09-20 18:38:52 +02:00
2020-08-17 09:44:02 +02:00
else:
for chat_id in include_chats:
chat = await client.get_entity(chat_id)
alias_id = generate_alias_id(chat)
log.debug(f"Index added for {chat.id} :: {chat.title} at /{alias_id}")
2020-09-20 18:38:52 +02:00
2020-08-12 12:39:29 +02:00
app.add_routes(routes)