-
Notifications
You must be signed in to change notification settings - Fork 0
/
rssxkcd.py
147 lines (132 loc) · 4.87 KB
/
rssxkcd.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/local/bin/python2
import argparse
import requests
import feedparser
import time
import sys
import sqlite3
import datetime
# Command line args
parser = argparse.ArgumentParser(description='Provide HipChat integration url to post xkcd comics')
parser.add_argument('url', type=str, help='(string) a special url for posting to a hipchat room')
parser.add_argument('-c', '--commit', action='store_true', help='check the output, and commit if it looks right')
args = parser.parse_args()
class Entry:
def __init__(self, title, imglink, summary, link, pubts, posted):
self.title = title
self.imglink = imglink
self.summary = summary
self.link = link # this will be the id field in db
self.pubts = pubts
self.posted = 0
def analyze(self):
data = "Link: " + self.link + "\n"
data += "Title: " + self.title + "\n"
data += "Summary: " + self.summary + "\n"
data += "Pubts: " + self.pubts + "\n"
data += "Imglink: " + self.imglink + "\n"
data += "Posted: " + str(self.posted)
print(data)
# Get rss feed from URL (https://xkcd.com/rss.xml)
def check_rss_feed(cursor, feedurl, rssentries):
row = cursor.execute("SELECT id FROM lastpub")
lastts = row.fetchone() or ("",)
req = requests.get(feedurl, headers={
"If-Modified-Since": lastts[0]
})
if req.text:
# get the rss feed data from the feedurl
feed = feedparser.parse(feedurl)
entries = feed.entries
for i in range(len(entries)):
e = Entry(
entries[i]['title'],
entries[i]['summary'].split('\"')[3],
entries[i]['summary'].split('\"')[1],
entries[i]['link'],
entries[i]['published'],
0
)
rssentries.append(e)
return req
# Hipchat posting function
def post_to_hipchat(title, src, alttext, posturl):
payload = {
"color": "gray",
"message": "<span>" + title + "</span><br/><img src='" + src + "'/>" +
"<br/><span>(Alt-text: " + alttext + ")</span>",
"notify": True,
"message_format": "html"
}
if args.commit:
r = requests.post(posturl, data=payload)
print(title, "Posted!", args.commit)
# Database functions
def insert_entry(db, cursor, e):
if args.commit:
cursor.execute('''INSERT INTO entries(id, title, imglink, summary, pubts, posted)
VALUES(?,?,?,?,?,?)''', (e.link, e.title, e.imglink, e.summary, e.pubts, 0))
db.commit()
print("Saved entry in db", args.commit)
def update_to_posted(db, cursor, e):
if args.commit:
cursor.execute('UPDATE entries SET posted=1 WHERE id=?', (e.link,))
db.commit()
print("Updated posted for:", e.link, args.commit)
def check_if_in_db(db, cursor, e):
rc = cursor.execute('SELECT id FROM entries WHERE id=?', (e.link,))
if rc.fetchone():
return True
else:
return False
def check_if_posted(db, cursor, e):
rc = cursor.execute('SELECT posted FROM entries WHERE id=?', (e.link,))
exists = rc.fetchone()[0]
if exists is 1:
return True
else:
return False
# Primary function
def check_and_post(db, cursor, ents, posturl):
# TODO: lines 96-99 and 102-106 are ripe for refactor
update_timestamp = False
for e in ents:
indb = check_if_in_db(db, cursor, e)
if indb:
posted = check_if_posted(db, cursor, e)
if not posted:
title = e.title + " (" + str(e.link) + ")"
post_to_hipchat(title, e.imglink, e.summary, posturl)
update_to_posted(db, cursor, e)
update_timestamp = True
print("in db, not posted", datetime.datetime.now())
else:
insert_entry(db, cursor, e)
title = e.title + " (" + str(e.link) + ")"
post_to_hipchat(title, e.imglink, e.summary, posturl)
update_to_posted(db, cursor, e)
update_timestamp = True
print("not in db at all", datetime.datetime.now())
return update_timestamp
def main():
# Globals
feedurl = 'https://xkcd.com/rss.xml'
posturl = str(sys.argv[1])
RSSEntries = []
db = sqlite3.connect("feed.db")
cursor = db.cursor()
if feedurl and posturl:
req = check_rss_feed(cursor, feedurl, RSSEntries)
RSSEntries = sorted(RSSEntries, key=lambda e: e.link)
if len(RSSEntries) > 0:
need_update_timestamp = check_and_post(db, cursor, RSSEntries, posturl)
if need_update_timestamp:
newts = (req.headers["Last-Modified"],)
if args.commit:
cursor.execute("UPDATE lastpub set id=?", newts)
db.commit()
print('Updated lastpub date to: ', newts, args.commit)
else:
print("All up to date!", datetime.datetime.now())
if __name__ == "__main__":
main()