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 -
+
{% for chat in chats %} + class="mx-auto flex flex-col justify-items-center w-2/3 min-h-full md:w-2/5 lg:w-1/6 rounded my-2 p-2 shadow-md hover:shadow-lg hover:border hover:border-blue-300 hover:bg-blue-100">
{{chat.name}}
From d2d9305dd004c5a38cdd2387d3321efa11d1983a Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:33:48 +0530 Subject: [PATCH 6/8] =?UTF-8?q?=E2=9C=85=20gitignored=20some=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c5bfa1a..31468fb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ tests/ logo/ app.json Procfile -.vscode \ No newline at end of file +.vscode +.gitignore \ No newline at end of file From 83c5528fea598189fc0b13fb0d84a01be02891ad Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:41:41 +0530 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=90=9B=20changed=20username?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 515eb74..a29011f 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ pip3 install -U -r requirements.txt | `DEBUG` (optional) | Give `true` to set logging level to debug, info by default. | `BLOCK_DOWNLOADS` (optional) | Enable downloads or not. If any value is provided, downloads will be disabled. | `RESULTS_PER_PAGE` (optional) | Number of results to be returned per page defaults to 20. -| `USERNAME` (optional) | Username for authentication, defaults to `''`. +| `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`. | `PASSWORD` (optional) | Username for authentication, defaults to `''`. | `SESSION_COOKIE_LIFETIME` (optional) | Number of minutes, for which authenticated session is valid for, after which user has to login again. defaults to 60. | `SECRET_KEY` (optional) | Long string for signing the session cookies, required if authentication is enabled. From befe2b18b9eea6cdcdd9c80c000b14a183f6dc85 Mon Sep 17 00:00:00 2001 From: Rayan fernandes <37145078+Rayanfer32@users.noreply.github.com> Date: Sun, 6 Jun 2021 18:12:06 +0530 Subject: [PATCH 8/8] =?UTF-8?q?=E2=9C=A8=20added=20SHORT=5FURL=5FLEN=20to?= =?UTF-8?q?=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + app/config.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a29011f..018ad3f 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ pip3 install -U -r requirements.txt | `RESULTS_PER_PAGE` (optional) | Number of results to be returned per page defaults to 20. | `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`. | `PASSWORD` (optional) | Username for authentication, defaults to `''`. +| `SHORT_URL_LEN` (optional) | Url length for aliases | `SESSION_COOKIE_LIFETIME` (optional) | Number of minutes, for which authenticated session is valid for, after which user has to login again. defaults to 60. | `SECRET_KEY` (optional) | Long string for signing the session cookies, required if authentication is enabled. diff --git a/app/config.py b/app/config.py index 319558d..67e9a76 100644 --- a/app/config.py +++ b/app/config.py @@ -49,7 +49,7 @@ 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 +SHORT_URL_LEN = int(os.environ.get("SHORT_URL_LEN", 3)) authenticated = username and password SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60") try: