Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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:
Expand Down Expand Up @@ -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!"
Expand Down
1 change: 1 addition & 0 deletions config.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 9 additions & 5 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ 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():
self.__db_worker.execute(insert.strip())
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"""
Expand All @@ -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

Expand All @@ -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
17 changes: 13 additions & 4 deletions feedupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down