|
| 1 | +# ruff: noqa: E741 |
1 | 2 | from __future__ import annotations |
2 | 3 |
|
3 | 4 | import csv |
4 | 5 | import re |
5 | 6 | import sys |
6 | 7 | from pathlib import Path |
7 | 8 |
|
8 | | -import requests # requests==2.28.1 |
| 9 | +import requests |
9 | 10 |
|
| 11 | +if len(sys.argv) != 2: |
| 12 | + sys.exit(1) |
10 | 13 |
|
11 | | -def sort_tsv_files(): |
12 | | - """Sorts TSV files in the 'source' directory by their keys.""" |
| 14 | +if sys.argv[1] == "sort": |
13 | 15 | for p in Path("source").glob("*.tsv"): |
14 | | - with p.open() as f: # Replaced open() with Path.open() |
| 16 | + with p.open() as f: |
15 | 17 | reader = csv.reader(f, delimiter="\t") |
16 | | - dct = {k: v for k, v in reader if k != "id"} # Dictionary comprehension |
| 18 | + dct = {k: v for k, v in reader if k != "id"} |
17 | 19 | keys = sorted(dct) |
18 | 20 |
|
19 | | - with p.open("w") as f: # Replaced open() with Path.open() |
| 21 | + with p.open("w") as f: |
20 | 22 | f.write("id\tmessage\n") |
21 | 23 | for i, item in enumerate(keys, start=1): |
22 | 24 | f.write(f"{item}\t{dct[item]}") |
23 | 25 | if i != len(keys): |
24 | 26 | f.write("\n") |
25 | 27 |
|
| 28 | +elif sys.argv[1] == "scrape": |
| 29 | + b = "https://core.telegram.org" |
| 30 | + c = "/api/errors" |
| 31 | + a = requests.get(b + c) |
| 32 | + d = a.text |
| 33 | + e = r"\<a\ href\=\"(.*)\"\>here.*\<\/a\>" |
| 34 | + f = re.search(e, d) |
| 35 | + if f: |
| 36 | + a = requests.get(b + f.group(1)) |
| 37 | + d = a.json() |
| 38 | + e = d.get("errors", []) |
| 39 | + for h in e: |
| 40 | + dct = {} |
| 41 | + j = d.get("errors").get(h) |
| 42 | + for k in j: |
| 43 | + if k.endswith("_*"): |
| 44 | + continue |
| 45 | + g = d.get("descriptions") |
| 46 | + l = g.get(k) |
| 47 | + m = k.replace("_%d", "_X") |
| 48 | + l = l.replace("%d", "{value}") |
| 49 | + l = l.replace("»", "»") |
| 50 | + l = l.replace("«", "«") |
| 51 | + l = l.replace("](/api/", f"]({b}/api/") |
| 52 | + dct[m] = l |
| 53 | + |
| 54 | + for p in Path("source/").glob(f"{h}*.tsv"): |
| 55 | + with p.open() as f: |
| 56 | + reader = csv.reader(f, delimiter="\t") |
| 57 | + for k, v in reader: |
| 58 | + if k != "id": |
| 59 | + dct[k] = v |
26 | 60 |
|
27 | | -def scrape_telegram_errors(): |
28 | | - """Scrapes error data from Telegram's API and updates corresponding TSV files.""" |
29 | | - base_url = "https://corefork.telegram.org" |
30 | | - api_errors_path = "/api/errors" |
31 | | - response = requests.get(base_url + api_errors_path) |
32 | | - if response.status_code != 200: |
33 | | - print("Failed to fetch errors from Telegram API") |
34 | | - sys.exit(1) |
35 | | - |
36 | | - # Extract link to additional error details |
37 | | - html_content = response.text |
38 | | - link_pattern = r'<a href="(.*)">here.*</a>' |
39 | | - match = re.search(link_pattern, html_content) |
40 | | - if not match: |
41 | | - print("No error details found") |
42 | | - return |
43 | | - |
44 | | - error_url = base_url + match[1] |
45 | | - error_response = requests.get(error_url) |
46 | | - if error_response.status_code != 200: |
47 | | - print("Failed to fetch detailed errors") |
48 | | - sys.exit(1) |
49 | | - |
50 | | - errors_data = error_response.json() |
51 | | - process_errors(errors_data, base_url) |
52 | | - |
53 | | - |
54 | | -def process_errors(errors_data: dict, base_url: str): |
55 | | - """Processes errors and updates the TSV files with error messages.""" |
56 | | - error_entries = errors_data.get("errors", {}) |
57 | | - descriptions = errors_data.get("descriptions", {}) |
58 | | - |
59 | | - for error_code, error_list in error_entries.items(): |
60 | | - dct = {} |
61 | | - |
62 | | - # Process each error |
63 | | - for error in error_list: |
64 | | - if error.endswith("_*"): |
65 | | - continue |
66 | | - |
67 | | - description = descriptions.get(error, "") |
68 | | - description = format_description(description, base_url) |
69 | | - formatted_key = error.replace("_%d", "_X") |
70 | | - dct[formatted_key] = description |
71 | | - |
72 | | - update_tsv_files(error_code, dct) |
73 | | - |
74 | | - |
75 | | -def format_description(description: str, base_url: str) -> str: |
76 | | - """Formats the description by replacing specific characters and links.""" |
77 | | - description = description.replace("%d", "{value}") |
78 | | - description = description.replace("»", "»") |
79 | | - description = description.replace("«", "«") |
80 | | - return description.replace("](/api/", f"]({base_url}/api/") |
81 | | - |
82 | | - |
83 | | -def update_tsv_files(error_code: str, dct: dict): |
84 | | - """Updates the TSV files for the given error code with the provided dictionary of error messages.""" |
85 | | - for p in Path("source/").glob(f"{error_code}*.tsv"): |
86 | | - with p.open() as f: # Replaced open() with Path.open() |
87 | | - reader = csv.reader(f, delimiter="\t") |
88 | | - dct.update( |
89 | | - {k: v for k, v in reader if k != "id"} |
90 | | - ) # Dictionary comprehension |
91 | | - |
92 | | - keys = sorted(dct) |
93 | | - |
94 | | - with p.open("w") as f: # Replaced open() with Path.open() |
95 | | - f.write("id\tmessage\n") |
96 | | - for i, item in enumerate(keys, start=1): |
97 | | - f.write(f"{item}\t{dct[item]}\n") |
98 | | - |
| 61 | + keys = sorted(dct) |
99 | 62 |
|
100 | | -if sys.argv[1] == "sort": |
101 | | - sort_tsv_files() |
102 | | -elif sys.argv[1] == "scrape": |
103 | | - scrape_telegram_errors() |
| 63 | + for p in Path("source/").glob(f"{h}*.tsv"): |
| 64 | + with p.open("w") as f: |
| 65 | + f.write("id\tmessage\n") |
| 66 | + for i, item in enumerate(keys, start=1): |
| 67 | + f.write(f"{item}\t{dct[item]}\n") |
0 commit comments