-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram_processor.py
More file actions
137 lines (120 loc) · 4.05 KB
/
telegram_processor.py
File metadata and controls
137 lines (120 loc) · 4.05 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
#!/usr/bin/env python3
"""CLI entrypoint for the Telegram channel processor."""
from __future__ import annotations
import argparse
import json
import sys
from datetime import datetime
from pathlib import Path
from telegram_processor_lib import (
ChannelChecker,
ChannelConfig,
ProcessingError,
Settings,
SystemOps,
TQDM_AVAILABLE,
TelegramProcessor,
setup_logging,
)
__all__ = [
"ChannelChecker",
"ChannelConfig",
"ProcessingError",
"Settings",
"SystemOps",
"TQDM_AVAILABLE",
"TelegramProcessor",
"main",
"setup_logging",
]
def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Telegram Channel Processor",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--input", required=True, help="Input CSV file with channel configurations")
parser.add_argument("--start", help="Start date (DD-MM-YYYY)")
parser.add_argument("--end", help="End date (DD-MM-YYYY)")
parser.add_argument("--output-dir", help="Output directory for processed files")
parser.add_argument("--download-dir", help="Directory for downloaded files")
parser.add_argument("--settings", help="Path to settings JSON file")
parser.add_argument(
"--verbose",
action="store_true",
help="Show detailed output including tdl commands",
)
parser.add_argument(
"--process-only",
action="store_true",
help="Skip download phase and only process existing files",
)
parser.add_argument(
"--auto-clean",
action="store_true",
help="Automatically clean up after processing without prompting",
)
parser.add_argument(
"--check-channels",
action="store_true",
help="Validate configured channels and exit without downloading or processing",
)
parser.add_argument(
"--comment-missing",
action="store_true",
help="When used with --check-channels, comment out inactive rows in the input CSV",
)
args = parser.parse_args()
try:
settings_file = Path(args.settings or "settings.json")
if settings_file.exists():
with open(settings_file, "r", encoding="utf-8") as handle:
settings_data = json.load(handle)
else:
settings_data = None
except Exception:
settings_data = None
logger = setup_logging(args.verbose, settings_data)
if not TQDM_AVAILABLE:
logger.info("tqdm not available. Install it for progress bars: pip install tqdm")
try:
if args.check_channels:
checker = ChannelChecker(input_file=args.input)
checker.run(comment_missing=args.comment_missing)
return
if not args.start or not args.end:
logger.error("--start and --end are required unless --check-channels is used")
sys.exit(1)
try:
start_epoch = datetime.strptime(args.start, "%d-%m-%Y").timestamp()
end_epoch = datetime.strptime(args.end, "%d-%m-%Y").timestamp()
except ValueError:
logger.error("Invalid date format. Use DD-MM-YYYY")
sys.exit(1)
processor = TelegramProcessor(
input_file=args.input,
start_date=str(int(start_epoch)),
end_date=str(int(end_epoch)),
output_dir=args.output_dir,
download_dir=args.download_dir,
settings_file=args.settings,
verbose=args.verbose,
process_only=args.process_only,
auto_clean=args.auto_clean,
)
processor.check_dependencies()
processor.process()
except ProcessingError as exc:
logger.error(str(exc))
sys.exit(1)
except KeyboardInterrupt:
logger.info("Process interrupted by user")
sys.exit(130)
except Exception:
logger.exception("Unexpected error occurred")
sys.exit(1)
finally:
if "processor" in locals():
processor.cleanup_temp_files()
if __name__ == "__main__":
main()