-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrssbot2.py
More file actions
117 lines (104 loc) · 4.04 KB
/
rssbot2.py
File metadata and controls
117 lines (104 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
#
# rssbot2.py
#
# Github Repo:
# https://github.com/sodonnell/rssbot2/
#
# Install Requirements:
# $ sudo pip install feedparser mysql-connector-python
#
__version__ = "2.0"
__author__ = "Sean O'Donnell <sean@seanodonnell.com>"
__license__ = "GPL"
__copyright__ = "Copyright 2019, Sean O'Donnell"
import mysql.connector, feedparser, config
class rssbot2:
def __init__(self):
# SET DEFAULT OBJECT PARAMS
self.title = "rssbot2"
self.set_root_url("https://github.com/sodonnell/rssbot2/")
self.set_useragent("rssbot/2.0 +%s" % self.root_url)
self.set_max_feeds(250)
self.debug = 1
def set_root_url(self,url):
# define the root URL for the rssbot.org webUI
self.root_url = url
def set_max_feeds(self,int):
# Maximum number of RSS Feeds to scour per-session (250 max suggested)
self.max_feeds = int
def set_useragent(self,agent):
# define a custom user agent for this application
self.user_agent = agent
feedparser.USER_AGENT = agent
def db_connect(self):
self.conn = mysql.connector.connect(host=config.db['host'],user=config.db['user'],password=config.db['password'],database=config.db['database'])
return self.conn
def get_feeds(self):
sql = "SELECT title, link, id FROM rssbot2_feeds WHERE active = 'Y' ORDER BY RAND() LIMIT 0, {}".format(self.max_feeds)
cursor = self.conn.cursor()
cursor.execute(sql)
self.feeds = cursor.fetchall()
self.feeds_count = cursor.rowcount
cursor.close()
def deactivate_feed(self,id):
sql = "UPDATE rssbot2_feeds SET active = 'N' WHERE id = '{}'". format(id)
print(sql)
cursor = self.conn.cursor()
cursor.execute(sql)
cursor.close()
print("Feed %d de-activated." % id)
def activate_feed(self,id):
sql = "UPDATE rssbot2_feeds SET active = 'Y' WHERE id = '{}'". format(id)
print(sql)
cursor = self.conn.cursor()
cursor.execute(sql)
cursor.close()
print("Feed %d activated." % id)
def add_feed(self,link,active='N'):
# get rss feed data via link
sql = "select id, active from rssbot2_feeds where link = '{}'". format(link)
cursor = self.conn.cursor()
cursor.execute(sql)
self.feed = cursor.fetchone()
self.feed_count = cursor.rowcount
if self.feed_count == 0:
rss = feedparser.parse(link,referrer=self.root_url)
try: rss.status
except:
print('HTTP Status not found.')
return -1
else:
http_status = "HTTP Response Status Code: %d" % (rss.status)
print(http_status)
if rss.feed.has_key('title'):
rss.feed.title = rss.feed.title.replace("'",r"\'")
cursor = self.conn.cursor()
sql = "INSERT IGNORE INTO rssbot2_feeds ( title, link, active) VALUES ('%s','%s','%s')" % (rss.feed.title,link,active)
cursor.execute(sql)
self.conn.commit()
id = cursor.lastrowid
cursor.close()
return id
else:
return 0
else:
print("Existing feed_id: %d" % self.feed[0])
# temporary kludge. @todo fix this entire process.
if active == "N":
self.deactivate_feed(self.feed[0])
return -3
# temporary kludge. @todo fix this entire process.
elif active == "Y":
self.activate_feed(self.feed[0])
return -3
return -2
def add_link(self,feed_id,title,link):
cursor = self.conn.cursor()
title = title.replace("'",r"\'")
sql = "INSERT IGNORE INTO rssbot2_archive ( title, link, feed_id) VALUES ('%s','%s','%s')" % (title,link,feed_id)
cursor.execute(sql)
self.conn.commit()
id = cursor.lastrowid
cursor.close()
return id