Skip to content

Instantly share code, notes, and snippets.

@dselans
Created March 7, 2019 20:54
Show Gist options
  • Save dselans/ec21573cf5306257c5c1245ef6a8257e to your computer and use it in GitHub Desktop.
Save dselans/ec21573cf5306257c5c1245ef6a8257e to your computer and use it in GitHub Desktop.
#metal metalinjection rss script
#!/usr/bin/env python
#
import re
import sys
import pickle
import pprint
import feedparser
from slackclient import SlackClient
reviews_file = "./sometal.pickle"
token = 'xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxx'
feed_url = 'http://feeds.feedburner.com/metalinjection'
# metal-dev CDS3TSJRJ
# metal-dev2 CDS9ZQS9Y
# metal C1H4KK2JX
channel = 'C1H4KK2JX'
def update_known_reviews(filename, new_reviews):
reviews = load_known_reviews(filename)
updated_reviews = reviews + [v['id'] for v in new_reviews]
print "updated reviews looks like this: {0}".format(updated_reviews)
try:
pickle.dump(updated_reviews, open(filename, 'wb'))
except Exception, e:
print "Write Exception: {}".format(e)
def load_known_reviews(filename):
try:
reviews = pickle.load(open(filename, 'rb'))
except Exception, e:
print "Exception: {}".format(e)
return []
return reviews
class RSS:
def __init__(self, token, feed_url, reviews_file):
self.token = token
self.feed_url = feed_url
self.known_reviews = load_known_reviews(reviews_file)
self.sc = SlackClient(token)
def send_message(self, msg, chan):
self.sc.api_call(
"chat.postMessage",
channel=chan,
text=msg,
as_user=True,
)
def get_new_reviews(self):
feed = feedparser.parse(self.feed_url)
if 'entries' not in feed:
return []
new_reviews = []
for entry in feed['entries']:
if has_missing_keys(entry):
continue
match = re.match('^album\s+review:.+', entry['title'], re.IGNORECASE)
if match is None:
continue
# Are we aware of this release?
if entry['id'] in self.known_reviews:
continue
new_reviews.append(entry)
return new_reviews
def has_missing_keys(map):
required_keys = ['id', 'title', 'summary', 'link', 'published']
for k in required_keys:
if k not in map:
print "error: missing key in {}".format(k)
return True
return False
def main():
rss = RSS(token, feed_url, reviews_file)
reviews = rss.get_new_reviews()
if len(reviews) == 0:
print "No new reviews"
sys.exit(0)
update_known_reviews(reviews_file, reviews)
for r in reviews:
msg = ":horns: *New Album Review [{0}]* :horns:\n\n" \
"{1}".format(r['published'], r['link'])
print "Date: {}".format(r['published'])
print "Title: {}".format(r['title'])
print "Link: {}".format(r['link'])
print "------------------------------------"
rss.send_message(msg, channel)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment