tgdata 0.0.4__tar.gz → 0.0.6__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.4 → tgdata-0.0.6}/PKG-INFO +8 -1
- {tgdata-0.0.4 → tgdata-0.0.6}/README.md +7 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/__init__.py +8 -2
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/message_engine.py +76 -9
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/tgdata.py +4 -2
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata.egg-info/PKG-INFO +8 -1
- {tgdata-0.0.4 → tgdata-0.0.6}/setup.cfg +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/setup.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/connection_engine.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/models.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/progress.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/__init__.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/my_test.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_00_connection.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_01_list_group_chats.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_02_get_message_count.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_03_get_all_messages.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_04_get_all_messages_in_batches.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_05_with_progress_feature.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_06_polling_simple.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_07_on_new_message_simple.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_07_real_time_event_handler.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_08_batch_callback.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_08_polling.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_10_advanced_features.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_debug_min_id.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_polling_debug.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/smoke_tests/test_username_support.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata/utils.py +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata.egg-info/SOURCES.txt +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata.egg-info/dependency_links.txt +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/tgdata.egg-info/requires.txt +0 -0
- {tgdata-0.0.4 → tgdata-0.0.6}/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.6
|
|
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
|
|
@@ -192,6 +192,13 @@ messages = await tg.get_messages(
|
|
|
192
192
|
)
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
+
A numeric id resolves only if the session can actually see the group. On a
|
|
196
|
+
cache miss tgdata syncs the account's chat list once and retries — so fresh
|
|
197
|
+
sessions work for groups the account belongs to. If the group still can't be
|
|
198
|
+
found it raises `tgdata.GroupAccessError` (the account is not a member, or
|
|
199
|
+
the id is wrong) instead of Telethon's generic "Could not find the input
|
|
200
|
+
entity" ValueError.
|
|
201
|
+
|
|
195
202
|
### get_messages with start_date parameter
|
|
196
203
|
|
|
197
204
|
```python
|
|
@@ -166,6 +166,13 @@ messages = await tg.get_messages(
|
|
|
166
166
|
)
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
+
A numeric id resolves only if the session can actually see the group. On a
|
|
170
|
+
cache miss tgdata syncs the account's chat list once and retries — so fresh
|
|
171
|
+
sessions work for groups the account belongs to. If the group still can't be
|
|
172
|
+
found it raises `tgdata.GroupAccessError` (the account is not a member, or
|
|
173
|
+
the id is wrong) instead of Telethon's generic "Could not find the input
|
|
174
|
+
entity" ValueError.
|
|
175
|
+
|
|
169
176
|
### get_messages with start_date parameter
|
|
170
177
|
|
|
171
178
|
```python
|
|
@@ -7,6 +7,9 @@ A production-grade Python library for extracting and processing Telegram group a
|
|
|
7
7
|
# Main class
|
|
8
8
|
from .tgdata import TgData
|
|
9
9
|
|
|
10
|
+
# Errors
|
|
11
|
+
from .message_engine import GroupAccessError
|
|
12
|
+
|
|
10
13
|
# Models
|
|
11
14
|
from .models import (
|
|
12
15
|
MessageData,
|
|
@@ -30,12 +33,15 @@ from .utils import (
|
|
|
30
33
|
# Progress tracking
|
|
31
34
|
from .progress import ProgressTracker
|
|
32
35
|
|
|
33
|
-
__version__ = "0.0.
|
|
36
|
+
__version__ = "0.0.6"
|
|
34
37
|
|
|
35
38
|
__all__ = [
|
|
36
39
|
# Main class
|
|
37
40
|
"TgData",
|
|
38
|
-
|
|
41
|
+
|
|
42
|
+
# Errors
|
|
43
|
+
"GroupAccessError",
|
|
44
|
+
|
|
39
45
|
# Models
|
|
40
46
|
"MessageData",
|
|
41
47
|
"GroupInfo",
|
|
@@ -29,6 +29,12 @@ DEFAULT_FETCH_LIMIT = 750
|
|
|
29
29
|
MAX_FLOOD_RETRIES = 3
|
|
30
30
|
|
|
31
31
|
|
|
32
|
+
class GroupAccessError(Exception):
|
|
33
|
+
"""The account cannot see this group: it is not a member (or the id is
|
|
34
|
+
wrong). Raised only after a dialog sync ruled out the benign cause —
|
|
35
|
+
a fresh session whose entity cache simply hasn't met the id yet."""
|
|
36
|
+
|
|
37
|
+
|
|
32
38
|
class MessageEngine:
|
|
33
39
|
"""
|
|
34
40
|
Handles all message-related operations.
|
|
@@ -58,7 +64,8 @@ class MessageEngine:
|
|
|
58
64
|
rate_limit_strategy: str = 'wait',
|
|
59
65
|
allow_full_fetch: bool = False,
|
|
60
66
|
download_media_to: Optional[str] = None,
|
|
61
|
-
include_media: bool = False
|
|
67
|
+
include_media: bool = False,
|
|
68
|
+
heartbeat: Optional[Callable] = None) -> pd.DataFrame:
|
|
62
69
|
"""
|
|
63
70
|
Fetch messages from a group with various filters.
|
|
64
71
|
|
|
@@ -106,6 +113,19 @@ class MessageEngine:
|
|
|
106
113
|
)
|
|
107
114
|
progress_tracker.start()
|
|
108
115
|
|
|
116
|
+
# Liveness heartbeat: called with a phase label on EVERY sign of life —
|
|
117
|
+
# each message, each media chunk, each slice of a designed sleep. The
|
|
118
|
+
# caller's watchdog can then treat SILENCE as the hang signal with a
|
|
119
|
+
# small flat threshold, instead of guessing from total duration
|
|
120
|
+
# (which punishes big fetches). Exception-safe: a broken callback
|
|
121
|
+
# must never kill a fetch.
|
|
122
|
+
def _beat(phase: str) -> None:
|
|
123
|
+
if heartbeat is not None:
|
|
124
|
+
try:
|
|
125
|
+
heartbeat(phase)
|
|
126
|
+
except Exception: # noqa: BLE001
|
|
127
|
+
pass
|
|
128
|
+
|
|
109
129
|
messages_data = []
|
|
110
130
|
processed_count = 0
|
|
111
131
|
iterated_count = 0
|
|
@@ -134,7 +154,10 @@ class MessageEngine:
|
|
|
134
154
|
try:
|
|
135
155
|
async with self.connection_engine.session() as client:
|
|
136
156
|
# Get the entity
|
|
137
|
-
|
|
157
|
+
try:
|
|
158
|
+
channel = await client.get_entity(group_id)
|
|
159
|
+
except ValueError:
|
|
160
|
+
channel = await self._entity_after_dialog_sync(client, group_id, _beat)
|
|
138
161
|
logger.info(f"Fetching messages from: {channel.title}")
|
|
139
162
|
|
|
140
163
|
# Per-chat context stamped onto every row (provenance / deep links)
|
|
@@ -213,6 +236,7 @@ class MessageEngine:
|
|
|
213
236
|
logger.debug(f"Processing message {msg.id} (min_id={min_id})")
|
|
214
237
|
|
|
215
238
|
# Apply date filters (direction-aware: stop as soon as the
|
|
239
|
+
_beat("messages")
|
|
216
240
|
# iteration order guarantees no further in-window messages)
|
|
217
241
|
if start_date and msg.date < start_date:
|
|
218
242
|
if reverse:
|
|
@@ -235,7 +259,8 @@ class MessageEngine:
|
|
|
235
259
|
chat_id=chat_id,
|
|
236
260
|
link_base=link_base,
|
|
237
261
|
media_dir=download_media_to,
|
|
238
|
-
include_media=include_media
|
|
262
|
+
include_media=include_media,
|
|
263
|
+
_beat=_beat
|
|
239
264
|
)
|
|
240
265
|
|
|
241
266
|
if message_data:
|
|
@@ -265,7 +290,14 @@ class MessageEngine:
|
|
|
265
290
|
# Apply batch delay to avoid rate limits
|
|
266
291
|
if batch_delay > 0:
|
|
267
292
|
logger.info(f"Waiting {batch_delay}s between batches to avoid rate limits...")
|
|
268
|
-
|
|
293
|
+
# sliced so the designed pause BEATS —
|
|
294
|
+
# a paused fetch is alive, not hung
|
|
295
|
+
_left = float(batch_delay)
|
|
296
|
+
while _left > 0:
|
|
297
|
+
_beat("pause")
|
|
298
|
+
_step = min(10.0, _left)
|
|
299
|
+
await asyncio.sleep(_step)
|
|
300
|
+
_left -= _step
|
|
269
301
|
|
|
270
302
|
processed_count += 1
|
|
271
303
|
|
|
@@ -319,7 +351,17 @@ class MessageEngine:
|
|
|
319
351
|
f"FloodWait #{flood_attempts}/{MAX_FLOOD_RETRIES}: waiting {e.seconds}s, "
|
|
320
352
|
f"then resuming after message id {last_seen_id}"
|
|
321
353
|
)
|
|
322
|
-
|
|
354
|
+
if rate_limit_strategy == 'wait':
|
|
355
|
+
# sliced locally so the mandated wait BEATS with its
|
|
356
|
+
# remaining seconds — visible, alive, exact total.
|
|
357
|
+
_left = float(e.seconds)
|
|
358
|
+
while _left > 0:
|
|
359
|
+
_beat(f"flood-wait {int(_left)}s")
|
|
360
|
+
_step = min(10.0, _left)
|
|
361
|
+
await asyncio.sleep(_step)
|
|
362
|
+
_left -= _step
|
|
363
|
+
else:
|
|
364
|
+
await self.connection_engine.handle_rate_limit(e, client, strategy=rate_limit_strategy)
|
|
323
365
|
# loop continues → resume from last_seen_id with identical settings
|
|
324
366
|
|
|
325
367
|
except Exception as e:
|
|
@@ -364,6 +406,27 @@ class MessageEngine:
|
|
|
364
406
|
df['GroupedId'] = df['GroupedId'].astype('Int64')
|
|
365
407
|
return df
|
|
366
408
|
|
|
409
|
+
@staticmethod
|
|
410
|
+
async def _entity_after_dialog_sync(client, group_id, _beat=lambda phase: None):
|
|
411
|
+
"""get_entity said "never seen this id" — an ambiguous error hiding two
|
|
412
|
+
different situations. Telegram sessions can only resolve ids they have
|
|
413
|
+
encountered, so a FRESH session fails even for groups the account IS a
|
|
414
|
+
member of. Sync the chat list (fills the entity cache) and retry once:
|
|
415
|
+
a member resolves and the fetch proceeds as if nothing happened; a
|
|
416
|
+
non-member fails again, and only then is it a real access problem —
|
|
417
|
+
reported as one, instead of Telethon's cache riddle."""
|
|
418
|
+
logger.info(f"Entity {group_id} unknown to this session — syncing dialogs to check membership")
|
|
419
|
+
_beat("dialog-sync")
|
|
420
|
+
await client.get_dialogs()
|
|
421
|
+
_beat("dialog-sync done")
|
|
422
|
+
try:
|
|
423
|
+
return await client.get_entity(group_id)
|
|
424
|
+
except ValueError:
|
|
425
|
+
raise GroupAccessError(
|
|
426
|
+
f"account has no access to group {group_id} — the account is not "
|
|
427
|
+
f"a member (join the group with this account), or the id is wrong"
|
|
428
|
+
) from None
|
|
429
|
+
|
|
367
430
|
@staticmethod
|
|
368
431
|
def _link_base(chat) -> Optional[str]:
|
|
369
432
|
"""Base t.me URL for deep links to this chat's messages (None if not linkable)."""
|
|
@@ -418,7 +481,7 @@ class MessageEngine:
|
|
|
418
481
|
"""
|
|
419
482
|
return f"{chat_id}_{message_id}" if chat_id is not None else str(message_id)
|
|
420
483
|
|
|
421
|
-
async def _download_or_reuse(self, client, msg, media_dir: str, chat_id: Optional[int]):
|
|
484
|
+
async def _download_or_reuse(self, client, msg, media_dir: str, chat_id: Optional[int], _beat=lambda phase: None):
|
|
422
485
|
"""
|
|
423
486
|
Download a message's media to '<media_dir>/<chat_id>_<message_id>.<ext>',
|
|
424
487
|
REUSING the file if it already exists (idempotent re-scrape).
|
|
@@ -435,7 +498,10 @@ class MessageEngine:
|
|
|
435
498
|
logger.debug(f"Media for message {msg.id} already present ({stem}{ext}); reusing")
|
|
436
499
|
return target
|
|
437
500
|
# File doesn't exist yet: Telethon writes exactly to `target` (no rename).
|
|
438
|
-
|
|
501
|
+
# progress_callback fires per chunk — a 50MB video beats continuously.
|
|
502
|
+
return await client.download_media(
|
|
503
|
+
msg, file=target,
|
|
504
|
+
progress_callback=lambda cur, tot: _beat("media"))
|
|
439
505
|
|
|
440
506
|
async def _process_message(self,
|
|
441
507
|
msg,
|
|
@@ -444,7 +510,8 @@ class MessageEngine:
|
|
|
444
510
|
chat_id: Optional[int] = None,
|
|
445
511
|
link_base: Optional[str] = None,
|
|
446
512
|
media_dir: Optional[str] = None,
|
|
447
|
-
include_media: bool = False
|
|
513
|
+
include_media: bool = False,
|
|
514
|
+
_beat=lambda phase: None) -> Optional[MessageData]:
|
|
448
515
|
"""Process a single message"""
|
|
449
516
|
try:
|
|
450
517
|
sender = await msg.get_sender()
|
|
@@ -486,7 +553,7 @@ class MessageEngine:
|
|
|
486
553
|
if media_dir and msg.media is not None:
|
|
487
554
|
try:
|
|
488
555
|
message_data.media_path = await self._download_or_reuse(
|
|
489
|
-
client, msg, media_dir, chat_id
|
|
556
|
+
client, msg, media_dir, chat_id, _beat
|
|
490
557
|
)
|
|
491
558
|
except Exception as e:
|
|
492
559
|
logger.warning(f"Failed to download media for message {msg.id}: {e}")
|
|
@@ -136,7 +136,8 @@ class TgData:
|
|
|
136
136
|
rate_limit_strategy: str = 'wait',
|
|
137
137
|
allow_full_fetch: bool = False,
|
|
138
138
|
download_media_to: Optional[str] = None,
|
|
139
|
-
include_media: bool = False
|
|
139
|
+
include_media: bool = False,
|
|
140
|
+
heartbeat: Optional[Callable] = None) -> pd.DataFrame:
|
|
140
141
|
"""
|
|
141
142
|
Get messages from a group with various options.
|
|
142
143
|
|
|
@@ -204,7 +205,8 @@ class TgData:
|
|
|
204
205
|
rate_limit_strategy=rate_limit_strategy,
|
|
205
206
|
allow_full_fetch=allow_full_fetch,
|
|
206
207
|
download_media_to=download_media_to,
|
|
207
|
-
include_media=include_media
|
|
208
|
+
include_media=include_media,
|
|
209
|
+
heartbeat=heartbeat
|
|
208
210
|
)
|
|
209
211
|
|
|
210
212
|
if with_progress:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tgdata
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
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
|
|
@@ -192,6 +192,13 @@ messages = await tg.get_messages(
|
|
|
192
192
|
)
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
+
A numeric id resolves only if the session can actually see the group. On a
|
|
196
|
+
cache miss tgdata syncs the account's chat list once and retries — so fresh
|
|
197
|
+
sessions work for groups the account belongs to. If the group still can't be
|
|
198
|
+
found it raises `tgdata.GroupAccessError` (the account is not a member, or
|
|
199
|
+
the id is wrong) instead of Telethon's generic "Could not find the input
|
|
200
|
+
entity" ValueError.
|
|
201
|
+
|
|
195
202
|
### get_messages with start_date parameter
|
|
196
203
|
|
|
197
204
|
```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
|