-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
128 lines (103 loc) · 4.96 KB
/
main.py
File metadata and controls
128 lines (103 loc) · 4.96 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
#!/usr/bin/env python
# main.py - Entry point for Headless Bing Search Automator
import argparse
import logging
import sys
import os
from config import Config
from utils.logger import setup_logging, get_topics_logger
from utils.network import is_connected, wait_for_connection
from gui_module import GUI
from browser_controller import BrowserController
from data_manager import DataManager
from rewards_watcher import RewardsWatcher
from daily_topics import DailyTopics
from utils.runtime_topic_generator import RuntimeTopicGenerator
from version import __version__
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
class Application:
"""Main application class to orchestrate the components."""
def __init__(self, config, topics_logger=None):
self.config = config
self.topics_logger = topics_logger
self.logger = logging.getLogger(__name__)
self.logger.info("Initializing application components...")
self.data_manager = DataManager(self.config)
# Select and instantiate topic provider based on configuration
topic_provider = self._create_topic_provider(config)
self.browser_controller = BrowserController(
self.config, self.data_manager, topic_provider
)
self.gui = GUI(self.config, self.data_manager, self.browser_controller)
self.rewards_watcher = RewardsWatcher(self.config, self.data_manager, self.gui)
self.browser_controller.gui = self.gui
self.browser_controller.metrics_collector = self.gui.metrics_collector
self.gui.rewards_watcher = self.rewards_watcher
self.logger.info("Application components initialized successfully.")
def _create_topic_provider(self, config):
"""Create and return appropriate topic provider based on config.
Args:
config: Config object
Returns:
TopicProvider: Either DailyTopics or RuntimeTopicGenerator
"""
topic_generator_type = getattr(config, 'topic_generator_type', 'runtime').lower()
if topic_generator_type == 'runtime':
provider = RuntimeTopicGenerator(
config={
'cache_duplicates': True,
'max_generation_attempts': 10
},
topics_logger=self.topics_logger
)
self.logger.info("Using RuntimeTopicGenerator for dynamic topic generation")
else:
provider = DailyTopics()
self.logger.info("Using DailyTopics for daily-based topic selection")
return provider
def run(self):
"""Start the application."""
self.logger.info("Starting Rewards Watcher...")
self.rewards_watcher.start()
try:
self.logger.info("Starting GUI...")
self.gui.start()
except KeyboardInterrupt:
self.logger.info("Application interrupted by user.")
finally:
self.logger.info("Stopping Rewards Watcher...")
try:
self.rewards_watcher.stop()
except Exception as e:
self.logger.error(f"Error stopping rewards watcher: {e}")
self.logger.info("Application has been shut down gracefully.")
def main():
"""Entry point of the application."""
parser = argparse.ArgumentParser(description="Bing Search Automator (Headless with Playwright)")
parser.add_argument('--config', default='config.yaml', help='Path to the configuration file (default: config.yaml)')
parser.add_argument('--profile', default=None, help='Configuration profile to use (e.g., stealth_mode, speed_mode, balanced_mode)')
args = parser.parse_args()
# Load configuration with optional profile
config = Config.from_yaml(args.config, profile=args.profile)
# Set up logging
setup_logging(log_level=config.log_level, log_file=config.log_file_path, log_format=config.log_format)
# Display version
logger = logging.getLogger(__name__)
logger.info(f"Bing Search Automator v{__version__} starting...")
# Set up topics logger (for runtime topic generation mode)
topics_log_file = None
if getattr(config, 'topic_generator_type', 'runtime').lower() == 'runtime':
# Create topics.log in same directory as app.log
log_dir = os.path.dirname(config.log_file_path)
topics_log_file = os.path.join(log_dir, 'topics.log')
logger.info(f"Runtime topic generation enabled. Topics will be logged to: {topics_log_file}")
topics_logger = get_topics_logger(topics_log_file) if topics_log_file else None
# Create and run the application
app = Application(config, topics_logger=topics_logger)
# Ensure we have internet before starting
if not is_connected():
logging.getLogger(__name__).warning("No internet detected at startup. Waiting for connectivity...")
wait_for_connection(logger=logging.getLogger(__name__))
app.run()
if __name__ == "__main__":
main()