· 9 years ago · Dec 12, 2016, 09:55 PM
1import time
2#import datetime
3#from datetime import datetime as dt
4import os
5import sqlite3
6import threading
7import urllib.request
8import xml.etree.ElementTree as ET
9
10
11@plugin
12class Rss(object):
13 def __init__(self, server):
14 # Variables.
15 self.server = server
16 self.network = self.server.config["network"]
17 self.commands = ["rss-add", "rss-del", "rss-list", "rss-check"]
18 self.loop = True
19 self.server.handle("command", self.handle_command, self.commands)
20 self.network = self.server.config["network"]
21 # Define and/or create database.
22 self.db = os.path.join(self.server.config["confdir"], "rss.db")
23 self.db_exists = os.path.isfile(self.db)
24 self.connection = sqlite3.connect(self.db)
25 self.cursor = self.connection.cursor()
26 if not self.db_exists:
27 # create the db
28 self.cursor.execute('''CREATE TABLE rss (id text, network text, channel text, url text)''')
29 self.connection.commit()
30 print("[DEBUG] [rss] Database created for the first time")
31 # Set up the thread to check for new feeds.
32 self.timeCheckFeeds = 60
33
34 def loop(plugin, server):
35 timeWaited = 0
36 while self.loop:
37 if timeWaited >= self.timeCheckFeeds:
38 self.checkFeeds()
39 timeWaited = 0
40 time.sleep(10)
41 timeWaited += 10
42 self.t1 = threading.Thread(target=loop, args=(self, server,))
43 self.t1.daemon = True
44 self.t1.start()
45
46 def handle_command(self, channel, user, cmd, args):
47 connection = sqlite3.connect(self.db)
48 cursor = connection.cursor()
49 if cmd == 'rss-add':
50 url = args[0]
51 cursor.execute('''SELECT url FROM rss WHERE network = ? and channel = ? and url = ?''', (self.network, channel, url))
52 if len(cursor.fetchall()) == 0:
53 cursor.execute('''INSERT INTO rss VALUES(?,?,?,?)''', ('', self.network, channel, url))
54 connection.commit()
55 self.server.doMessage(channel, "Done. RSS item %s added to %s." % (url, channel))
56 else:
57 self.server.doMessage(channel, "Error: RSS item %s already exists at %s" % (url, channel))
58 elif cmd == 'rss-list':
59 cursor.execute('''SELECT url FROM rss WHERE network = ? and channel = ?''', (self.network, channel))
60 for my_url in cursor.fetchall():
61 self.server.doMessage(channel, "RSS item: " + my_url[0])
62 elif cmd == 'rss-del':
63 url = args[0]
64 cursor.execute('''SELECT url FROM rss WHERE network = ? and channel = ? and url = ?''', (self.network, channel, url))
65 if len(cursor.fetchall()) == 0:
66 self.server.doMessage(channel, "Error: %s RSS feed not found" % url)
67 else:
68 try:
69 cursor.execute('''DELETE FROM rss WHERE network = ? and channel = ? and url = ?''', (self.network, channel, url))
70 self.server.doMessage(channel, url + " RSS feed removed")
71 connection.commit()
72 except sqlite3.Error as e:
73 self.server.doMessage(channel, "Error: %s RSS feed not removed (%s)" % (args[0], e.args[0]))
74 elif cmd == 'rss-check':
75 self.checkFeeds()
76 cursor.close()
77 connection.close()
78
79 def announceFeed(self, row):
80 self.server.prnt("Announcing feed... " + str(row))
81 # row = ('1', 'gnu', '#guppy', 'http://phys.org/rss-feed/')
82 url = row[3]
83 last_known_id = row[0]
84 bool_updated = 0
85 request = urllib.request.Request(url, headers={'user-agent': 'guppy ' + self.server.config["version"]})
86 s = urllib.request.urlopen(request)
87 s = s.read()
88 root = ET.fromstring(s)
89 for channel in root:
90 children = channel.findall('item')
91 for child in children:
92 feed_id = child.find('guid').text
93 if feed_id == last_known_id:
94 return
95 if bool_updated == 0:
96 # update the db
97 connection = sqlite3.connect(self.db)
98 cursor = connection.cursor()
99 cursor.execute("UPDATE rss SET id = ? where network = ? and channel = ? and url = ?", (feed_id, row[1], row[2], row[3]))
100 connection.commit()
101 connection.close()
102 bool_updated = 1
103 title = child.find('title').text
104 url = child.find('link').text
105 url = urllib.request.urlopen("http://tinyurl.com/api-create.php?url=" + url).readline().decode('utf8')
106 msg = url + " "
107 if child.find('category') is not None:
108 msg += "[" + child.find('category').text + "]" + " "
109 msg += title
110 self.server.doMessage(row[2], msg)
111 time.sleep(.5)
112 # print("%s [%s] %s" % (str(url), str(cat), str(title)))
113
114 def checkFeeds(self):
115 connection = sqlite3.connect(self.db)
116 cursor = connection.cursor()
117 cursor.execute('SELECT * FROM rss')
118 for row in cursor.fetchall():
119 self.announceFeed(row)
120 cursor.close()