Skip to content

Commit

Permalink
[rss] Decorators and s/willie/bot per sopel-irc#276
Browse files Browse the repository at this point in the history
But it should probably also use a periodic function.
This whole module is a mess, though, and I don't want
to screw with it right now.
  • Loading branch information
embolalia committed Jun 22, 2013
1 parent 957417c commit 40f67fa
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
http://willie.dfbta.net
"""

from willie.module import commands, priority
import feedparser
import socket
import sys
Expand All @@ -23,23 +24,27 @@
def checkdb(cursor):
cursor.execute("CREATE TABLE IF NOT EXISTS rss ( channel text, site_name text, site_url text, fg text, bg text)")

def setup(willie):

def setup(bot):
global SUB
SUB = (willie.db.substitution,)
SUB = (bot.db.substitution,)


def manage_rss(willie, trigger):
@commands('rss')
@priority('low')
def manage_rss(bot, trigger):
""" .rss operation channel site_name url -- operation can be either 'add', 'del', or 'list' no further operators needed if 'list' used """
if not trigger.admin:
willie.reply("Sorry, you need to be an admin to modify the RSS feeds.")
bot.reply("Sorry, you need to be an admin to modify the RSS feeds.")
return
conn = willie.db.connect()
conn = bot.db.connect()
c = conn.cursor()
checkdb(c)
conn.commit()

text = trigger.group().split()
if len(text) < 2:
willie.reply("Proper usage: '.rss add ##channel Site_Name URL', '.rss del ##channel Site_Name URL', '.rss del ##channel'")
bot.reply("Proper usage: '.rss add ##channel Site_Name URL', '.rss del ##channel Site_Name URL', '.rss del ##channel'")
elif len(text) > 2:
channel = text[2].lower()

Expand All @@ -65,41 +70,39 @@ def manage_rss(willie, trigger):
if len(ending) == 3:
bg_colour = ending[2]
else:
willie.reply("Not enough parameters specified.")
bot.reply("Not enough parameters specified.")
return
if fg_colour:
fg_colour = fg_colour.zfill(2)
if bg_colour:
bg_colour = bg_colour.zfill(2)
c.execute('INSERT INTO rss VALUES (%s,%s,%s,%s,%s)' % (SUB*5), (channel, site_name, site_url, fg_colour, bg_colour))
c.execute('INSERT INTO rss VALUES (%s,%s,%s,%s,%s)' % (SUB * 5), (channel, site_name, site_url, fg_colour, bg_colour))
conn.commit()
c.close()
willie.reply("Successfully added values to database.")
bot.reply("Successfully added values to database.")
elif len(text) == 3 and text[1] == 'del':
# .rss del ##channel
c.execute('DELETE FROM rss WHERE channel = %s' % SUB, (channel,))
conn.commit()
c.close()
willie.reply("Successfully removed values from database.")
bot.reply("Successfully removed values from database.")
elif len(text) >= 4 and text[1] == 'del':
# .rss del ##channel Site_Name
c.execute('DELETE FROM rss WHERE channel = %s and site_name = %s' % (SUB*2), (channel, " ".join(text[3:])))
c.execute('DELETE FROM rss WHERE channel = %s and site_name = %s' % (SUB * 2), (channel, " ".join(text[3:])))
conn.commit()
c.close()
willie.reply("Successfully removed the site from the given channel.")
bot.reply("Successfully removed the site from the given channel.")
elif len(text) == 2 and text[1] == 'list':
c.execute("SELECT * FROM rss")
k = 0
for row in c.fetchall():
k += 1
willie.say("list: " + unicode(row))
bot.say("list: " + unicode(row))
if k == 0:
willie.reply("No entries in database")
bot.reply("No entries in database")
else:
willie.reply("Incorrect parameters specified.")
bot.reply("Incorrect parameters specified.")
conn.close()
manage_rss.commands = ['rss']
manage_rss.priority = 'low'


class Feed(object):
Expand All @@ -110,18 +113,18 @@ class Feed(object):
feeds = dict()


def read_feeds(willie):
def read_feeds(bot):
global restarted
global STOP

restarted = False
conn = willie.db.connect()
conn = bot.db.connect()
cur = conn.cursor()
checkdb(cur)
cur.execute("SELECT * FROM rss")
if not cur.fetchall():
STOP = True
willie.say("No RSS feeds found in database. Please add some rss feeds.")
bot.say("No RSS feeds found in database. Please add some rss feeds.")

cur.execute("CREATE TABLE IF NOT EXISTS recent ( channel text, site_name text, article_title text, article_url text )")
cur.execute("SELECT * FROM rss")
Expand All @@ -135,7 +138,7 @@ def read_feeds(willie):
try:
fp = feedparser.parse(feed_url)
except IOError, E:
willie.say("Can't parse, " + str(E))
bot.say("Can't parse, " + str(E))
entry = fp.entries[0]

if not feed_fg and not feed_bg:
Expand All @@ -154,28 +157,30 @@ def read_feeds(willie):

# only print if new entry
sql_text = (feed_channel, feed_site_name, entry.title, article_url)
cur.execute('SELECT * FROM recent WHERE channel = %s AND site_name = %s and article_title = %s AND article_url = %s' % (SUB*4), sql_text)
cur.execute('SELECT * FROM recent WHERE channel = %s AND site_name = %s and article_title = %s AND article_url = %s' % (SUB * 4), sql_text)
if len(cur.fetchall()) < 1:

response = site_name_effect + " %s \x02%s\x02" % (entry.title, article_url)
if entry.updated:
response += " - %s" % (entry.updated)

willie.msg(feed_channel, response)
bot.msg(feed_channel, response)

t = (feed_channel, feed_site_name, entry.title, article_url,)
cur.execute('INSERT INTO recent VALUES (%s, %s, %s, %s)' % (SUB*4), t)
cur.execute('INSERT INTO recent VALUES (%s, %s, %s, %s)' % (SUB * 4), t)
conn.commit()
else:
if DEBUG:
willie.msg(feed_channel, u"Skipping previously read entry: %s %s" % (site_name_effect, entry.title))
bot.msg(feed_channel, u"Skipping previously read entry: %s %s" % (site_name_effect, entry.title))
conn.close()


def startrss(willie, trigger):
@commands('startrss')
@priority('high')
def startrss(bot, trigger):
""" Begin reading RSS feeds """
if not trigger.admin:
willie.reply("You must be an admin to start up the RSS feeds.")
bot.reply("You must be an admin to start up the RSS feeds.")
return
global first_run, restarted, DEBUG, INTERVAL, STOP
DEBUG = False
Expand All @@ -184,43 +189,38 @@ def startrss(willie, trigger):
if query == '-v':
DEBUG = True
STOP = False
willie.reply("Debugging enabled.")
bot.reply("Debugging enabled.")
elif query == '-q':
DEBUG = False
STOP = False
willie.reply("Debugging disabled.")
bot.reply("Debugging disabled.")
elif query == '-i':
INTERVAL = trigger.group(3)
willie.reply("INTERVAL updated to: %s" % (str(INTERVAL)))
bot.reply("INTERVAL updated to: %s" % (str(INTERVAL)))
elif query == '--stop':
STOP = True
willie.reply("Stop parameter updated.")
bot.reply("Stop parameter updated.")

if first_run:
if DEBUG:
willie.say("Okay, I'll start rss fetching...")
bot.say("Okay, I'll start rss fetching...")
first_run = False
else:
restarted = True
if DEBUG:
willie.say("Okay, I'll re-start rss...")
bot.say("Okay, I'll re-start rss...")

if not STOP:
while True:
if STOP:
willie.reply("STOPPED")
bot.reply("STOPPED")
first_run = False
STOP = False
break
if DEBUG:
willie.say("Rechecking feeds")
read_feeds(willie)
bot.say("Rechecking feeds")
read_feeds(bot)
time.sleep(INTERVAL)

if DEBUG:
willie.say("Stopped checking")
startrss.commands = ['startrss']
startrss.priority = 'high'

if __name__ == '__main__':
print __doc__.strip()
bot.say("Stopped checking")

0 comments on commit 40f67fa

Please sign in to comment.