From ef797f127c0abf4f28f9af5c7fd16c1f349075cb Mon Sep 17 00:00:00 2001 From: octospacc Date: Wed, 10 May 2023 00:48:56 +0200 Subject: [PATCH] Use file session; Fix HTML to have easier find/replace; Include libs --- .gitignore | 3 +- README.md | 66 +++++++++++++---------------- app/config.py | 7 --- app/generate_session_string.py | 10 ----- app/main.py | 3 +- app/telegram.py | 5 +-- app/templates/footer.html | 36 ++++++---------- app/templates/header.html | 50 +++++++++++----------- app/templates/home.html | 6 +-- app/templates/index.html | 22 +++++----- app/templates/info.html | 24 +++++++---- app/templates/js/fluidplayer.min.js | 2 + app/views/info_view.py | 2 +- 13 files changed, 106 insertions(+), 130 deletions(-) delete mode 100644 app/generate_session_string.py create mode 100644 app/templates/js/fluidplayer.min.js diff --git a/.gitignore b/.gitignore index 3f37279..8182e3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.session +*.session-journal .env __pycache__/ venv/ @@ -10,4 +11,4 @@ app.json Procfile .vscode/ .gitignore -pyproject.toml \ No newline at end of file +pyproject.toml diff --git a/README.md b/README.md index e6b07ba..e8b2848 100644 --- a/README.md +++ b/README.md @@ -15,50 +15,47 @@ - **Clone to local machine.** -```bash -$ git clone https://github.com/odysseusmax/tg-index.git - -$ cd tg-index +```sh +git clone https://github.com/odysseusmax/tg-index.git +cd tg-index ``` - **Create and activate virtual environment.** -```bash -$ python -m venv venv - -$ source venv/bin/activate +```sh +python -m venv venv +source venv/bin/activate ``` - **Install dependencies.** -```bash -$ pip3 install -U -r requirements.txt +```sh +pip3 install -U -r requirements.txt ``` - **Environment Variables.** -| Variable Name | Value | -| ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `API_ID` (required) | Telegram api_id obtained from . | -| `API_HASH` (required) | Telegram api_hash obtained from . | -| `INDEX_SETTINGS` (required) | See the below description. | -| `SESSION_STRING` (required) | String obtained by running `$ python3 app/generate_session_string.py`. (Login with the telegram account which is a participant of the given channel (or chat). | -| `PORT` (optional) | Port on which app should listen to, defaults to 8080. | -| `HOST` (optional) | Host name on which app should listen to, defaults to 0.0.0.0. | -| `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. | -| `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`. | -| `PASSWORD` (optional) | Password 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) | 32 characters long string for signing the session cookies, required if authentication is enabled. | +| Variable Name | Value | +| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | +| `API_ID` (required) | Telegram api_id obtained from . | +| `API_HASH` (required) | Telegram api_hash obtained from . | +| `INDEX_SETTINGS` (required) | See the below description. | +| `PORT` (optional) | Port on which app should listen to, defaults to 8080. | +| `HOST` (optional) | Host name on which app should listen to, defaults to 0.0.0.0. | +| `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. | +| `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`. | +| `PASSWORD` (optional) | Password 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) | 32 characters long string for signing the session cookies, required if authentication is enabled. | - **Setting value for `INDEX_SETTINGS`** This is the general format, change the values of corresponding fields as your requirements. You can copy paste this as is to index all the channels available in your account. -**Remember to remove spaces.** +**Remember to remove newlines.** ```json { @@ -84,18 +81,15 @@ This is the general format, change the values of corresponding fields as your re python3 -m app ``` +Note: you will be asked to **log in** the first time. (Login with the telegram account which is a participant of the chats you want to index). +After that, a `tg-index.session` file will be stored in the current working directory. **Keep it secret and secure**. + ## Deploy Guide (Repl.it) A detailed and beginner friendly guide on how to deploy this project on a free instance of can be found [here](./repl-config/replit-deploy-guide.md). -## Contributions +## License, Credits -Contributions are welcome. +This program is forked from [odysseusmax/tg-index](https://github.com/odysseusmax/tg-index). -## Contact - -You can contact me [@odysseusmax](https://tx.me/odysseusmax). - -## License - -Code released under [The GNU General Public License](LICENSE). +As per the original, code is released under [The GNU General Public License](LICENSE). diff --git a/app/config.py b/app/config.py index bf8fc53..d9066c1 100644 --- a/app/config.py +++ b/app/config.py @@ -34,13 +34,6 @@ except Exception: print("\n\nPlease set the INDEX_SETTINGS environment variable correctly") sys.exit(1) -try: - session_string = os.environ["SESSION_STRING"] -except (KeyError, ValueError): - traceback.print_exc() - print("\n\nPlease set the SESSION_STRING environment variable correctly") - sys.exit(1) - host = os.environ.get("HOST", "0.0.0.0") debug = bool(os.environ.get("DEBUG")) block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS")) diff --git a/app/generate_session_string.py b/app/generate_session_string.py deleted file mode 100644 index 6cace64..0000000 --- a/app/generate_session_string.py +++ /dev/null @@ -1,10 +0,0 @@ -import os - -from telethon.sync import TelegramClient -from telethon.sessions import StringSession - -api_id = int(os.getenv('API_ID') or input("Enter your API_ID: ")) -api_hash = os.getenv('API_HASH') or input("Enter your API_HASH: ") - -with TelegramClient(StringSession(), api_id, api_hash) as client: - print("\n" + client.session.save()) diff --git a/app/main.py b/app/main.py index bf08b97..377e7b9 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,6 @@ from .views import Views, middleware_factory from .config import ( host, port, - session_string, api_id, api_hash, authenticated, @@ -54,7 +53,7 @@ class Indexer: self.server.on_startup.append(self.startup) self.server.on_cleanup.append(self.cleanup) - self.tg_client = Client(session_string, api_id, api_hash) + self.tg_client = Client(api_id, api_hash) self.server["is_authenticated"] = authenticated self.server["username"] = username diff --git a/app/telegram.py b/app/telegram.py index 5dcd145..00ff8a9 100644 --- a/app/telegram.py +++ b/app/telegram.py @@ -3,12 +3,11 @@ import logging import asyncio from telethon import TelegramClient, utils -from telethon.sessions import StringSession class Client(TelegramClient): - def __init__(self, session_string: str, *args, **kwargs): - super().__init__(StringSession(session_string), *args, **kwargs) + def __init__(self, *args, **kwargs): + super().__init__("tg-index.session", *args, **kwargs) self.log = logging.getLogger(__name__) async def download(self, file, file_size, offset, limit): diff --git a/app/templates/footer.html b/app/templates/footer.html index d4804fd..9400999 100644 --- a/app/templates/footer.html +++ b/app/templates/footer.html @@ -1,26 +1,18 @@ - + - - \ No newline at end of file + diff --git a/app/templates/header.html b/app/templates/header.html index 1489435..0d2b89b 100644 --- a/app/templates/header.html +++ b/app/templates/header.html @@ -1,50 +1,52 @@ - + - - - - {% if title %} {{title}} {% else %} Telegram Index {% endif %} - - +
- Telegram index + Telegram Index
+ [Source Code] - {% if authenticated %} - - {% else %} {% endif %} + Theme + + {% if authenticated %} + + {% else %} {% endif %}
-
\ No newline at end of file +
diff --git a/app/templates/home.html b/app/templates/home.html index 6ddeb11..82a5438 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -7,10 +7,10 @@
{% for chat in chats %} - - +
{{chat.name}}
@@ -18,4 +18,4 @@
-{% include 'footer.html' %} \ No newline at end of file +{% include 'footer.html' %} diff --git a/app/templates/index.html b/app/templates/index.html index 1307244..aba8f2c 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,14 +1,13 @@ {% include 'header.html' %} {% block javascript %} {% endblock %}
@@ -46,12 +45,11 @@
{% if item.media %} - +
{{item.insight}}
@@ -68,7 +66,7 @@ {% if 'video' in item.mime_type %} - @@ -94,7 +92,7 @@ {% else %} - +
{{item.insight}}
{% endif %} @@ -114,17 +112,17 @@
{% if prev_page %} - Page {{prev_page.no}} + title="Previous page">Page {{prev_page.no}} {% endif %}

Page {{cur_page}}

{% if next_page %} - Page {{next_page.no}} + title="Next page">Page {{next_page.no}} {% endif %}
@@ -136,4 +134,4 @@ {% endif %} -{% include 'footer.html' %} \ No newline at end of file +{% include 'footer.html' %} diff --git a/app/templates/info.html b/app/templates/info.html index 9cc7daa..2484667 100644 --- a/app/templates/info.html +++ b/app/templates/info.html @@ -9,15 +9,15 @@
{% if media.image %} - {{name}} + {{name}} {% elif media.video %}
-
@@ -52,9 +52,9 @@
- {{name}} -