|
1 | 1 | '''Telegram bot that (primarily) attempts to perform url hacks to get around paywalls''' |
2 | 2 |
|
3 | 3 |
|
4 | | -__version__ = '2.3.1' |
| 4 | +__version__ = '2.4.0' |
5 | 5 |
|
6 | 6 |
|
7 | 7 | import asyncio |
|
25 | 25 | from urlextract import URLExtract |
26 | 26 |
|
27 | 27 | from data.secrets import LIST_OF_ADMINS, TOKEN # If it crashed here it's because you didn't create secrets.py correctly (or at all). Or you didn't pass docker run -v /full/path/to/data/:/home/botuser/data/ |
| 28 | +import subprocess |
28 | 29 |
|
29 | 30 |
|
30 | 31 | logging.basicConfig(format='%(asctime)s - %(levelname)s %(message)s', level=logging.INFO) |
@@ -117,6 +118,37 @@ async def chat_data(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
117 | 118 | await say(html.escape(text), update, context) |
118 | 119 |
|
119 | 120 |
|
| 121 | +@log |
| 122 | +@send_typing_action |
| 123 | +async def library_versions(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
| 124 | + '''Show installed library versions and the latest available versions online''' |
| 125 | + |
| 126 | + installed_result = subprocess.run(['pip', 'list', '--format=columns'], capture_output=True, text=True) |
| 127 | + outdated_result = subprocess.run(['pip', 'list', '--outdated', '--format=columns'], capture_output=True, text=True) |
| 128 | + |
| 129 | + installed_libraries = installed_result.stdout.splitlines() |
| 130 | + outdated_libraries = outdated_result.stdout.splitlines() |
| 131 | + |
| 132 | + outdated_dict = {line.split()[0]: line.split()[2] for line in outdated_libraries[2:]} |
| 133 | + |
| 134 | + response = [installed_libraries[0]] # Headers |
| 135 | + for line in installed_libraries[2:]: |
| 136 | + lib_name = line.split()[0] |
| 137 | + if lib_name in outdated_dict: |
| 138 | + response.append(f"{line} (latest: {outdated_dict[lib_name]})") |
| 139 | + else: |
| 140 | + response.append(line) |
| 141 | + |
| 142 | + further_instructions = '' |
| 143 | + if outdated_dict: |
| 144 | + further_instructions = "To update all outdated libraries, run:\n<code>pip list --outdated | awk 'NR>2 {print $1}' | xargs -n1 pip install -U</code>" |
| 145 | + |
| 146 | + text = html.escape("\n".join(response)) |
| 147 | + |
| 148 | + if text: |
| 149 | + await say(f'<pre>{text}</pre>{further_instructions}', update, context) |
| 150 | + |
| 151 | + |
120 | 152 | # internal bot helper stuff |
121 | 153 | async def say(text: str, update: Update, context: ContextTypes.DEFAULT_TYPE) -> int | None: |
122 | 154 | '''Send text to channel''' |
@@ -236,7 +268,7 @@ async def wayback(url: str, client: httpx.AsyncClient) -> str | None: |
236 | 268 | archive_org_url = r.json().get('archived_snapshots', {}).get('closest', {}).get('url') |
237 | 269 | if archive_org_url: |
238 | 270 | return archive_org_url |
239 | | - |
| 271 | + |
240 | 272 | except httpx.TimeoutException: |
241 | 273 | pass |
242 | 274 |
|
@@ -581,6 +613,7 @@ async def post_init(application: Application) -> None: |
581 | 613 |
|
582 | 614 | application.add_handler(CommandHandler('start', start)) |
583 | 615 | application.add_handler(CommandHandler('version', version)) |
| 616 | + application.add_handler(CommandHandler('library_versions', library_versions, filters=filters.User(user_id=LIST_OF_ADMINS))) |
584 | 617 | application.add_handler(CommandHandler('translate', translate)) |
585 | 618 | application.add_handler(CommandHandler('include', include)) |
586 | 619 | application.add_handler(CommandHandler('remove', remove)) |
|
0 commit comments