-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram_processor.py
More file actions
1431 lines (1206 loc) · 53.5 KB
/
telegram_processor.py
File metadata and controls
1431 lines (1206 loc) · 53.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Telegram Channel Processor
This script processes Telegram channels by downloading content and processing
files based on their type (archives or text files). It handles extraction,
deduplication, and organization of the processed files.
"""
from __future__ import annotations
import argparse
import csv
import json
import logging
import logging.handlers
import os
import shutil
import subprocess
import sys
import time
import tempfile
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
from contextlib import contextmanager
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Dict, Any, Tuple
# Configure logging with rotation
def setup_logging(verbose: bool = False, settings: Optional[Dict[str, Any]] = None) -> logging.Logger:
"""Setup logging with file rotation and console output."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG if verbose else logging.INFO)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG if verbose else logging.INFO)
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
# File handler with rotation - use settings if available
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
# Get logging settings
if settings:
max_size = settings.get("logging", {}).get("max_file_size_mb", 10) * 1024 * 1024
backup_count = settings.get("logging", {}).get("backup_count", 5)
else:
max_size = 10 * 1024 * 1024 # 10MB default
backup_count = 5
file_handler = logging.handlers.RotatingFileHandler(
log_dir / "telegram_processor.log",
maxBytes=max_size,
backupCount=backup_count
)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(name)s - %(funcName)s:%(lineno)d - %(message)s'
)
file_handler.setFormatter(file_formatter)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return logger
# Initialize logger (will be reconfigured in main)
logger = logging.getLogger(__name__)
# Check if tqdm is available
try:
from tqdm import tqdm
TQDM_AVAILABLE = True
except ImportError:
TQDM_AVAILABLE = False
# Don't log warning here as logger may not be configured yet
class ProcessingError(Exception):
"""Base exception for processing errors."""
pass
class ChannelConfig:
"""Configuration for a Telegram channel."""
def __init__(self, name: str, channel: str, passwords: Optional[List[str]] = None) -> None:
"""
Initialize channel configuration.
Args:
name: Display name for the channel
channel: Channel identifier (ID or username)
passwords: Optional list of passwords for archive extraction
Raises:
ValueError: If name or channel is empty
"""
if not name or not name.strip():
raise ValueError("Channel name cannot be empty")
if not channel or not channel.strip():
raise ValueError("Channel identifier cannot be empty")
self.name: str = name.strip()
self.channel: str = channel.strip()
self.passwords: List[str] = [p.strip() for p in passwords if p and p.strip()] if passwords else []
self.working_dir: Optional[Path] = None
@property
def has_passwords(self) -> bool:
"""Check if channel has any valid passwords."""
return bool(self.passwords)
class Settings:
"""Configuration settings loaded from JSON file."""
DEFAULT_SETTINGS_FILE = "settings.json"
def __init__(self, settings_file: Optional[str | Path] = None) -> None:
"""
Initialize settings from JSON file.
Args:
settings_file: Optional path to settings file
"""
self.settings_file = Path(settings_file or self.DEFAULT_SETTINGS_FILE)
self.settings: Dict[str, Any] = self._load_settings()
def _load_settings(self) -> Dict[str, Any]:
"""
Load settings from JSON file.
Returns:
Dict containing settings
Raises:
ProcessingError: If settings file is invalid
"""
try:
with open(self.settings_file, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
logger.warning(
"Settings file not found: %s, using defaults", self.settings_file
)
return self._default_settings()
except json.JSONDecodeError as e:
raise ProcessingError(f"Invalid JSON in settings file: {e}")
except Exception as e:
raise ProcessingError(f"Failed to load settings: {e}")
def _default_settings(self) -> Dict[str, Any]:
"""Return default settings."""
return {
"stealer_log_processor": {"path": "./stealer-log-processor/main.py"},
"tdl": {
"max_parallel_downloads": 1,
"reconnect_timeout": 0,
"download_timeout": 7200,
"export_channel_threads": 4,
"bandwidth_limit": 0, # 0 means unlimited
"chunk_size": 128, # in KB
"excluded_extensions": [
"jpg", "gif", "png", "webp", "webm", "mp4"
],
"included_extensions": [
"zip", "rar", "7z", "txt", "csv"
],
"max_retries": 3,
"retry_delay": 5,
},
"sort": {
"memory_percent": 30,
"max_parallel": 16,
"temp_dir": "/tmp",
"chunk_size": 1000000, # lines per chunk for external sort
},
"archive": {
"extract_patterns": [
"*.txt",
"*.csv",
"*pass*",
"*auto*"
],
"supported_extensions": [".zip", ".rar", ".7z"],
"extract_timeout": 300, # 5 minutes default
"max_retries": 2,
"retry_delay": 2,
},
"processing": {
"max_workers": None, # None means use CPU count
"checkpoint_interval": 100, # Save progress every N files
"min_file_size_bytes": 0,
"min_result_file_size_bytes": 1,
},
"logging": {
"max_file_size_mb": 10,
"backup_count": 5,
"temp_cleanup_patterns": [
"tdl-export*.json",
"*.temp",
"sort*"
]
},
"subprocess": {
"default_timeout": 300,
"terminate_timeout": 5
}
}
def get(self, *keys, default=None):
"""
Get a nested setting value.
Args:
*keys: Sequence of keys to access nested settings
default: Default value if key doesn't exist
Returns:
Setting value or default
"""
try:
current = self.settings
for key in keys:
current = current[key]
return current
except (KeyError, TypeError):
return default
def prepare_extensions(self, extensions: List[str]) -> List[str]:
"""
Prepare extensions by ensuring both lower and upper case variants are included.
Args:
extensions: List of extensions (without leading dot)
Returns:
List with both lowercase and uppercase variants
"""
result = set()
for ext in extensions:
# Add lowercase version
ext = ext.lower().strip()
if ext:
# Remove leading dots if present
if ext.startswith('.'):
ext = ext[1:]
result.add(ext)
# Add uppercase version if different
upper_ext = ext.upper()
if upper_ext != ext:
result.add(upper_ext)
return list(result)
class RetryMixin:
"""Mixin class for retry functionality."""
def retry_operation(self, func, *args, max_retries: int = 3, retry_delay: int = 5, **kwargs):
"""
Retry an operation with exponential backoff.
Args:
func: Function to retry
*args: Positional arguments for func
max_retries: Maximum number of retries
retry_delay: Initial delay between retries in seconds
**kwargs: Keyword arguments for func
Returns:
Result of successful operation
Raises:
Last exception if all retries fail
"""
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except Exception as e:
if attempt == max_retries - 1:
raise
wait_time = retry_delay * (2 ** attempt)
logger.warning(
"Attempt %d/%d failed: %s. Retrying in %d seconds...",
attempt + 1, max_retries, str(e), wait_time
)
time.sleep(wait_time)
class TelegramProcessor(RetryMixin):
"""Main processor for Telegram channels."""
CHECKPOINT_FILE = ".processing_checkpoint.json"
EXPORT_FILE = "tdl-export.json"
DEDUPLICATION_FILE = "results.txt"
def __init__(
self,
input_file: str | Path,
start_date: str,
end_date: str,
output_dir: Optional[str | Path] = None,
download_dir: Optional[str | Path] = None,
settings_file: Optional[str | Path] = None,
verbose: bool = False,
process_only: bool = False,
auto_clean: bool = False,
) -> None:
"""
Initialize the processor.
Args:
input_file: Path to CSV file with channel configurations
start_date: Start date in epoch format
end_date: End date in epoch format
output_dir: Optional output directory for processed files
download_dir: Optional directory for downloaded files
settings_file: Optional path to settings file
verbose: Whether to show detailed output
process_only: Whether to skip download phase and only process existing files
auto_clean: Whether to automatically clean up without prompting
Raises:
ProcessingError: If input file doesn't exist or output directory can't be created
"""
self.input_file = Path(input_file)
if not self.input_file.exists():
raise ProcessingError(f"Input file not found: {input_file}")
self.start_date = start_date
self.end_date = end_date
self.process_only = process_only
self.auto_clean = auto_clean
# Convert epoch start date to month-year format for file naming
start_datetime = datetime.fromtimestamp(float(start_date))
self.date_suffix = start_datetime.strftime("%m-%Y")
# Setup directories
self.output_dir = Path(output_dir) if output_dir else Path.cwd() / "output"
self.downloads_dir = Path(download_dir) if download_dir else Path.cwd() / "downloads"
try:
self.output_dir.mkdir(parents=True, exist_ok=True)
self.downloads_dir.mkdir(parents=True, exist_ok=True)
except Exception as e:
raise ProcessingError(f"Failed to create required directories: {e}")
self.channels: List[ChannelConfig] = []
self.settings = Settings(settings_file)
self.verbose = verbose
# Track processing state
self.processed_channels: Dict[str, bool] = {}
self.checkpoint_file = self.downloads_dir / self.CHECKPOINT_FILE
def check_dependencies(self) -> None:
"""
Check if required external tools are available.
Raises:
ProcessingError: If any required tool is missing
"""
required_tools = {
"tdl": "Telegram downloader (tdl) - Install from: https://github.com/iyear/tdl",
"7z": "7-Zip archiver - Install: apt-get install p7zip-full",
"rdfind": "Duplicate file finder - Install: apt-get install rdfind",
}
missing_tools = []
for tool, description in required_tools.items():
if not shutil.which(tool):
missing_tools.append(f"{tool}: {description}")
if missing_tools:
error_msg = "Missing required tools:\n" + "\n".join(missing_tools)
raise ProcessingError(error_msg)
def load_checkpoint(self) -> None:
"""Load processing checkpoint if it exists."""
if self.checkpoint_file.exists():
try:
with open(self.checkpoint_file, "r") as f:
self.processed_channels = json.load(f)
logger.info("Loaded checkpoint with %d processed channels", len(self.processed_channels))
except Exception as e:
logger.warning("Failed to load checkpoint: %s", e)
self.processed_channels = {}
def save_checkpoint(self) -> None:
"""Save processing checkpoint."""
try:
with open(self.checkpoint_file, "w") as f:
json.dump(self.processed_channels, f)
except Exception as e:
logger.warning("Failed to save checkpoint: %s", e)
def load_channels(self) -> None:
"""
Load channel configurations from CSV file.
Raises:
ProcessingError: If CSV file is invalid or missing required columns
"""
try:
with open(self.input_file, "r", encoding="utf-8") as f:
reader = csv.reader(f)
try:
header = next(reader)
except StopIteration:
raise ProcessingError("CSV file is empty")
# Basic header validation
if len(header) < 2 or header[0].strip().lower() != 'name' or header[1].strip().lower() != 'channel':
raise ProcessingError("CSV must start with 'name' and 'channel' columns")
for row in reader:
if not row or not row[0] or not row[1]:
continue # Skip empty rows or rows missing name/channel
name = row[0]
channel_id = row[1]
passwords = row[2:] if len(row) > 2 else []
channel = ChannelConfig(
name=name,
channel=channel_id,
passwords=passwords,
)
self.channels.append(channel)
if not self.channels:
raise ProcessingError("No channels found in CSV file")
except csv.Error as e:
raise ProcessingError(f"Failed to parse CSV file: {e}")
def cleanup_temp_files(self) -> None:
"""Clean up any temporary files."""
try:
# Get cleanup patterns from settings with fallback
cleanup_patterns = self.settings.get("logging", "temp_cleanup_patterns", default=[
"tdl-export*.json", "*.temp", "sort*"
])
# Ensure we have a list to iterate over
if cleanup_patterns is None:
cleanup_patterns = ["tdl-export*.json", "*.temp", "sort*"]
# Clean up files in current directory
for pattern in cleanup_patterns:
if pattern != "sort*": # Handle sort files separately
for temp_file in Path().glob(pattern):
temp_file.unlink(missing_ok=True)
# Clean up sort temp files in temp directory
temp_dir = Path(self.settings.get("sort", "temp_dir", default="/tmp"))
for sort_temp in temp_dir.glob("sort*"):
if sort_temp.is_file():
sort_temp.unlink(missing_ok=True)
except Exception as e:
logger.warning("Failed to clean up temporary files: %s", e)
@contextmanager
def managed_process(self, *args, **kwargs):
"""Context manager for subprocess with proper cleanup."""
process = subprocess.Popen(*args, **kwargs)
try:
yield process
finally:
if process.poll() is None:
terminate_timeout = self.settings.get("subprocess", "terminate_timeout", default=5)
process.terminate()
try:
process.wait(timeout=terminate_timeout)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
def safe_move(self, src: Path, dst: Path) -> None:
"""
Safely move a file with proper error handling.
Args:
src: Source path
dst: Destination path
Raises:
ProcessingError: If move operation fails
"""
try:
# Ensure destination directory exists
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.move(str(src), str(dst))
except Exception as e:
raise ProcessingError(f"Failed to move {src} to {dst}: {e}")
def parse_export_file(self, channel: ChannelConfig) -> List[str]:
"""
Parse tdl-export.json file to get list of expected files.
Args:
channel: Channel configuration
Returns:
List of filenames to be downloaded
"""
export_file = self.downloads_dir / channel.name / self.EXPORT_FILE
try:
with open(export_file, "r", encoding="utf-8") as f:
export_data = json.load(f)
# Extract file information from messages
files = []
for message in export_data.get("messages", []):
if message.get("type") == "message" and "file" in message:
files.append(message["file"])
return files
except (FileNotFoundError, json.JSONDecodeError, KeyError) as e:
logger.error(
"Failed to parse export file for channel %s: %s", channel.name, str(e)
)
return []
def download_channel_with_retry(self, channel: ChannelConfig) -> bool:
"""
Download content from a Telegram channel with retry logic.
Args:
channel: Channel configuration
Returns:
bool: True if any files were downloaded, False otherwise
"""
max_retries = self.settings.get("tdl", "max_retries", default=3)
retry_delay = self.settings.get("tdl", "retry_delay", default=5)
return self.retry_operation(
self._download_channel_impl,
channel,
max_retries=max_retries,
retry_delay=retry_delay
)
def _download_channel_impl(self, channel: ChannelConfig) -> bool:
"""
Implementation of channel download logic.
Args:
channel: Channel configuration
Returns:
bool: True if any files were downloaded, False otherwise
"""
logger.info("Downloading channel: %s", channel.name)
# Setup channel directory path
channel_dir = self.downloads_dir / channel.name
export_file = channel_dir / self.EXPORT_FILE
try:
# Ensure channel directory exists for export file
channel_dir.mkdir(parents=True, exist_ok=True)
# Create export command for file downloads
export_cmd = [
"tdl",
"chat",
"export",
"-c",
channel.channel,
"-i",
f"{self.start_date},{self.end_date}",
"-o",
str(export_file),
"-t",
str(self.settings.get("tdl", "export_channel_threads", default=4))
]
# Run export
if self.verbose:
logger.info("Running command: %s", " ".join(export_cmd))
# Capture output to prevent interference with tqdm
export_result = subprocess.run(export_cmd, check=True, capture_output=True, text=True, errors='replace')
if self.verbose and export_result.stderr:
# tdl sends progress and info to stderr
logger.info("tdl export output for %s:\n%s", channel.name, export_result.stderr.strip())
# Parse export file to get expected files
expected_files = self.parse_export_file(channel)
if not expected_files:
logger.info(
"No files found to download for channel %s in the specified date range",
channel.name,
)
return False
logger.info(
"Found %d files to download for channel %s",
len(expected_files),
channel.name,
)
# Prepare the download command with optimized parameters
dl_cmd = [
"tdl",
"dl",
"-l",
str(self.settings.get("tdl", "max_parallel_downloads", default=4)),
"-f",
str(export_file),
"--reconnect-timeout",
str(self.settings.get("tdl", "reconnect_timeout", default=0)),
"--skip-same",
"-d",
str(channel_dir),
"--continue"
]
# Add bandwidth limit if specified and not zero
bandwidth_limit = self.settings.get("tdl", "bandwidth_limit", default=0)
if bandwidth_limit > 0:
dl_cmd.extend(["--limit", str(bandwidth_limit)])
# Process extensions to exclude
excluded_extensions = self.settings.get("tdl", "excluded_extensions", default=[])
if excluded_extensions:
# Process extensions to handle case variants
excluded_extensions = self.settings.prepare_extensions(excluded_extensions)
dl_cmd.extend(["-e", ",".join(excluded_extensions)])
# Run download with proper error handling
if self.verbose:
logger.info("Running command: %s", " ".join(dl_cmd))
# Show real-time progress by not capturing output
logger.info("Starting download of %d files for channel %s...", len(expected_files), channel.name)
# Track download progress by monitoring the directory
initial_files = set(
f.name for f in channel_dir.iterdir()
if f.is_file() and not f.name.endswith(".tmp") and f.name != "tdl-export.json"
)
try:
# Run the download process without capturing output to show real-time progress
process = subprocess.Popen(dl_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1, universal_newlines=True)
# Stream output line by line to show progress
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
# Print tdl's progress output directly
print(output.strip())
# Check for specific progress indicators from tdl
if "downloading" in output.lower() or "progress" in output.lower():
# Count files downloaded so far
current_files = set(
f.name for f in channel_dir.iterdir()
if f.is_file() and not f.name.endswith(".tmp") and f.name != "tdl-export.json"
)
new_files = current_files - initial_files
if new_files:
logger.info("Downloaded %d/%d files for channel %s",
len(new_files), len(expected_files), channel.name)
# Wait for process to complete and check return code
return_code = process.wait()
if return_code != 0:
raise subprocess.CalledProcessError(return_code, dl_cmd)
except subprocess.CalledProcessError as e:
logger.error("Download failed for %s with exit code %d", channel.name, e.returncode)
raise
# Verify downloads by checking the directory
downloaded_files = [
f for f in channel_dir.iterdir()
if f.is_file() and not f.name.endswith(".tmp") and f.name != "tdl-export.json"
]
if not downloaded_files:
logger.warning(
"Download command completed but no files were found for channel %s",
channel.name
)
return False
logger.info(
"Successfully downloaded %d files for channel %s",
len(downloaded_files),
channel.name
)
return True
except subprocess.CalledProcessError as e:
error_msg = f"Failed to process channel {channel.name}"
if hasattr(e, 'stderr') and e.stderr:
error_msg += f": {e.stderr}"
logger.error(error_msg)
raise
except Exception as e:
logger.error("Unexpected error downloading channel %s: %s", channel.name, str(e))
raise
finally:
# Clean up export file if it exists outside channel directory
root_export_file = Path(self.EXPORT_FILE)
if root_export_file.exists():
root_export_file.unlink()
def download_channel_wrapper(self, channel: ChannelConfig) -> Tuple[ChannelConfig, bool, Optional[str]]:
"""
Wrapper for download_channel to use with concurrent.futures.
Args:
channel: Channel configuration
Returns:
Tuple containing (channel, success_status, error_message)
"""
try:
# Check if already processed
if self.processed_channels.get(channel.name, False):
logger.info("Channel %s already processed, skipping", channel.name)
channel.working_dir = self.downloads_dir / channel.name
return channel, True, None
success = self.download_channel_with_retry(channel)
if success:
channel.working_dir = self.downloads_dir / channel.name
if not channel.working_dir.exists() or not any(channel.working_dir.iterdir()):
return channel, False, f"No files found in {channel.working_dir}"
# Mark as processed
self.processed_channels[channel.name] = True
self.save_checkpoint()
return channel, True, None
else:
return channel, False, "No files downloaded"
except Exception as e:
return channel, False, str(e)
def get_archive_files(self, directory: Path) -> List[Path]:
"""
Get list of archive files in directory.
Args:
directory: Directory to search
Returns:
List of archive file paths
"""
if not directory.exists():
return []
# Get supported archive extensions from settings
supported_extensions = self.settings.get("archive", "supported_extensions", default=[".zip", ".rar", ".7z"])
archive_extensions = {ext.lower() for ext in supported_extensions}
return [
f
for f in directory.iterdir()
if f.is_file() and f.suffix.lower() in archive_extensions
]
def extract_single_archive(
self, archive_path: Path, password: Optional[str] = None
) -> bool:
"""
Extract a single archive file with retry logic.
Args:
archive_path: Path to archive file
password: Optional password for extraction
Returns:
bool: True if extraction was successful
"""
max_retries = self.settings.get("archive", "max_retries", default=2)
for attempt in range(max_retries):
try:
if self._extract_archive_impl(archive_path, password):
return True
if attempt < max_retries - 1:
if self.verbose:
logger.warning(
"Extraction failed for %s, retrying (%d/%d)...",
archive_path.name, attempt + 1, max_retries
)
except Exception as e:
if attempt == max_retries - 1:
logger.error(
"Failed to extract %s after %d attempts: %s",
archive_path.name, max_retries, str(e)
)
return False
return False
def _extract_archive_impl(
self, archive_path: Path, password: Optional[str] = None
) -> bool:
"""
Implementation of archive extraction logic.
Args:
archive_path: Path to archive file
password: Optional password for extraction
Returns:
bool: True if extraction was successful
"""
patterns = self.settings.get("archive", "extract_patterns", default=["*.txt", "*.csv"])
try:
# Base command
# -y: assume yes to all queries
# -aoa: overwrite all existing files
# -bd: disable progress indicator
base_cmd = ["7z", "x", str(archive_path.name), "-aoa", "-y", "-bd"]
# Add patterns
for pattern in patterns:
base_cmd.extend(["-ir!" + pattern])
# Add password if provided
if password:
base_cmd.extend(["-p" + password])
if self.verbose:
logger.info("Running command: %s", " ".join(base_cmd))
logger.info("Working directory: %s", archive_path.parent)
# Set timeout
timeout = self.settings.get("archive", "extract_timeout", default=300)
env = os.environ.copy()
env["LANG"] = "C.UTF-8"
with self.managed_process(
base_cmd,
cwd=archive_path.parent,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env,
encoding='utf-8',
errors='replace'
) as process:
try:
_, stderr = process.communicate(timeout=timeout)
# Check for specific errors
if stderr and ("ERROR: Cannot convert" in stderr or "Wrong password" in stderr):
if self.verbose:
logger.error(
"7z extraction failed for %s:\n%s",
archive_path.name,
stderr[:500]
)
return False
if process.returncode != 0:
if self.verbose:
logger.error(
"7z extraction failed for %s with exit code %d",
archive_path.name,
process.returncode
)
return False
return True
except subprocess.TimeoutExpired:
logger.error(
"Extraction timed out after %d seconds for %s",
timeout,
archive_path.name,
)
return False
except Exception as e:
logger.error(
"Failed to run extraction for %s: %s", archive_path.name, str(e)
)
return False
def extract_archives(self, channel: ChannelConfig) -> bool:
"""
Extract archives from channel directory. Tries all provided passwords,
and if all fail, attempts extraction without a password.
Uses parallel processing for faster extraction.
Args:
channel: Channel configuration
Returns:
bool: True if any archives were successfully extracted
"""
if not channel.working_dir or not channel.working_dir.exists():
return False
archive_files = self.get_archive_files(channel.working_dir)
if not archive_files:
return True # No archives is considered success
total_count = len(archive_files)
max_workers = self.settings.get("archive", "max_parallel_extractions", default=4)
# Limit workers to the number of archives if we have fewer archives
actual_workers = min(max_workers, total_count)
if actual_workers > 1:
logger.info(
"Starting parallel extraction with %d workers for %d archives in channel %s",
actual_workers, total_count, channel.name
)
def extract_archive_with_passwords(archive_path: Path) -> Tuple[Path, bool, Optional[str]]:
"""
Extract a single archive trying all passwords.
Returns: (archive_path, success, password_used)
"""
if self.verbose:
logger.info("Processing archive %s", archive_path.name)
# Try extraction with passwords if provided
if channel.has_passwords:
for password in channel.passwords:
if self.extract_single_archive(archive_path, password):
return (archive_path, True, password)
# Try without password
if self.extract_single_archive(archive_path):
return (archive_path, True, None)
return (archive_path, False, None)
# Process archives in parallel using ThreadPoolExecutor
success_count = 0
failed_archives = []
with ThreadPoolExecutor(max_workers=actual_workers) as executor:
# Submit all extraction tasks
futures = {executor.submit(extract_archive_with_passwords, archive): archive
for archive in archive_files}
# Process results as they complete
for future in as_completed(futures):
archive_path, success, password_used = future.result()
if success:
success_count += 1
if self.verbose:
if password_used:
logger.info("Successfully extracted %s with password", archive_path.name)
else:
logger.info("Successfully extracted %s without password", archive_path.name)
else:
failed_archives.append(archive_path.name)
if self.verbose:
logger.warning("Failed to extract %s with any method", archive_path.name)
# Report results
if success_count == 0 and total_count > 0:
logger.error("Failed to extract any archives for channel %s", channel.name)
return False
if success_count < total_count:
logger.warning(
"Extracted %d out of %d archives for channel %s (failed: %s)",
success_count,
total_count,
channel.name,
", ".join(failed_archives[:5]) + ("..." if len(failed_archives) > 5 else "")
)
else:
logger.info(
"Successfully extracted all %d archives for channel %s",
total_count,
channel.name,
)
return True
def has_archives(self, directory: Path) -> bool:
"""
Check if directory contains archive files.
Args:
directory: Directory to check