-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from life4/add-ids
Add post IDs
- Loading branch information
Showing
32 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 704 | ||
published: 2022-11-01 | ||
traces: | ||
- [type: BaseException, method: add_note] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 709 | ||
published: 2022-11-29 | ||
traces: | ||
- [module: typing, function: assert_type] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-04-18 | ||
id: 724 | ||
author: orsinium | ||
traces: | ||
- [module: asyncio, function: create_task] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-04-11 | ||
id: 723 | ||
author: orsinium | ||
traces: | ||
- [module: asyncio, function: gather] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-03-28 | ||
id: 721 | ||
author: orsinium | ||
traces: | ||
- [module: asyncio, function: run] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-04-04 | ||
id: 722 | ||
author: orsinium | ||
traces: | ||
- [module: asyncio, function: sleep] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-02-28 | ||
id: 717 | ||
author: orsinium | ||
topics: | ||
- packaging | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2022-09-27 | ||
id: 699 | ||
author: orsinium | ||
traces: | ||
- [keyword: is] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 708 | ||
published: 2022-11-22 | ||
traces: | ||
- [module: contextlib, function: chdir] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 710 | ||
published: 2022-12-06 | ||
pep: 681 | ||
python: "3.11" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 727 | ||
published: 2023-07-25 | ||
pep: 495 | ||
python: "3.6" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 706 | ||
published: 2022-11-15 | ||
traces: | ||
- [{exception: ExceptionGroup}] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 705 | ||
published: 2022-11-08 | ||
traces: | ||
- [{exception: ExceptionGroup}] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: ypankovych | ||
id: 700 | ||
published: 2022-10-11 | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 714 | ||
published: 2023-01-03 | ||
traces: | ||
- [{function: isinstance}] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 713 | ||
published: 2022-12-27 | ||
pep: 675 | ||
python: "3.11" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-03-14 | ||
id: 719 | ||
author: orsinium | ||
traces: | ||
- [module: math, function: fsum] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 701 | ||
published: 2022-10-18 | ||
traces: | ||
- [module: os, function: curdir] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-02-21 | ||
id: 716 | ||
author: orsinium | ||
topics: | ||
- packaging | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 702 | ||
published: 2022-10-24 | ||
topics: | ||
- news | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 712 | ||
published: 2022-12-20 | ||
topics: | ||
- typing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2022-09-20 | ||
id: 698 | ||
author: orsinium | ||
traces: | ||
- [module: string, type: Template] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-03-07 | ||
id: 718 | ||
author: orsinium | ||
traces: | ||
- [module: tomllib] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 703 | ||
published: 2022-10-25 | ||
pep: 657 | ||
python: "3.11" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 711 | ||
published: 2022-12-03 | ||
traces: | ||
- [module: typing, type: Self] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 728 | ||
published: 2023-08-01 | ||
traces: | ||
- [module: unittest.mock, function: seal] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2022-09-13 | ||
id: 697 | ||
author: orsinium | ||
traces: | ||
- [module: warnings] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
published: 2023-02-14 | ||
id: 715 | ||
author: orsinium | ||
topics: | ||
- packaging | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
author: orsinium | ||
id: 726 | ||
published: 2023-07-18 | ||
pep: 615 | ||
python: "3.6" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
import os | ||
import re | ||
from functools import cached_property | ||
from pathlib import Path | ||
|
||
from telethon import TelegramClient | ||
|
||
from ..post import Post | ||
from ._command import Command | ||
|
||
|
||
CHANNEL = 'pythonetc' | ||
REX_URL = re.compile(r'https?\:[a-zA-Z0-9\-\/\.\(]+') | ||
REX_PYTHON = re.compile(r'3\.[0-9]{1,2}') | ||
REX_QNAME = re.compile(r'[a-z]{2,}\.[a-zA-Z0-9]{2,}') | ||
URLs = tuple[str, ...] | ||
|
||
|
||
class AddIDsCommand(Command): | ||
"""Add Telegram post IDs to all posts that don't have it. | ||
""" | ||
name = 'add-ids' | ||
|
||
def run(self) -> int: | ||
if 'API_ID' not in os.environ: | ||
self.warn( | ||
'API_ID and API_HASH env vars required, ' | ||
'you can get them at https://my.telegram.org/apps', | ||
) | ||
return 1 | ||
asyncio.run(self._run()) | ||
return 0 | ||
|
||
async def _run(self) -> None: | ||
self.print('reading posts...') | ||
paths = self._get_paths() | ||
self.print('fetching IDs...') | ||
async with self._client: | ||
ids = await self._get_ids() | ||
self.print('setting IDs...') | ||
for keyword, id in ids.items(): | ||
path = paths.get(keyword) | ||
if path is None: | ||
continue | ||
content = path.read_text(encoding='utf-8') | ||
lines = content.splitlines() | ||
lines.insert(2, f'id: {id}') | ||
new_content = '\n'.join(lines) | ||
new_content = new_content.rstrip() + '\n' | ||
assert new_content != content | ||
path.write_text(new_content, encoding='utf-8') | ||
self.print(f'added ID for {path.name}') | ||
|
||
def _get_paths(self) -> dict[URLs, Path]: | ||
paths: dict[URLs, Path] = {} | ||
for path in sorted(Path('posts').iterdir()): | ||
if path.suffix != '.md': | ||
continue | ||
post = Post.from_path(path) | ||
if post.id is not None: | ||
continue | ||
keywords = self._get_keywords(post.md_content) | ||
if not keywords: | ||
continue | ||
if keywords in paths: | ||
name1 = path.name | ||
name2 = paths[keywords].name | ||
msg = f'duplicate set of keywords: {name1} and {name2}' | ||
raise RuntimeError(msg) | ||
paths[keywords] = path | ||
return paths | ||
|
||
async def _get_ids(self) -> dict[URLs, int]: | ||
ids = {} | ||
async for message in self._client.iter_messages(CHANNEL): | ||
if message.text is None: | ||
continue | ||
keywords = self._get_keywords(message.text) | ||
if not keywords: | ||
continue | ||
if keywords in ids: | ||
continue | ||
ids[keywords] = message.id | ||
return ids | ||
|
||
def _get_keywords(self, text: str) -> URLs: | ||
""" | ||
Get some key components from the text (URLs, qualnames, Python versions) | ||
that can be used to uniquely identify a text. | ||
It allows us to match the same text ignoring changes in formatting | ||
or corrected typos. | ||
""" | ||
result = REX_URL.findall(text) | ||
result.extend(REX_PYTHON.findall(text)) | ||
result.extend(REX_QNAME.findall(text)) | ||
return tuple(result) | ||
|
||
@cached_property | ||
def _client(self) -> TelegramClient: | ||
return TelegramClient( | ||
'bot', | ||
api_id=os.environ['API_ID'], | ||
api_hash=os.environ['API_HASH'], | ||
) |