Skip to content

Commit

Permalink
[rss] Minor cleanup of messages, queries, code style
Browse files Browse the repository at this point in the history
  • Loading branch information
saltire committed Jul 11, 2013
1 parent c7ada22 commit 18f893f
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def checkdb(cursor):
cursor.execute("CREATE TABLE IF NOT EXISTS rss ( channel text, site_name text, site_url text, fg text, bg text)")
cursor.execute('CREATE TABLE IF NOT EXISTS rss (channel text, site_name text, site_url text, fg text, bg text)')


def setup(bot):
Expand Down Expand Up @@ -64,12 +64,13 @@ def manage_rss(bot, trigger):
channel = text[2]
feed_name = text[3]
feed_url = text[4]
fg = text[5].zfill(2) if len(text) >= 6 else '01'
bg = text[6].zfill(2) if len(text) >= 7 else '00'
fg = (int(text[5]) % 16).zfill(2) if len(text) >= 6 and text[5].isdigit() else '01'
bg = (int(text[6]) % 16).zfill(2) if len(text) >= 7 and text[6].isdigit() else '00'

c.execute('SELECT * FROM rss WHERE channel = %s AND site_name = %s' % (SUB * 2), (channel, feed_name))
c.execute('SELECT * FROM rss WHERE channel = %s AND site_name = %s' % (SUB * 2),
(channel, feed_name))
if c.fetchall():
c.execute('UPDATE rss SET site_url=%s, fg=%s, bg=%s WHERE channel = %s AND site_name = %s' % (SUB * 5),
c.execute('UPDATE rss SET site_url = %s, fg = %s, bg = %s WHERE channel = %s AND site_name = %s' % (SUB * 5),
(feed_url, fg, bg, channel, feed_name))
bot.reply("Successfully modified the feed.")
else:
Expand All @@ -96,20 +97,22 @@ def manage_rss(bot, trigger):
bot.reply("Successfully removed the feed from the given channel.")
else:
# .rss del Feed_Name
c.execute('DELETE FROM rss WHERE site_name = %s' % SUB,
(text[2],))
c.execute('DELETE FROM rss WHERE site_name = %s' % SUB, (text[2],))
conn.commit()
c.close()
bot.reply("Successfully removed the feed from all channels.")

elif text[1] == 'list':
c.execute("SELECT * FROM rss")
k = 0
for row in c.fetchall():
k += 1
bot.say("{0} {1} ({2}) - {3} {4}".format(*row))
if k == 0:
bot.reply("No RSS feeds in database.")
c.execute('SELECT * FROM rss')
feeds = c.fetchall()

if not feeds:
bot.reply("No RSS feeds in the database.")
else:
noun = 'feeds' if len(feeds) != 1 else 'feed'
bot.say("{0} RSS {1} in database:".format(len(feeds), noun))
for feed in feeds:
bot.say("{0} \x03{3},{4}{1}\x03 {2} - {3} {4}".format(*feed))

conn.close()

Expand All @@ -119,7 +122,7 @@ def manage_rss(bot, trigger):
def startrss(bot, trigger):
"""Begin reading RSS feeds. [-v : Verbose | -q : Quiet | -i [seconds] : Set fetch interval | --stop : Stop fetching] """
if not trigger.admin:
bot.reply("You must be an admin to start up the RSS feeds.")
bot.reply("You must be an admin to start fetching RSS feeds.")
return

global first_run, DEBUG, STOP
Expand All @@ -141,11 +144,11 @@ def startrss(bot, trigger):

if flag == '--stop':
STOP = True
bot.reply("Okay, I'll stop RSS fetching...")
bot.reply("Okay, I'll stop fetching RSS feeds.")
else:
STOP = False
bot.reply("Okay, I'll start RSS fetching..." if first_run else
"Okay, I'll restart RSS fetching...")
bot.reply("Okay, I'll start fetching RSS feeds..." if first_run else
"Continuing to fetch RSS feeds...")
first_run = False


Expand All @@ -158,7 +161,7 @@ def read_feeds(bot):
conn = bot.db.connect()
c = conn.cursor()
checkdb(c)
c.execute("SELECT * FROM rss")
c.execute('SELECT * FROM rss')
feeds = c.fetchall()

if not feeds:
Expand All @@ -167,8 +170,8 @@ def read_feeds(bot):
return

if DEBUG:
s = 's' if len(feeds) != 1 else ''
msg_all_channels(bot, "Checking {0} RSS feed{1}...".format(len(feeds), s))
noun = 'feeds' if len(feeds) != 1 else 'feed'
msg_all_channels(bot, "Checking {0} RSS {1}...".format(len(feeds), noun))

for feed in feeds:
feed_channel, feed_name, feed_url, feed_fg, feed_bg = feed
Expand All @@ -180,22 +183,23 @@ def read_feeds(bot):
msg_all_channels(bot, "Can't parse, " + str(E))

if DEBUG:
bot.msg(feed_channel, "Found {0} entries for {1}".format(len(fp.entries), feed_name))
noun = 'entries' if len(fp.entries) != 1 else 'entry'
bot.msg(feed_channel, "Found {0} {1} for {2}".format(len(fp.entries), noun, feed_name))

try:
entry = fp.entries[0]
except IndexError:
continue

# check if new entry
c.execute("CREATE TABLE IF NOT EXISTS recent ( channel text, site_name text, article_title text, article_url text )")
c.execute('CREATE TABLE IF NOT EXISTS recent (channel text, site_name text, article_title text, article_url text)')
c.execute('SELECT * FROM recent WHERE channel = %s AND site_name = %s and article_title = %s AND article_url = %s' % (SUB * 4),
(feed_channel, feed_name, entry.title, entry.link))
if c.fetchall():
if DEBUG:
bot.msg(feed_channel, u"Skipping previously read entry: [{0}] {1}".format(feed_name, entry.title))
else:
# print entry
# print entry and save into recent
message = "[{0}] \x02{1}\x02 {2}".format(feed_name_colour, entry.title, entry.link)
if entry.updated:
message += " - " + entry.updated
Expand Down

0 comments on commit 18f893f

Please sign in to comment.