File size: 2,357 Bytes
3fe0726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import logging
from news_scraper.services.news_processor import NewsProcessor
from news_scraper.adapters.alpaca_ws import AlpacaNewsFeedAdapter
from news_scraper.helpers.timer import Timer

def main():
    # Configure logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )
    
    # Initialize the timer
    timer = Timer("logs/news_processing_times.log")
    
    # Initialize the news processor
    news_processor = NewsProcessor()
    
    # Register an async callback for news processing
    async def news_callback(news_item):
        # Stop timing when the news item is processed
        timer.stop_timing(news_item)
        print(f"[PROCESSOR] [FUNC] Processing | {news_item.headline if hasattr(news_item, 'headline') else ''}")
    
    news_processor.register_callback(news_callback)
    
    # Start processing news items
    news_processor.start_processing()
    
    # Initialize AlpacaNewsFeedAdapter
    def print_news(news_item):
        # Start timing when the news item enters the queue
        timer.start_timing(news_item)
        print(f"[PROCESSOR] [QUEUE] News item | {news_item.headline}")
        news_processor.add_news(news_item)

    # Create the Alpaca adapter and register callback
    alpaca_adapter = AlpacaNewsFeedAdapter()
    alpaca_adapter.register_callback(print_news)
    
    # Initialize CalendarProcessor
    from news_scraper.services.calendar_processor import CalendarProcessor
    calendar_processor = CalendarProcessor()
    last_run_date = None
    
    # Keep the main thread alive to receive messages
    try:
        while True:
            # Run Calendar Processor daily
            from datetime import date
            current_date = date.today()
            if last_run_date != current_date:
                calendar_processor.run_daily_scan()
                last_run_date = current_date

            # Periodically log queue statistics
            if time.time() % 120 < 1:  # Roughly every two minutes
                timer.get_queue_stats()
                timer.get_processing_stats()
            time.sleep(1)
    except KeyboardInterrupt:
        print("Exiting...")
        alpaca_adapter.close()
        news_processor.stop_processing()

if __name__ == "__main__":
    main()