from threading import Thread, Timer

from Rss import *

class RssManager():
    def __init__(self, rss_options):
        self.rss_list = [Rss(opt['id'], opt['type'], opt['url'], opt['result_handler']) for opt in rss_options]
        self.running = False

    def run(self):
        try:
            for rss in self.rss_list:
                print('Info: start async run')
                t = Thread(target=rss.run)
                t.start()
        except:
            print('Error: unable to start thread')
            print('Info: start sync run')
            for rss in self.rss_list:
                rss.run()

    def start(self):
        # self.run()
        print('rss server start!!!')
        self.running = True
        self.run()
        while self.running:
            self.timer = Timer(3600, self.run)
            self.timer.start()
            self.timer.join()
            
    def stop(self):
        self.running = False
        
