diff --git a/bot.py b/bot.py index de25db5..d6c6755 100644 --- a/bot.py +++ b/bot.py @@ -82,7 +82,10 @@ def __handle_msg(self, msg): items = items[::-1] for entry in items: - answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" + if self.__config.excerpt_len > 0: + answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + entry[4] + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" + else: + answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" # Print last config.feedlimit news for a specific feed elif msg.startswith("!lastfeed"): @@ -95,7 +98,10 @@ def __handle_msg(self, msg): if not self.__config.feedorderdesc: items = items[::-1] for entry in items: - answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" + if self.__config.excerpt_len > 0: + answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + str(entry[4]) + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" + else: + answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text(self.color_url,str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n" # Else tell the user how to use the bot else: @@ -201,8 +207,12 @@ def start(self): 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)) + def print_feed_update(feed_title, news_title, news_url, news_date, news_excerpt=""): + if news_excerpt: + output_str = "[+]: {}||{}||{}||{}||{}".format(feed_title, news_title, news_excerpt, news_url, news_date) + else: + output_str = "[+]: {}||{}||{}||{}".format(feed_title, news_title, news_url, news_date) + print(output_str) if self.__config.update_before_connecting: print "Started pre-connection updates!" diff --git a/config.py.sample b/config.py.sample index f33e878..0f14a1d 100644 --- a/config.py.sample +++ b/config.py.sample @@ -25,3 +25,4 @@ class Config(object): self.feedlimit = 10 self.feedorderdesc = True self.update_before_connecting = True #Update all feeds before connecting to the IRC server + self.excerpt_len = 0 # Do not show a description. Otherwise limit to N characters diff --git a/db.py b/db.py index 2622e9f..f2e4257 100644 --- a/db.py +++ b/db.py @@ -18,7 +18,7 @@ def __initiate_db(self): if not os.path.exists(self.__db_path): self.__db_worker = Sqlite3Worker(self.__db_path) self.__db_worker.execute('CREATE TABLE feeds (id INTEGER PRIMARY KEY AUTOINCREMENT, name CHAR(200) UNIQUE, url CHAR(200) UNIQUE, frequency INTEGER(3))') - self.__db_worker.execute('CREATE TABLE news (id INTEGER PRIMARY KEY AUTOINCREMENT, title CHAR(255), url CHAR(255), feedid INTEGER, published TEXT, FOREIGN KEY(feedid) REFERENCES feeds(id))') + self.__db_worker.execute('CREATE TABLE news (id INTEGER PRIMARY KEY AUTOINCREMENT, title CHAR(255), url CHAR(255), feedid INTEGER, published TEXT, excerpt TEXT, FOREIGN KEY(feedid) REFERENCES feeds(id))') if os.path.exists("./feeds.sql"): f = open("./feeds.sql", "r") for insert in f.readlines(): @@ -26,6 +26,10 @@ def __initiate_db(self): f.close() else: self.__db_worker = Sqlite3Worker(self.__db_path) + try: + self.__db_worker.execute('ALTER TABLE news ADD COLUMN excerpt TEXT;') + except Exception as e: + print(e) def get_feeds(self): """Returns all feeds""" @@ -37,14 +41,14 @@ def get_feeds(self): def get_news_from_feed(self, feed_id, limit=10): """Returns 'limit' news from a specific feed""" news = [] - for item in self.__db_worker.execute("select id, title, url, published from news where feedid = :feedid order by id desc limit :limit", {'feedid': feed_id, 'limit':limit}): + for item in self.__db_worker.execute("select id, title, url, published, excerpt from news where feedid = :feedid order by id desc limit :limit", {'feedid': feed_id, 'limit':limit}): news.append(item) return news def get_latest_news(self, limit=10): """Returns 'limit' latest news""" news = [] - for item in self.__db_worker.execute("select id, title, url, published from news order by id desc limit :limit", {'limit':limit}): + for item in self.__db_worker.execute("select id, title, url, published, excerpt from news order by id desc limit :limit", {'limit':limit}): news.append(item) return news @@ -58,10 +62,10 @@ def get_news_count(self): count = self.__db_worker.execute("select count(id) from news")[0][0] return count - def insert_news(self, feed_id, title, url, published): + def insert_news(self, feed_id, title, url, published, excerpt): """Checks if a news item with the given information exists. If not, create a new entry.""" exists = self.__db_worker.execute("select exists(select 1 FROM news WHERE feedid = :feedid and url = :url and published = :published LIMIT 1)", {'feedid': feed_id, 'url': url, 'published': published})[0][0] if exists: return False - self.__db_worker.execute("INSERT INTO news (title, url, feedid, published) VALUES (:title, :url, :feedid, :published)", {'title': title, 'url': url, 'feedid': feed_id, 'published': published}) + self.__db_worker.execute("INSERT INTO news (title, url, feedid, published, excerpt) VALUES (:title, :url, :feedid, :published, :except)", {'title': title, 'url': url, 'feedid': feed_id, 'published': published, 'excerpt':excerpt}) return True diff --git a/feedupdater.py b/feedupdater.py index d319e9a..c1aac45 100644 --- a/feedupdater.py +++ b/feedupdater.py @@ -73,10 +73,15 @@ def __fetch_feed(self, feed_info, callback, forever): except Exception as e: newsdate = "no date" + if self.__config.excerpt_len > 0: + newsexcerpt = newsitem.summary[:self.__config.excerpt_len] + else: + newsexcerpt = "" + # Update the database. If it's a new issue, post it to the channel - is_new = self.__db.insert_news(feed_info['id'], newstitle, newsitem.link, newsdate) + is_new = self.__db.insert_news(feed_info['id'], newstitle, newsitem.link, newsdate, newsexcerpt) if is_new and callback is not None: - callback(feed_info['title'], newstitle, newsurl, newsdate) + callback(feed_info['title'], newstitle, newsurl, newsdate, newsexcerpt) print "Updated: " + feed_info['title'] except Exception as e: print e @@ -89,8 +94,12 @@ def __fetch_feed(self, feed_info, callback, forever): time.sleep(int(feed_info['published'])*60) if __name__ == "__main__": - def print_line(feed_title, news_title, news_url, news_date): - print("[+]: {}||{}||{}||{}".format(feed_title, news_title, news_url, news_date)) + def print_line(feed_title, news_title, news_url, news_date, news_excerpt): + if news_excerpt: + output_str = "[+]: {}||{}||{}||{}||{}".format(feed_title, news_title, news_excerpt, news_url, news_date) + else: + output_str = "[+]: {}||{}||{}||{}".format(feed_title, news_title, news_url, news_date) + print(output_str) def main(): config = Config()