Skip to content
Merged
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
27 changes: 18 additions & 9 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, config, db, on_connect_cb):
self.color_num = self.__config.num_col
self.color_date = self.__config.date
self.color_feedname = self.__config.feedname
self.color_url = self.__config.url
self.shorturls = self.__config.shorturls
self.dateformat = self.__config.dateformat

Expand Down Expand Up @@ -65,29 +66,36 @@ def __handle_msg(self, msg):
elif msg == "!list":
answer = ""
for entry in self.__db.get_feeds():
answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text('',str(entry[2])) + self.__get_colored_text(self.color_date,", updated every ") + self.__get_colored_text(self.color_num,str(entry[3])) + self.__get_colored_text(self.color_date," min") + "\n"
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,", updated every ") + self.__get_colored_text(self.color_num,str(entry[3])) + self.__get_colored_text(self.color_date," min") + "\n"

# Print some simple stats (Feed / News count)
elif msg == "!stats":
feeds_count = self.__db.get_feeds_count()
news_count = self.__db.get_news_count()
answer = "Feeds: " + self.__get_colored_text(self.color_num,str(feeds_count)) + ", News: " + self.__get_colored_text(self.color_num,str(news_count))

# Print last 25 news.
# Print last config.feedlimit news.
elif msg == "!last":
answer = ""
for entry in self.__db.get_latest_news(self.__config.feedlimit)[::-1]:
answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text('',str(entry[2])) + ", " + self.__get_colored_text(self.color_date,entry[3]) + "\n"
items = self.__db.get_latest_news(self.__config.feedlimit)
if not self.__config.feedorderdesc:
items = items[::-1]

# Print last 25 news for a specific feed
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"

# Print last config.feedlimit news for a specific feed
elif msg.startswith("!lastfeed"):
answer = ""
try:
feedid = int(msg.replace("!lastfeed","").strip())
except:
return self.__get_colored_text('1',"Wrong command: ") + msg + ", use: !lastfeed <feedid>"
for entry in self.__db.get_news_from_feed(feedid, self.__config.feedlimit)[::-1]:
answer += "#" + self.__get_colored_text(self.color_num,str(entry[0])) + ": " + entry[1] + ", " + self.__get_colored_text('',str(entry[2])) + ", " + self.__get_colored_text(self.color_date,str(entry[3])) + "\n"
items = self.__db.get_news_from_feed(feedid, self.__config.feedlimit)
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"

# Else tell the user how to use the bot
else:
Expand Down Expand Up @@ -141,7 +149,7 @@ def send_msg(self, target, msg):
def post_news(self, feed_name, title, url, date):
"""Posts a new announcement to the channel"""
try:
msg = self.__get_colored_text(self.color_feedname,str(feed_name)) + ": " + title + ", " + self.__get_colored_text('',url) + ", " + self.__get_colored_text(self.color_date,str(date))
msg = self.__get_colored_text(self.color_feedname,str(feed_name)) + ": " + title + ", " + self.__get_colored_text(self.color_url,url) + ", " + self.__get_colored_text(self.color_date,str(date))
self.send_msg(self.__config.CHANNEL, msg)
except Exception as e:
print e
Expand Down Expand Up @@ -177,7 +185,8 @@ def __init__(self):

def __check_config(self):
necessary_options = ["HOST", "PORT", "PASSWORD", "SSL", "CHANNEL", "NICK", "admin_nicks", "use_colors",
"num_col", "date", "feedname", "shorturls", "dateformat", "feedlimit", "update_before_connecting"]
"num_col", "date", "feedname", "shorturls", "dateformat", "feedlimit", "update_before_connecting",
"url", "feedorderdesc"]
missing_options = []
for key in necessary_options:
if not hasattr(self.__config, key):
Expand Down
2 changes: 2 additions & 0 deletions config.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class Config(object):
self.num_col = '1'
self.date = '8'
self.feedname = '2'
self.url = '4'
self.shorturls = False
self.dateformat = '%Y-%m-%d %H:%M:%S %z'
self.feedlimit = 10
self.feedorderdesc = True
self.update_before_connecting = True #Update all feeds before connecting to the IRC server
2 changes: 1 addition & 1 deletion db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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 limit :limit", {'feedid': feed_id, 'limit':limit}):
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}):
news.append(item)
return news

Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def signal_handler(signal, frame):
missing_config_keys = bot.get_missing_options()
if not len(missing_config_keys) == 0:
for key in missing_config_keys:
print "Key is missing: {}".format(key)
print "Config option '{}' is missing! Please check your config!".format(key)
os._exit(1)

bot._Bot__irc.connection.buffer_class.errors = 'replace' # prevent utf-8 error in jaraco.stream
Expand Down