From bd940799ca026a20cbc7e614b86b316b7f357131 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Wed, 10 Jun 2026 11:30:19 +0700 Subject: [PATCH] refactor: simplify project, no behavior change - requirements.txt: drop pinned transitive deps (pyaes, pyasn1, rsa) - .gitignore: trim from 166 to 19 lines (drop unused template cruft) - crawl.py: remove no-op client.start(phone) after explicit sign-in - common.py: inline single-use latest_two_group_exports into compare.py; simplify _member_index - compare.py: use list_group_exports + slice; add lazy-import comment - configure.py: add lazy-import comment - config.py: lowercase error messages to match the rest of the project --- .gitignore | 161 +++-------------------------------------------- common.py | 19 ++---- compare.py | 8 ++- config.py | 6 +- configure.py | 1 + crawl.py | 1 - requirements.txt | 3 - 7 files changed, 21 insertions(+), 178 deletions(-) diff --git a/.gitignore b/.gitignore index ae03633..6e88cb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,166 +1,19 @@ +# project *.session *.csv plans/ - - -# Byte-compiled / optimized / DLL files +# python __pycache__/ *.py[cod] *$py.class -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments +# env .env -.venv -env/ +.venv/ venv/ -ENV/ -env.bak/ -venv.bak/ +env/ -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. +# tooling .idea/ +.claude/ diff --git a/common.py b/common.py index 20e4baa..307d14e 100644 --- a/common.py +++ b/common.py @@ -84,14 +84,6 @@ def get_group_export(group_id, run_time): return None -def latest_two_group_exports(group_id): - """Return the latest two export records for a group.""" - exports = list_group_exports(group_id) - if len(exports) < 2: - return None, None - return exports[-2], exports[-1] - - def diff_group_members(before_record, after_record): """Compare two group export records by Telegram member id.""" before_members = _member_index(before_record) @@ -106,12 +98,11 @@ def diff_group_members(before_record, after_record): def _member_index(record): - members = {} - for member in record.get('members', []): - member_id = member.get('id') - if member_id is not None: - members[int(member_id)] = member - return members + return { + int(member['id']): member + for member in record.get('members', []) + if member.get('id') is not None + } def _is_run_time(value): diff --git a/compare.py b/compare.py index 6246228..886c731 100644 --- a/compare.py +++ b/compare.py @@ -26,7 +26,8 @@ def parse_args(): def main(): args = parse_args() - from common import diff_group_members, get_group_export, latest_two_group_exports + # Lazy import: keeps `python compare.py --help` working when REDIS_URL is unset. + from common import diff_group_members, get_group_export, list_group_exports if args.times: before_time, after_time = args.times @@ -39,10 +40,11 @@ def main(): print(f'export not found for group {args.group_id} at {after_time}', file=sys.stderr) return 1 else: - before_record, after_record = latest_two_group_exports(args.group_id) - if before_record is None or after_record is None: + exports = list_group_exports(args.group_id) + if len(exports) < 2: print(f'need at least 2 exports for group {args.group_id}', file=sys.stderr) return 1 + before_record, after_record = exports[-2:] added, removed = diff_group_members(before_record, after_record) print_summary(args.group_id, before_record, after_record, added, removed) diff --git a/config.py b/config.py index 5712150..be984f2 100644 --- a/config.py +++ b/config.py @@ -26,19 +26,19 @@ def load_app_config(): raw = redis_client.get(key('config')) if not raw: raise SystemExit( - 'Telegram config not found in Redis. Run configure.py first.' + 'telegram config not found in Redis. run configure.py first.' ) try: config = json.loads(raw) except ValueError as exc: - raise SystemExit('Telegram config in Redis is not valid JSON.') from exc + raise SystemExit('telegram config in Redis is not valid JSON.') from exc missing = [ name for name in ('api_id', 'api_hash', 'phone', 'group_ids') if not config.get(name) ] if missing: - raise SystemExit(f'Telegram config missing: {", ".join(missing)}') + raise SystemExit(f'telegram config missing: {", ".join(missing)}') return { 'api_id': int(config['api_id']), diff --git a/configure.py b/configure.py index 8e68276..4f2a435 100644 --- a/configure.py +++ b/configure.py @@ -64,6 +64,7 @@ def main(): phone = prompt_phone(args.phone) group_ids = prompt_groups(args.groups) + # Lazy import: keeps `python configure.py --help` working when REDIS_URL is unset. from config import save_app_config config = save_app_config(api_id, api_hash, phone, group_ids) diff --git a/crawl.py b/crawl.py index f7f0609..6dacf17 100644 --- a/crawl.py +++ b/crawl.py @@ -28,7 +28,6 @@ def main(): except SessionPasswordNeededError: client.sign_in(password=getpass.getpass()) - client.start(phone) save_session(client.session.save()) run_time = new_run_time() diff --git a/requirements.txt b/requirements.txt index a73ab0e..da541bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,3 @@ -pyaes==1.6.1 -pyasn1==0.6.3 python-dotenv==1.2.2 redis[hiredis]==5.2.1 -rsa==4.9.1 Telethon==1.42.0