diff --git a/.gitignore b/.gitignore index 1efcff8..3a36d0f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__/ venv/ *.sh +logo/ diff --git a/app/config.py b/app/config.py index 2e10253..f72e972 100644 --- a/app/config.py +++ b/app/config.py @@ -1,3 +1,4 @@ +from pathlib import Path import traceback import json import sys @@ -24,8 +25,6 @@ except (KeyError, ValueError): try: index_settings_str = os.environ["INDEX_SETTINGS"].strip() index_settings = json.loads(index_settings_str) - otg_settings = index_settings['otg'] - enable_otg = otg_settings['enable'] except: traceback.print_exc() print("\n\nPlease set the INDEX_SETTINGS environment variable correctly") @@ -40,5 +39,7 @@ except (KeyError, ValueError): host = os.environ.get("HOST", "0.0.0.0") debug = bool(os.environ.get("DEBUG")) -chat_ids = [] -alias_ids = [] +block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS")) +results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20")) +logo_folder = Path('logo/') +logo_folder.mkdir(exist_ok=True) diff --git a/app/generate_session_string.py b/app/generate_session_string.py index 9d1f258..124922b 100644 --- a/app/generate_session_string.py +++ b/app/generate_session_string.py @@ -1,8 +1,10 @@ +import os + from telethon.sync import TelegramClient from telethon.sessions import StringSession -api_id = int(input("Enter your API_ID: ")) -api_hash = input("Enter your API_HASH: ") +api_id = int(os.environ.get('API_ID') or input("Enter your API_ID: ")) +api_hash = os.environ.get('API_HASH') or input("Enter your API_HASH: ") with TelegramClient(StringSession(), api_id, api_hash) as client: print(client.session.save()) diff --git a/app/routes.py b/app/routes.py index 855f495..5973b5b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,75 +3,70 @@ import string import logging from aiohttp import web +from telethon.tl.types import Channel, Chat, User -from .config import index_settings, alias_ids, chat_ids +from .config import index_settings 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): h = handler client = h.client - p = r"/{chat:[^/]+}" - routes = [ - web.get('/', h.home), - 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) - ] 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'] + routes = [ + web.get('/', h.home) + ] if index_all: + #print(await client.get_dialogs()) async for chat in client.iter_dialogs(): alias_id = None if chat.id in exclude_chats: continue - if chat.is_user: - if index_private: - alias_id = generate_alias_id(chat) - elif chat.is_channel: - if index_channel: - alias_id = generate_alias_id(chat) - else: - if index_group: - alias_id = generate_alias_id(chat) + entity = chat.entity - if not alias_id: + if isinstance(entity, User) and not index_private: + print(f'{chat.title}, private: {index_private}') continue - log.debug(f"Index added for {chat.id} :: {chat.title} at /{alias_id}") + elif isinstance(entity, Channel) and not index_channel: + print(f'{chat.title}, channel: {index_channel}') + continue + elif isinstance(entity, Chat) and not index_group: + print(f'{chat.title}, group: {index_group}') + continue + + alias_id = h.generate_alias_id(chat) + p = "/{chat:" + alias_id + "}" + routes.extend([ + 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), + ]) + log.debug(f"Index added for {chat.id} at /{alias_id}") 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}") - + alias_id = h.generate_alias_id(chat) + p = "/{chat:" + alias_id + "}" + routes.extend([ + 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), + ]) + log.debug(f"Index added for {chat.id} at /{alias_id}") + routes.append(web.view(r'/{wildcard:.*}', h.wildcard)) app.add_routes(routes) diff --git a/app/telegram.py b/app/telegram.py index db01cf5..63be4bd 100644 --- a/app/telegram.py +++ b/app/telegram.py @@ -10,7 +10,7 @@ class Client(TelegramClient): def __init__(self, session_string, *args, **kwargs): super().__init__(StringSession(session_string), *args, **kwargs) self.log = logging.getLogger(__name__) - + async def download(self, file, file_size, offset, limit): part_size_kb = utils.get_appropriated_part_size(file_size) @@ -25,7 +25,7 @@ class Client(TelegramClient): async for chunk in self.iter_download(file, offset=first_part * part_size, request_size=part_size): if part == first_part: yield chunk[first_part_cut:] - elif part == last_part: + elif part == last_part-1: yield chunk[:last_part_cut] else: yield chunk diff --git a/app/templates/info.html b/app/templates/info.html index 8c88817..b2ffb67 100644 --- a/app/templates/info.html +++ b/app/templates/info.html @@ -1,26 +1,29 @@ {% include 'header.html' %} - +
{{ caption_html|safe }}
@@ -87,15 +90,15 @@{{ reason }}
{% endif %} - +