Skip to content
Snippets Groups Projects
Commit fa123f6d authored by Ciaran McNally's avatar Ciaran McNally
Browse files

Added colour configuration

parent a641674d
Branches
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import tinyurl ...@@ -7,6 +7,7 @@ import tinyurl
import time import time
import re import re
import feedparser import feedparser
from colour import Colours
from db import FeedDB from db import FeedDB
from config import Config from config import Config
...@@ -17,6 +18,9 @@ class IRCBot(irc.client.SimpleIRCClient): ...@@ -17,6 +18,9 @@ class IRCBot(irc.client.SimpleIRCClient):
self.__db = db self.__db = db
self.connect(self.__config.HOST, self.__config.PORT, self.__config.NICK) self.connect(self.__config.HOST, self.__config.PORT, self.__config.NICK)
self.__on_connect_cb = on_connect_cb self.__on_connect_cb = on_connect_cb
self.num_col = self.__config.num_col
self.date = self.__config.date
self.feedname = self.__config.feedname
def on_welcome(self, connection, event): def on_welcome(self, connection, event):
"""Join the correct channel upon connecting""" """Join the correct channel upon connecting"""
...@@ -25,7 +29,7 @@ class IRCBot(irc.client.SimpleIRCClient): ...@@ -25,7 +29,7 @@ class IRCBot(irc.client.SimpleIRCClient):
def on_join(self, connection, event): def on_join(self, connection, event):
"""Say hello to other people in the channel. """ """Say hello to other people in the channel. """
connection.privmsg(self.__config.CHANNEL, "Hi, I'm " + connection.get_nickname() + " and your bot. Send !help to get a list of commands.") connection.privmsg(self.__config.CHANNEL, "Hi, I'm " + Colours('3',str(connection.get_nickname())).get() + " your bot. Send " + Colours(self.num_col,"!help").get() +" to get a list of commands.")
self.__on_connect_cb() self.__on_connect_cb()
def __handle_msg(self, msg): def __handle_msg(self, msg):
...@@ -39,19 +43,19 @@ class IRCBot(irc.client.SimpleIRCClient): ...@@ -39,19 +43,19 @@ class IRCBot(irc.client.SimpleIRCClient):
elif msg == "!list": elif msg == "!list":
answer = "" answer = ""
for entry in self.__db.get_feeds(): for entry in self.__db.get_feeds():
answer += "#" + str(entry[0]) + ": " + entry[1] + ", " + entry[2] + ", updated every " + str(entry[3]) + " min" + "\n" answer += "#" + Colours(self.num_col,str(entry[0])).get() + ": " + entry[1] + ", " + Colours('',str(entry[2])).get() + Colours(self.date,", updated every ").get() + Colours(self.num_col,str(entry[3])).get() + Colours(self.date," min").get() + "\n"
# Print some simple stats (Feed / News count) # Print some simple stats (Feed / News count)
elif msg == "!stats": elif msg == "!stats":
feeds_count = self.__db.get_feeds_count() feeds_count = self.__db.get_feeds_count()
news_count = self.__db.get_news_count() news_count = self.__db.get_news_count()
answer = "Feeds: " + str(feeds_count) + ", News: " + str(news_count) answer = "Feeds: " + Colours(self.num_col,str(feeds_count)).get() + ", News: " + Colours(self.num_col,str(news_count)).get()
# Print last 25 news. # Print last 25 news.
elif msg == "!last": elif msg == "!last":
answer = "" answer = ""
for entry in self.__db.get_latest_news()[::-1]: for entry in self.__db.get_latest_news()[::-1]:
answer += "#" + str(entry[0]) + ": " + entry[1] + ", " + entry[2] + ", " + entry[3] + "\n" answer += "#" + Colours(self.num_col,str(entry[0])).get() + ": " + entry[1] + ", " + Colours('',str(entry[2])).get() + ", " + Colours(self.date,entry[3]).get() + "\n"
# Print last 25 news for a specific feed # Print last 25 news for a specific feed
elif msg.startswith("!lastfeed"): elif msg.startswith("!lastfeed"):
...@@ -59,9 +63,9 @@ class IRCBot(irc.client.SimpleIRCClient): ...@@ -59,9 +63,9 @@ class IRCBot(irc.client.SimpleIRCClient):
try: try:
feedid = int(msg.replace("!lastfeed","").strip()) feedid = int(msg.replace("!lastfeed","").strip())
except: except:
return "Wrong command: " + msg + ", use: !lastfeed <feedid>" return Colours('1',"Wrong command: ").get() + msg + ", use: !lastfeed <feedid>"
for entry in self.__db.get_news_from_feed(feedid)[::-1]: for entry in self.__db.get_news_from_feed(feedid)[::-1]:
answer += "#" + str(entry[0]) + ": " + entry[1] + ", " + entry[2] + ", " + entry[3] + "\n" answer += "#" + Colours(self.num_col,str(entry[0])).get() + ": " + entry[1] + ", " + Colours('',str(entry[2])).get() + ", " + Colours(self.date,str(entry[3])).get() + "\n"
# Else tell the user how to use the bot # Else tell the user how to use the bot
else: else:
...@@ -115,7 +119,7 @@ class IRCBot(irc.client.SimpleIRCClient): ...@@ -115,7 +119,7 @@ class IRCBot(irc.client.SimpleIRCClient):
def post_news(self, feed_name, title, url, date): def post_news(self, feed_name, title, url, date):
"""Posts a new announcement to the channel""" """Posts a new announcement to the channel"""
try: try:
msg = feed_name + ": " + title + ", " + url + ", " + date msg = Colours(self.feedname,str(feed_name)).get() + ": " + title + ", " + url + ", " + Colours(self.date,str(date)).get()
self.send_msg(self.__config.CHANNEL, msg) self.send_msg(self.__config.CHANNEL, msg)
except Exception as e: except Exception as e:
print e print e
...@@ -170,6 +174,7 @@ class Bot(object): ...@@ -170,6 +174,7 @@ class Bot(object):
newsurl = tinyurl.create_one(newsitem.link) # Create a short link newsurl = tinyurl.create_one(newsitem.link) # Create a short link
if newsurl == "Error": #If that fails, use the long version if newsurl == "Error": #If that fails, use the long version
newsurl = newsitem.link newsurl = newsitem.link
newsurl = Colours('', newsurl).get()
# Try to get the published date. Otherwise set it to 'no date' # Try to get the published date. Otherwise set it to 'no date'
try: try:
...@@ -185,10 +190,10 @@ class Bot(object): ...@@ -185,10 +190,10 @@ class Bot(object):
if is_new: if is_new:
self.__irc.post_news(feed_info[1], newstitle, newsurl, newsdate) self.__irc.post_news(feed_info[1], newstitle, newsurl, newsdate)
print "Updated: " + feed_info[1] print Colours('7',"Updated: ").get() + feed_info[1]
except Exception as e: except Exception as e:
print e print e
print "Failed: " + feed_info[1] print Colours('1',"Failed: ").get() + feed_info[1]
# sleep frequency minutes # sleep frequency minutes
time.sleep(int(feed_info[3])*60) time.sleep(int(feed_info[3])*60)
class Colours:
def __init__(self, col, string):
self.colour = col
self.string = string
self.default = '\033[0m'
self.ret = self.string+self.default
def get(self):
if self.colour == '1' or self.colour == 'red':
return '\033[031m'+self.ret
elif self.colour == '2' or self.colour == 'green':
return '\033[032m'+self.ret
elif self.colour == '3' or self.colour == 'yellow':
return '\033[033m'+self.ret
elif self.colour == '4' or self.colour == 'blue':
return '\033[034m'+self.ret
elif self.colour == '5' or self.colour == 'purple':
return '\033[035m'+self.ret
elif self.colour == '6' or self.colour == 'cyan':
return '\033[036m'+self.ret
elif self.colour == '7' or self.colour == 'lightgreen':
return '\033[1;32m'+self.ret
elif self.colour == '8' or self.colour == 'grey':
return '\033[1;30m'+self.ret
elif self.colour == '9' or self.colour == 'pink':
return '\033[1;35m'+self.ret
elif self.colour == '10' or self.colour == 'lightblue':
return '\033[1;34m'+self.ret
else:
return '\033[1;37m'+self.ret
#Testing
if __name__ == "__main__":
for i in range(0, 11):
print Colours(str(i), 'Testing').get() + "TESTING "+Colours(str(i), 'wat').get()
...@@ -8,3 +8,11 @@ class Config(object): ...@@ -8,3 +8,11 @@ class Config(object):
self.CHANNEL = "##YOURCHANNEL" self.CHANNEL = "##YOURCHANNEL"
self.NICK = "YOURBOTNICK" self.NICK = "YOURBOTNICK"
self.admin_nicks= ['YOURADMINNICK'] self.admin_nicks= ['YOURADMINNICK']
#=Colours=
#1 - red 2 - green 3 - yellow
#4 - blue 5 - purple 6 - cyan
#7 - lightgreen 8 - grey 9 - pink
#10 - lighblue
self.num_col = '1'
self.date = '8'
self.feedname = '2'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment