75 lines
2.8 KiB
Python
Raw Normal View History

2020-08-17 13:14:02 +05:30
import logging
2020-08-13 03:58:55 +00:00
2020-08-10 07:27:52 +00:00
from aiohttp import web
2020-12-12 19:46:44 +05:30
from telethon.tl.types import Channel, Chat, User
2020-08-10 07:27:52 +00:00
2020-12-12 19:46:44 +05:30
from .config import index_settings
2020-08-12 10:39:29 +00:00
2020-08-17 13:14:02 +05:30
log = logging.getLogger(__name__)
async def setup_routes(app, handler):
2020-08-10 07:27:52 +00:00
h = handler
2020-08-17 13:14:02 +05:30
client = h.client
2020-12-17 08:46:10 +05:30
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"]
2021-05-22 19:30:08 +05:30
routes = [
web.get("/", h.home, name="home"),
web.get("/login", h.login_get, name="login_page"),
web.post("/login", h.login_post, name="login_handle"),
web.get("/logout", h.logout_get, name="logout"),
]
2021-06-06 17:01:00 +05:30
def get_common_routes(p):
return [
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),
2021-06-12 21:00:52 +05:30
web.get(p + r"/{id:\d+}/v.mp4", h.download_get),
web.head(p + r"/{id:\d+}/v.mp4", h.download_head),
2021-06-06 17:01:00 +05:30
]
2020-08-17 13:14:02 +05:30
if index_all:
2020-12-17 08:46:10 +05:30
# print(await client.get_dialogs())
# dialogs = await client.get_dialogs()
# for chat in dialogs:
2020-08-17 13:14:02 +05:30
async for chat in client.iter_dialogs():
alias_id = None
if chat.id in exclude_chats:
continue
2020-09-20 22:08:52 +05:30
2020-12-12 19:46:44 +05:30
entity = chat.entity
2020-09-20 22:08:52 +05:30
2020-12-12 19:46:44 +05:30
if isinstance(entity, User) and not index_private:
2020-12-17 08:46:10 +05:30
log.debug(f"{chat.title}, private: {index_private}")
2020-12-12 19:46:44 +05:30
continue
elif isinstance(entity, Channel) and not index_channel:
2020-12-17 08:46:10 +05:30
log.debug(f"{chat.title}, channel: {index_channel}")
2020-12-12 19:46:44 +05:30
continue
elif isinstance(entity, Chat) and not index_group:
2020-12-17 08:46:10 +05:30
log.debug(f"{chat.title}, group: {index_group}")
2020-08-13 03:58:55 +00:00
continue
2020-12-12 19:46:44 +05:30
alias_id = h.generate_alias_id(chat)
p = "/{chat:" + alias_id + "}"
2020-12-17 08:46:10 +05:30
routes.extend(
2021-06-06 17:01:00 +05:30
get_common_routes(p)
2020-12-17 08:46:10 +05:30
)
2020-12-12 19:46:44 +05:30
log.debug(f"Index added for {chat.id} at /{alias_id}")
2020-09-20 22:08:52 +05:30
2020-08-17 13:14:02 +05:30
else:
for chat_id in include_chats:
chat = await client.get_entity(chat_id)
2020-12-12 19:46:44 +05:30
alias_id = h.generate_alias_id(chat)
p = "/{chat:" + alias_id + "}"
2020-12-17 08:46:10 +05:30
routes.extend(
2021-06-06 17:01:00 +05:30
get_common_routes(p) # returns list() of common routes
2020-12-17 08:46:10 +05:30
)
2020-12-12 19:46:44 +05:30
log.debug(f"Index added for {chat.id} at /{alias_id}")
2020-12-17 08:46:10 +05:30
routes.append(web.view(r"/{wildcard:.*}", h.wildcard))
2020-08-12 10:39:29 +00:00
app.add_routes(routes)