From 0cd6a136c0bba445cd1142e78dd27090d4c2fa5e Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:01:00 +0530 Subject: [PATCH 1/8] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Created=20common=20rou?= =?UTF-8?q?tes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/routes.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/app/routes.py b/app/routes.py index 7ab9e2c..e0b028d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,10 +5,8 @@ from telethon.tl.types import Channel, Chat, User from .config import index_settings - log = logging.getLogger(__name__) - async def setup_routes(app, handler): h = handler client = h.client @@ -24,6 +22,15 @@ async def setup_routes(app, handler): web.post("/login", h.login_post, name="login_handle"), web.get("/logout", h.logout_get, name="logout"), ] + 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), + ] if index_all: # print(await client.get_dialogs()) # dialogs = await client.get_dialogs() @@ -48,14 +55,7 @@ async def setup_routes(app, handler): 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), - ] + get_common_routes(p) ) log.debug(f"Index added for {chat.id} at /{alias_id}") @@ -65,14 +65,7 @@ async def setup_routes(app, handler): 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), - ] + get_common_routes(p) # returns list() of common routes ) log.debug(f"Index added for {chat.id} at /{alias_id}") routes.append(web.view(r"/{wildcard:.*}", h.wildcard)) From ffb719613cb3533fbf6314e154e0d937b8843547 Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:01:26 +0530 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=9A=80=20bot=20launcher=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run-dev.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 run-dev.py diff --git a/run-dev.py b/run-dev.py new file mode 100644 index 0000000..e1d8cad --- /dev/null +++ b/run-dev.py @@ -0,0 +1,29 @@ +import os +from dotenv import load_dotenv + +load_dotenv() # take environment variables from .env. + +# Code of your application, which uses environment variables (e.g. from `os.environ` or +# `os.getenv`) as if they came from the actual environment. + +# os.system("alias python3=python") + +def runSetup(): + def alert(missing="API_ID , API_HASH"): + print(f"\nCopy your {missing} and save it into Secrets(Environment variables) Sidebar!\n") + + api_id = os.getenv("API_ID") + if api_id == None: + alert() + return + + session_string = os.getenv("SESSION_STRING") + if session_string == None: + os.system("python app/generate_session_string.py") + alert(missing = "SESSION_STRING") + return + + os.system("python -m app") + +if __name__ == '__main__': + runSetup() \ No newline at end of file From dc5988ce8d5c912131d6a45e1a22d1dda9745a71 Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:02:24 +0530 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20username=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/config.py b/app/config.py index b39a496..319558d 100644 --- a/app/config.py +++ b/app/config.py @@ -44,10 +44,12 @@ host = os.environ.get("HOST", "0.0.0.0") debug = bool(os.environ.get("DEBUG")) block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS")) results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20")) -logo_folder = Path("/Temp/logo/" if platform.system() == "Windows" else "/tmp/logo") -logo_folder.mkdir(exist_ok=True) -username = os.environ.get("USERNAME", "") +logo_folder = Path("./Temp/logo/" if platform.system() == "Windows" else "/tmp/logo") +if not logo_folder.exists(): + logo_folder.mkdir(parents=True) +username = os.environ.get("TGINDEX_USERNAME", "") password = os.environ.get("PASSWORD", "") +SHORT_URL_LEN = 3 authenticated = username and password SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60") try: From 80da16a880b1caf6e84ed7430c7715837ba0400d Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:18:17 +0530 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=9A=B8=20Generate=20consistent=20alia?= =?UTF-8?q?ses=20using=20hashes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/__init__.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/views/__init__.py b/app/views/__init__.py index 24a7ba0..a8ed5c3 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -1,6 +1,10 @@ +import base64 +import hashlib import random import string +from ..config import SHORT_URL_LEN + from .home_view import HomeView from .wildcard_view import WildcardView from .download import Download @@ -13,6 +17,7 @@ from .logout_view import LogoutView from .middlewhere import middleware_factory + class Views( HomeView, Download, @@ -33,12 +38,15 @@ class Views( 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))) - ] - ) + # alias_id = "".join( + # [ + # random.choice(string.ascii_letters + string.digits) + # for _ in range(len(str(chat_id))) + # ] + # ) + orig_id = f"{title}{chat_id}" # the original id + alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode() + if alias_id in self.chat_ids: continue From 20c92dd0cce0dcd33a8bc95b54874c7ba0bbc077 Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:19:06 +0530 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=92=84=20show=20six=20tiles=20in=20a?= =?UTF-8?q?=20row?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/templates/home.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/templates/home.html b/app/templates/home.html index 0ae5aee..ef5bb33 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -4,11 +4,11 @@ Available Sources -