def __init__(self): self.__config = Config() self.__missing_options = self.__check_config() if len(self.__missing_options) > 0: return None self.__db = FeedDB(self.__config) self.__irc = IRCBot(self.__config, self.__db, self.on_started) self.__feedupdater = FeedUpdater(self.__config, self.__db) self.__connected = False
class Bot(object): def __init__(self): self.__config = Config() self.__missing_options = self.__check_config() if len(self.__missing_options) > 0: return None self.__db = FeedDB(self.__config) self.__irc = IRCBot(self.__config, self.__db, self.on_started) self.__feedupdater = FeedUpdater(self.__config, self.__db) self.__connected = False def __check_config(self): necessary_options = [ "HOST", "PORT", "PASSWORD", "SSL", "CHANNEL", "NICK", "admin_nicks", "use_colors", "num_col", "date", "feedname", "dateformat", "feedlimit", "update_before_connecting", "url", "feedorderdesc" ] missing_options = [] for key in necessary_options: if not hasattr(self.__config, key): missing_options.append(key) return missing_options def get_missing_options(self): return self.__missing_options def start(self): """Starts the IRC bot""" threading.Thread(target=self.__irc.start).start() def initial_feed_update(self): def print_feed_update(feed_title, news_title, news_url, news_date): print(("[+]: {}||{}||{}||{}".format(feed_title, news_title, news_url, news_date))) if self.__config.update_before_connecting: print("Started pre-connection updates!") self.__feedupdater.update_feeds(print_feed_update, False) print("DONE!") def on_started(self): """ Gets executed after the IRC thread has successfully established a connection. """ if not self.__connected: print("Connected!") self.__feedupdater.update_feeds(self.__irc.post_news, True) print("Started feed updates!") if self.__config.WAIT_FOR_FIRST_MSG: print("Clearing last messages table") self.__db.reset_messages_count() self.__connected = True
def main(): config = Config() db = FeedDB(config) updater = FeedUpdater(config, db) print datetime.datetime.now(), u"Starting offline update." sys.stdout.flush() updater.update_feeds(print_line, False)
def __init__(self): self.__db = FeedDB() self.__config = Config() self.__irc = IRCBot(self.__config, self.__db, self.on_started) self.__threads = [] self.__connected = False
class Bot(object): def __init__(self): self.__db = FeedDB() self.__config = Config() self.__irc = IRCBot(self.__config, self.__db, self.on_started) self.__threads = [] self.__connected = False def start(self): """Starts the IRC bot""" threading.Thread(target=self.__irc.start).start() def on_started(self): """Gets executed after the IRC thread has successfully established a connection.""" if not self.__connected: print "Connected!" # Start one fetcher thread per feed for feed in self.__db.get_feeds(): t = threading.Thread(target=self.__fetch_feed, args=(feed, )) t.start() self.__threads.append(t) print "Started fetcher threads!" self.__connected = True def __fetch_feed(self, feed_info): """Fetches a RSS feed, parses it and updates the database and/or announces new news.""" while 1: try: # Parse a feed's url news = feedparser.parse(feed_info[2]) # Reverse the ordering. Oldest first. for newsitem in news.entries[::-1]: newstitle = newsitem.title if self.__config.shorturls: newsurl = tinyurl.create_one( newsitem.link) # Create a short link if newsurl == "Error": #If that fails, use the long version newsurl = newsitem.link else: newsurl = newsitem.link # Try to get the published or updated date. Otherwise set it to 'no date' try: # Get date and parse it newsdate = dateutil.parser.parse(newsitem.published) # Format date based on 'dateformat' in config.py newsdate = newsdate.strftime(self.__config.dateformat) except Exception as e: try: # Get date and parse it newsdate = dateutil.parser.parse(newsitem.updated) # Format date based on 'dateformat' in config.py newsdate = newsdate.strftime( self.__config.dateformat) except Exception as e: newsdate = "no date" # Update the database. If it's a new issue, post it to the channel is_new = self.__db.insert_news(feed_info[0], newstitle, newsitem.link, newsdate) if is_new: self.__irc.post_news(feed_info[1], newstitle, newsurl, newsdate) print "Updated: " + feed_info[1] except Exception as e: print e print "Failed: " + feed_info[1] # sleep frequency minutes time.sleep(int(feed_info[3]) * 60)
class Bot(object): def __init__(self): self.__db = FeedDB() self.__config = Config() self.__irc = IRCBot(self.__config, self.__db, self.on_started) self.__threads = [] self.__connected = False def start(self): """Starts the IRC bot""" threading.Thread(target=self.__irc.start).start() def on_started(self): """Gets executed after the IRC thread has successfully established a connection.""" if not self.__connected: print "Connected!" # Start one fetcher thread per feed for feed in self.__db.get_feeds(): t = threading.Thread(target=self.__fetch_feed, args=(feed,)) t.start() self.__threads.append(t) print "Started fetcher threads!" self.__connected = True def __fetch_feed(self, feed_info): """Fetches a RSS feed, parses it and updates the database and/or announces new news.""" while 1: try: # Parse a feed's url news = feedparser.parse( feed_info[2] ) # Reverse the ordering. Oldest first. for newsitem in news.entries[::-1]: newstitle = newsitem.title newsurl = tinyurl.create_one(newsitem.link) # Create a short link if newsurl == "Error": #If that fails, use the long version newsurl = newsitem.link newsurl = Colours('', newsurl).get() # Try to get the published date. Otherwise set it to 'no date' try: newsdate = newsitem.published except Exception as e: try: newsdate = newsitem.updated except Exception as e: newsdate = "no date" # Update the database. If it's a new issue, post it to the channel is_new = self.__db.insert_news(feed_info[0], newstitle, newsurl, newsdate) if is_new: self.__irc.post_news(feed_info[1], newstitle, newsurl, newsdate) print Colours('7',"Updated: ").get() + feed_info[1] except Exception as e: print e print Colours('1',"Failed: ").get() + feed_info[1] # sleep frequency minutes time.sleep(int(feed_info[3])*60)
def main(): config = Config() db = FeedDB(config) updater = FeedUpdater(config, db) updater.update_feeds(print_line, False)