tgdata 0.0.2__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tgdata-0.0.2 → tgdata-0.0.4}/PKG-INFO +22 -2
- {tgdata-0.0.2 → tgdata-0.0.4}/README.md +21 -1
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/__init__.py +1 -1
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/connection_engine.py +32 -10
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/message_engine.py +42 -5
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata.egg-info/PKG-INFO +22 -2
- {tgdata-0.0.2 → tgdata-0.0.4}/setup.cfg +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/setup.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/models.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/progress.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/__init__.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/my_test.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_00_connection.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_01_list_group_chats.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_02_get_message_count.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_03_get_all_messages.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_04_get_all_messages_in_batches.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_05_with_progress_feature.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_06_polling_simple.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_07_on_new_message_simple.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_07_real_time_event_handler.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_08_batch_callback.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_08_polling.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_10_advanced_features.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_debug_min_id.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_polling_debug.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/smoke_tests/test_username_support.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/tgdata.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata/utils.py +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata.egg-info/SOURCES.txt +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata.egg-info/dependency_links.txt +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata.egg-info/requires.txt +0 -0
- {tgdata-0.0.2 → tgdata-0.0.4}/tgdata.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tgdata
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: A production-grade Python library for extracting and processing Telegram group and channel messages
|
|
5
5
|
Author: enes kuzucu
|
|
6
6
|
Classifier: Development Status :: 3 - Alpha
|
|
@@ -67,10 +67,21 @@ tgdata supports 1 authentication at the moment.
|
|
|
67
67
|
api_id = 1234566
|
|
68
68
|
api_hash = a24adjfakjdfakjshdflkajsbdflk
|
|
69
69
|
phone = +905064004949 # use full phone number including + and country code
|
|
70
|
-
username = 'powerpuffdude'
|
|
71
70
|
|
|
71
|
+
# Where the Telethon session lives (".session" is appended automatically).
|
|
72
|
+
# Use an ABSOLUTE path — a bare name creates the file in whatever directory
|
|
73
|
+
# the process happens to run from. Do not quote values: INI values are raw
|
|
74
|
+
# text, so quotes become part of the value (tgdata strips them defensively
|
|
75
|
+
# since 0.0.4, but don't rely on it).
|
|
76
|
+
session_file = /absolute/path/to/my_session
|
|
72
77
|
```
|
|
73
78
|
|
|
79
|
+
**Session naming precedence** (since 0.0.4): an explicit `session_file`
|
|
80
|
+
always wins. A legacy `username` key is used as the session name only when
|
|
81
|
+
no `session_file` is set — configs that relied on username-named sessions
|
|
82
|
+
keep working, but new configs should set `session_file` and omit `username`
|
|
83
|
+
(it plays no part in authentication).
|
|
84
|
+
|
|
74
85
|
|
|
75
86
|
## Quick Start
|
|
76
87
|
|
|
@@ -273,6 +284,15 @@ messages = await tg.get_messages(
|
|
|
273
284
|
# Note: CSV export drops MediaData; JSON stores a "[Binary data]" placeholder.
|
|
274
285
|
```
|
|
275
286
|
|
|
287
|
+
Downloaded files (modes A and B) are named `<ChatId>_<MessageId>.<ext>` — e.g.
|
|
288
|
+
`1707717812_183019.jpg` — so every file is traceable to its exact message and
|
|
289
|
+
group from the filename alone (and won't collide across groups sharing a folder).
|
|
290
|
+
The precise path is also recorded per row in the `MediaPath` column.
|
|
291
|
+
|
|
292
|
+
**Re-scraping is idempotent:** if a message's file is already on disk, it is
|
|
293
|
+
reused, not re-downloaded — so pulling the same (or an overlapping) range again
|
|
294
|
+
never duplicates files or re-fetches bytes. Each message maps to exactly one file.
|
|
295
|
+
|
|
276
296
|
### Progress Monitoring
|
|
277
297
|
|
|
278
298
|
```python
|
|
@@ -41,10 +41,21 @@ tgdata supports 1 authentication at the moment.
|
|
|
41
41
|
api_id = 1234566
|
|
42
42
|
api_hash = a24adjfakjdfakjshdflkajsbdflk
|
|
43
43
|
phone = +905064004949 # use full phone number including + and country code
|
|
44
|
-
username = 'powerpuffdude'
|
|
45
44
|
|
|
45
|
+
# Where the Telethon session lives (".session" is appended automatically).
|
|
46
|
+
# Use an ABSOLUTE path — a bare name creates the file in whatever directory
|
|
47
|
+
# the process happens to run from. Do not quote values: INI values are raw
|
|
48
|
+
# text, so quotes become part of the value (tgdata strips them defensively
|
|
49
|
+
# since 0.0.4, but don't rely on it).
|
|
50
|
+
session_file = /absolute/path/to/my_session
|
|
46
51
|
```
|
|
47
52
|
|
|
53
|
+
**Session naming precedence** (since 0.0.4): an explicit `session_file`
|
|
54
|
+
always wins. A legacy `username` key is used as the session name only when
|
|
55
|
+
no `session_file` is set — configs that relied on username-named sessions
|
|
56
|
+
keep working, but new configs should set `session_file` and omit `username`
|
|
57
|
+
(it plays no part in authentication).
|
|
58
|
+
|
|
48
59
|
|
|
49
60
|
## Quick Start
|
|
50
61
|
|
|
@@ -247,6 +258,15 @@ messages = await tg.get_messages(
|
|
|
247
258
|
# Note: CSV export drops MediaData; JSON stores a "[Binary data]" placeholder.
|
|
248
259
|
```
|
|
249
260
|
|
|
261
|
+
Downloaded files (modes A and B) are named `<ChatId>_<MessageId>.<ext>` — e.g.
|
|
262
|
+
`1707717812_183019.jpg` — so every file is traceable to its exact message and
|
|
263
|
+
group from the filename alone (and won't collide across groups sharing a folder).
|
|
264
|
+
The precise path is also recorded per row in the `MediaPath` column.
|
|
265
|
+
|
|
266
|
+
**Re-scraping is idempotent:** if a message's file is already on disk, it is
|
|
267
|
+
reused, not re-downloaded — so pulling the same (or an overlapping) range again
|
|
268
|
+
never duplicates files or re-fetches bytes. Each message maps to exactly one file.
|
|
269
|
+
|
|
250
270
|
### Progress Monitoring
|
|
251
271
|
|
|
252
272
|
```python
|
|
@@ -117,18 +117,38 @@ class ConnectionEngine:
|
|
|
117
117
|
|
|
118
118
|
if telegram_section is None:
|
|
119
119
|
raise ValueError("No [telegram] or [Telegram] section found in config file")
|
|
120
|
-
|
|
120
|
+
|
|
121
|
+
def _unquote(value):
|
|
122
|
+
# INI values are raw text: `username = 'name'` yields the QUOTES as
|
|
123
|
+
# part of the value, and a quoted session name becomes a file
|
|
124
|
+
# literally called 'name'.session on disk. Strip surrounding
|
|
125
|
+
# quotes from every string we read.
|
|
126
|
+
return value.strip().strip("'\"") if isinstance(value, str) else value
|
|
127
|
+
|
|
128
|
+
section = config[telegram_section]
|
|
129
|
+
raw_session = _unquote(section.get('session_file'))
|
|
130
|
+
raw_username = _unquote(section.get('username'))
|
|
131
|
+
|
|
132
|
+
# Session naming precedence: an EXPLICIT session_file always wins — it
|
|
133
|
+
# is the documented home of the credential, and honoring it keeps the
|
|
134
|
+
# session's location independent of the process's working directory.
|
|
135
|
+
# The legacy behavior (username as the session name) applies only when
|
|
136
|
+
# no session_file is configured, so old username-only configs keep
|
|
137
|
+
# finding their existing sessions. 'telegram_session' is the final
|
|
138
|
+
# fallback when neither is set.
|
|
139
|
+
session_name = raw_session or raw_username or 'telegram_session'
|
|
140
|
+
|
|
121
141
|
self._config = ConnectionConfig(
|
|
122
|
-
api_id=
|
|
123
|
-
api_hash=
|
|
124
|
-
session_file=
|
|
125
|
-
phone=
|
|
126
|
-
username=
|
|
142
|
+
api_id=_unquote(section['api_id']),
|
|
143
|
+
api_hash=_unquote(section['api_hash']),
|
|
144
|
+
session_file=session_name,
|
|
145
|
+
phone=_unquote(section.get('phone')),
|
|
146
|
+
username=raw_username,
|
|
127
147
|
max_retries=self.max_retries,
|
|
128
148
|
retry_delay=self.retry_delay,
|
|
129
149
|
exponential_backoff=self.exponential_backoff
|
|
130
150
|
)
|
|
131
|
-
|
|
151
|
+
|
|
132
152
|
return self._config
|
|
133
153
|
|
|
134
154
|
async def get_client(self) -> TelegramClient:
|
|
@@ -173,10 +193,12 @@ class ConnectionEngine:
|
|
|
173
193
|
"""Create and authenticate the primary client once (may be interactive)."""
|
|
174
194
|
config = self._load_config()
|
|
175
195
|
|
|
176
|
-
#
|
|
177
|
-
|
|
196
|
+
# Session naming is resolved ONCE in _load_config (explicit
|
|
197
|
+
# session_file > username > default) — config.session_file IS the
|
|
198
|
+
# resolved name, so the primary client and the pool clients below
|
|
199
|
+
# can never disagree about where the session lives.
|
|
178
200
|
self._primary_client = TelegramClient(
|
|
179
|
-
|
|
201
|
+
config.session_file,
|
|
180
202
|
config.api_id,
|
|
181
203
|
config.api_hash
|
|
182
204
|
)
|
|
@@ -9,6 +9,7 @@ import os
|
|
|
9
9
|
from datetime import datetime, timedelta, timezone
|
|
10
10
|
from typing import Optional, List, Dict, Any, Callable, Iterable, Union
|
|
11
11
|
import pandas as pd
|
|
12
|
+
from telethon import utils as tl_utils
|
|
12
13
|
from telethon.errors import FloodWaitError
|
|
13
14
|
from telethon.tl import types as tl_types
|
|
14
15
|
|
|
@@ -406,6 +407,36 @@ class MessageEngine:
|
|
|
406
407
|
return 'poll'
|
|
407
408
|
return 'other'
|
|
408
409
|
|
|
410
|
+
@staticmethod
|
|
411
|
+
def _media_stem(chat_id: Optional[int], message_id: int) -> str:
|
|
412
|
+
"""
|
|
413
|
+
Deterministic, self-identifying filename stem for a message's media:
|
|
414
|
+
'<chat_id>_<message_id>' (falls back to just the message id if chat_id is
|
|
415
|
+
unknown). Passed WITHOUT an extension — Telethon appends the correct one
|
|
416
|
+
(.jpg/.mp4/...). This makes each file traceable to its exact message/group
|
|
417
|
+
from the filename alone, and unique across groups sharing one folder.
|
|
418
|
+
"""
|
|
419
|
+
return f"{chat_id}_{message_id}" if chat_id is not None else str(message_id)
|
|
420
|
+
|
|
421
|
+
async def _download_or_reuse(self, client, msg, media_dir: str, chat_id: Optional[int]):
|
|
422
|
+
"""
|
|
423
|
+
Download a message's media to '<media_dir>/<chat_id>_<message_id>.<ext>',
|
|
424
|
+
REUSING the file if it already exists (idempotent re-scrape).
|
|
425
|
+
|
|
426
|
+
The extension is predicted up-front with telethon.utils.get_extension (an
|
|
427
|
+
O(1) exists-check, no directory scan), so re-scraping an overlapping range
|
|
428
|
+
neither re-downloads the bytes nor produces Telethon's ' (1)' duplicates —
|
|
429
|
+
each message maps to exactly one file, forever.
|
|
430
|
+
"""
|
|
431
|
+
stem = self._media_stem(chat_id, msg.id)
|
|
432
|
+
ext = tl_utils.get_extension(msg.media) or ''
|
|
433
|
+
target = os.path.join(media_dir, stem + ext)
|
|
434
|
+
if ext and os.path.exists(target):
|
|
435
|
+
logger.debug(f"Media for message {msg.id} already present ({stem}{ext}); reusing")
|
|
436
|
+
return target
|
|
437
|
+
# File doesn't exist yet: Telethon writes exactly to `target` (no rename).
|
|
438
|
+
return await client.download_media(msg, file=target)
|
|
439
|
+
|
|
409
440
|
async def _process_message(self,
|
|
410
441
|
msg,
|
|
411
442
|
client,
|
|
@@ -454,7 +485,9 @@ class MessageEngine:
|
|
|
454
485
|
# (e.g. text-only or a link-preview webpage).
|
|
455
486
|
if media_dir and msg.media is not None:
|
|
456
487
|
try:
|
|
457
|
-
message_data.media_path = await
|
|
488
|
+
message_data.media_path = await self._download_or_reuse(
|
|
489
|
+
client, msg, media_dir, chat_id
|
|
490
|
+
)
|
|
458
491
|
except Exception as e:
|
|
459
492
|
logger.warning(f"Failed to download media for message {msg.id}: {e}")
|
|
460
493
|
|
|
@@ -519,10 +552,14 @@ class MessageEngine:
|
|
|
519
552
|
results[mid] = None
|
|
520
553
|
continue
|
|
521
554
|
try:
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
555
|
+
if output_dir:
|
|
556
|
+
# Named '<chat_id>_<message_id>.<ext>', reused if already present
|
|
557
|
+
results[mid] = await self._download_or_reuse(
|
|
558
|
+
client, msg, output_dir, entity.id
|
|
559
|
+
)
|
|
560
|
+
else:
|
|
561
|
+
# In-memory bytes when no output_dir was given
|
|
562
|
+
results[mid] = await client.download_media(msg, file=bytes)
|
|
526
563
|
except Exception as e:
|
|
527
564
|
logger.warning(f"Failed to download media for message {mid}: {e}")
|
|
528
565
|
results[mid] = None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tgdata
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: A production-grade Python library for extracting and processing Telegram group and channel messages
|
|
5
5
|
Author: enes kuzucu
|
|
6
6
|
Classifier: Development Status :: 3 - Alpha
|
|
@@ -67,10 +67,21 @@ tgdata supports 1 authentication at the moment.
|
|
|
67
67
|
api_id = 1234566
|
|
68
68
|
api_hash = a24adjfakjdfakjshdflkajsbdflk
|
|
69
69
|
phone = +905064004949 # use full phone number including + and country code
|
|
70
|
-
username = 'powerpuffdude'
|
|
71
70
|
|
|
71
|
+
# Where the Telethon session lives (".session" is appended automatically).
|
|
72
|
+
# Use an ABSOLUTE path — a bare name creates the file in whatever directory
|
|
73
|
+
# the process happens to run from. Do not quote values: INI values are raw
|
|
74
|
+
# text, so quotes become part of the value (tgdata strips them defensively
|
|
75
|
+
# since 0.0.4, but don't rely on it).
|
|
76
|
+
session_file = /absolute/path/to/my_session
|
|
72
77
|
```
|
|
73
78
|
|
|
79
|
+
**Session naming precedence** (since 0.0.4): an explicit `session_file`
|
|
80
|
+
always wins. A legacy `username` key is used as the session name only when
|
|
81
|
+
no `session_file` is set — configs that relied on username-named sessions
|
|
82
|
+
keep working, but new configs should set `session_file` and omit `username`
|
|
83
|
+
(it plays no part in authentication).
|
|
84
|
+
|
|
74
85
|
|
|
75
86
|
## Quick Start
|
|
76
87
|
|
|
@@ -273,6 +284,15 @@ messages = await tg.get_messages(
|
|
|
273
284
|
# Note: CSV export drops MediaData; JSON stores a "[Binary data]" placeholder.
|
|
274
285
|
```
|
|
275
286
|
|
|
287
|
+
Downloaded files (modes A and B) are named `<ChatId>_<MessageId>.<ext>` — e.g.
|
|
288
|
+
`1707717812_183019.jpg` — so every file is traceable to its exact message and
|
|
289
|
+
group from the filename alone (and won't collide across groups sharing a folder).
|
|
290
|
+
The precise path is also recorded per row in the `MediaPath` column.
|
|
291
|
+
|
|
292
|
+
**Re-scraping is idempotent:** if a message's file is already on disk, it is
|
|
293
|
+
reused, not re-downloaded — so pulling the same (or an overlapping) range again
|
|
294
|
+
never duplicates files or re-fetches bytes. Each message maps to exactly one file.
|
|
295
|
+
|
|
276
296
|
### Progress Monitoring
|
|
277
297
|
|
|
278
298
|
```python
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|