-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
209 lines (165 loc) · 6.44 KB
/
Copy pathmain.py
File metadata and controls
209 lines (165 loc) · 6.44 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
"""
Audio Download Manager - Main Entry Point
Downloads shows for a radio station
Usage:
python main.py - Run GUI
python main.py --download-all - Run downloads in CLI mode (no GUI)
python main.py --source "Melinda Myers" - Download from specific source
"""
import os
import sys
import logging
import argparse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def setup_logging(log_to_file=True):
"""Configure logging for the application"""
logger = logging.getLogger("audio_downloader")
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
if log_to_file:
file_handler = logging.FileHandler("audio_downloader.log")
file_handler.setLevel(logging.DEBUG)
for module in ['sources', 'sources.base', 'browser_manager', 'download_utils']:
mod_logger = logging.getLogger(module)
mod_logger.setLevel(logging.DEBUG)
mod_logger.addHandler(file_handler)
file_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_format)
logger.addHandler(file_handler)
console_format = logging.Formatter('%(levelname)s - %(message)s')
console_handler.setFormatter(console_format)
logger.addHandler(console_handler)
return logger
def _touch_output_dir(config):
"""Update the output directory's mtime so it appears at the top in Explorer"""
output_dir = config.get_output_base_dir()
try:
os.utime(output_dir)
except Exception:
pass
def run_cli_downloads():
"""Run downloads in CLI mode without GUI"""
from config import ConfigManager, DOWNLOAD_SOURCES
from browser_manager import BrowserManager
from sources import create_downloader
logger = logging.getLogger("audio_downloader")
logger.info("=" * 50)
logger.info("AUDIO DOWNLOAD MANAGER - CLI MODE")
logger.info("=" * 50)
config = ConfigManager()
logger.info(f"Output directory: {config.get_output_base_dir()}")
logger.info(f"Browser download dir: {config.get_browser_download_dir()}")
logger.info("")
config.ensure_folders()
config.clear_browser_download_dir()
logger.info("Cleared browser download directory")
logger.info("")
results = {}
browser_manager = BrowserManager(config)
try:
for source_name in DOWNLOAD_SOURCES.keys():
logger.info("")
logger.info(f"--- Downloading from: {source_name} ---")
config.clear_browser_download_dir()
downloader = create_downloader(source_name, browser_manager, config)
try:
success = downloader.download()
results[source_name] = success
if success:
logger.info(f"✓ {source_name}: SUCCESS")
else:
logger.error(f"✗ {source_name}: FAILED")
except Exception as e:
logger.error(f"✗ {source_name}: ERROR - {e}")
results[source_name] = False
finally:
browser_manager.close_browser()
logger.info("")
logger.info("=" * 50)
logger.info("DOWNLOAD SUMMARY")
logger.info("=" * 50)
success_count = sum(1 for v in results.values() if v)
total_count = len(results)
for source_name, success in results.items():
status = "✓ SUCCESS" if success else "✗ FAILED"
logger.info(f" {source_name}: {status}")
logger.info("")
logger.info(f"Total: {success_count}/{total_count} successful")
_touch_output_dir(config)
return all(results.values())
def run_single_source(source_name):
"""Download from a single source"""
from config import ConfigManager, DOWNLOAD_SOURCES
from browser_manager import BrowserManager
from sources import create_downloader
logger = logging.getLogger("audio_downloader")
if source_name not in DOWNLOAD_SOURCES:
logger.error(f"Unknown source: {source_name}")
logger.info(f"Available sources: {list(DOWNLOAD_SOURCES.keys())}")
return False
logger.info("=" * 50)
logger.info(f"DOWNLOADING: {source_name}")
logger.info("=" * 50)
config = ConfigManager()
browser_manager = BrowserManager(config)
downloader = create_downloader(source_name, browser_manager, config)
try:
success = downloader.download()
if success:
logger.info(f"✓ {source_name}: SUCCESS")
else:
logger.error(f"✗ {source_name}: FAILED")
return success
except Exception as e:
logger.error(f"✗ {source_name}: ERROR - {e}")
return False
finally:
browser_manager.close_browser()
_touch_output_dir(config)
def main():
"""Main entry point for the application"""
parser = argparse.ArgumentParser(
description="Audio Download Manager",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py Run GUI mode
python main.py --download-all Download from all sources
python main.py --source "Melinda Myers" Download from specific source
"""
)
parser.add_argument(
'--download-all',
action='store_true',
help='Run downloads for all sources in CLI mode (no GUI)'
)
parser.add_argument(
'--source',
type=str,
help='Download from a specific source (e.g., "Melinda Myers")'
)
args = parser.parse_args()
if args.download_all:
setup_logging()
logger = logging.getLogger("audio_downloader")
success = run_cli_downloads()
sys.exit(0 if success else 1)
elif args.source:
setup_logging()
success = run_single_source(args.source)
sys.exit(0 if success else 1)
else:
setup_logging(log_to_file=True)
logger = logging.getLogger("audio_downloader")
try:
from gui import AudioDownloaderGUI
logger.info("Starting Audio Download Manager (GUI Mode)")
app = AudioDownloaderGUI()
app.run()
except Exception as e:
logger.error(f"Application error: {e}", exc_info=True)
print(f"Fatal error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()