diff --git a/app/config.py b/app/config.py index bf8fc53..250049a 100644 --- a/app/config.py +++ b/app/config.py @@ -5,9 +5,11 @@ import sys import os +from dotenv import load_dotenv +load_dotenv("config.env") try: - port = int(os.environ.get("PORT", "8080")) + port = int(os.environ.get("PORT", 8080)) except Exception as e: print(e) port = -1 @@ -27,8 +29,7 @@ sys.exit(1) try: - index_settings_str = os.environ["INDEX_SETTINGS"].strip() - index_settings = json.loads(index_settings_str) + index_settings = json.load(open("index_settings.json")) except Exception: traceback.print_exc() print("\n\nPlease set the INDEX_SETTINGS environment variable correctly") diff --git a/app/views/faviconicon_view.py b/app/views/faviconicon_view.py index 2663293..733f5a6 100644 --- a/app/views/faviconicon_view.py +++ b/app/views/faviconicon_view.py @@ -9,13 +9,13 @@ class FaviconIconView: async def faviconicon(self, req): favicon_path = logo_folder.joinpath("favicon.ico") - text = "T" if not favicon_path.exists(): W, H = (360, 360) color = tuple((random.randint(0, 255) for _ in range(3))) im = Image.new("RGB", (W, H), color) draw = ImageDraw.Draw(im) font = ImageFont.truetype("arial.ttf", 50) + text = "T" w, h = draw.textsize(text, font=font) draw.text(((W - w) / 2, (H - h) / 2), text, fill="white", font=font) im.save(favicon_path) diff --git a/app/views/info_view.py b/app/views/info_view.py index f5850dc..5ba83e3 100644 --- a/app/views/info_view.py +++ b/app/views/info_view.py @@ -38,16 +38,17 @@ async def info(self, req): "authenticated": req.app["is_authenticated"], } reply_btns = [] - if message.reply_markup: - if isinstance(message.reply_markup, types.ReplyInlineMarkup): - reply_btns = [ - [ - {"url": button.url, "text": button.text} - for button in button_row.buttons - if isinstance(button, types.KeyboardButtonUrl) - ] - for button_row in message.reply_markup.rows + if message.reply_markup and isinstance( + message.reply_markup, types.ReplyInlineMarkup + ): + reply_btns = [ + [ + {"url": button.url, "text": button.text} + for button in button_row.buttons + if isinstance(button, types.KeyboardButtonUrl) ] + for button_row in message.reply_markup.rows + ] if message.file and not isinstance(message.media, types.MessageMediaWebPage): file_name = get_file_name(message) @@ -60,11 +61,7 @@ async def info(self, req): elif "image/" in message.file.mime_type: media["image"] = True - if message.text: - caption = message.raw_text - else: - caption = "" - + caption = message.raw_text if message.text else "" caption_html = Markup.escape(caption).__str__().replace("\n", "
") return_val.update( { diff --git a/app/views/middlewhere.py b/app/views/middlewhere.py index 937040c..6a62864 100644 --- a/app/views/middlewhere.py +++ b/app/views/middlewhere.py @@ -59,30 +59,29 @@ async def _do_cookies_auth_check(request): def middleware_factory(): @middleware async def factory(request, handler): - if request.app["is_authenticated"] and str(request.rel_url.path) not in [ + if not request.app["is_authenticated"] or str(request.rel_url.path) in [ "/login", "/logout", "/favicon.ico", ]: - url = request.app.router["login_page"].url_for() - if str(request.rel_url) != "/": - url = url.with_query(redirect_to=str(request.rel_url)) + return await handler(request) + url = request.app.router["login_page"].url_for() + if str(request.rel_url) != "/": + url = url.with_query(redirect_to=str(request.rel_url)) - basic_auth_check_resp = _do_basic_auth_check(request) + basic_auth_check_resp = _do_basic_auth_check(request) - if basic_auth_check_resp is True: - return await handler(request) + if basic_auth_check_resp is True: + return await handler(request) - cookies_auth_check_resp = await _do_cookies_auth_check(request) + cookies_auth_check_resp = await _do_cookies_auth_check(request) - if cookies_auth_check_resp is not None: - return await handler(request) + if cookies_auth_check_resp is not None: + return await handler(request) - if isinstance(basic_auth_check_resp, Response): - return basic_auth_check_resp + if isinstance(basic_auth_check_resp, Response): + return basic_auth_check_resp - return HTTPFound(url) - - return await handler(request) + return HTTPFound(url) return factory diff --git a/app/views/thumbnail_view.py b/app/views/thumbnail_view.py index 7bbaff7..d676e8b 100644 --- a/app/views/thumbnail_view.py +++ b/app/views/thumbnail_view.py @@ -39,7 +39,7 @@ async def thumbnail_get(self, req): location = types.InputPhotoFileLocation if not thumbnails: - color = tuple([random.randint(0, 255) for i in range(3)]) + color = tuple(random.randint(0, 255) for _ in range(3)) im = Image.new("RGB", (100, 100), color) temp = io.BytesIO() im.save(temp, "PNG") diff --git a/config.env b/config.env new file mode 100644 index 0000000..93cabd7 --- /dev/null +++ b/config.env @@ -0,0 +1,15 @@ +API_ID = 1111111 +API_HASH = "123123123" +SESSION_STRING = "alşsdaişdlaislşdaişsdlialşsdiaslşdşlasd" + +HOST = "127.0.0.1" +PORT = "1453" + +# DEBUG = +# BLOCK_DOWNLOADS = +# RESULTS_PER_PAGE = +# TGINDEX_USERNAME = +# PASSWORD = +# SHORT_URL_LEN = +# SESSION_COOKIE_LIFETIME = +# SECRET_KEY = \ No newline at end of file diff --git a/index_settings.json b/index_settings.json new file mode 100644 index 0000000..0161e9a --- /dev/null +++ b/index_settings.json @@ -0,0 +1,8 @@ +{ + "index_all": true, + "index_private": false, + "index_group": false, + "index_channel": true, + "exclude_chats": [], + "include_chats": [] + } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 91e019c..6f251e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,9 @@ -aiohtt +aiohttp aiohttp-jinja2 telethon cryptg pillow aiohttp_session[secure] python-dotenv +Flask +Werkzeug \ No newline at end of file