Last active
August 29, 2015 14:23
-
-
Save riga/c2484e32e71205c12c84 to your computer and use it in GitHub Desktop.
HipChat Integration parsing the TheCodingLove.com feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
HipChat integration that parses the thecodinglove.com feed and sends | |
new posts to a HipChat room. | |
Dependencies: | |
requests, https://pypi.python.org/pypi/requests | |
feedparser, https://pypi.python.org/pypi/feedparser | |
Make sure to install "requests" with ssl support via "pip install requests[security]". | |
""" | |
# python imports | |
import time | |
import re | |
import json | |
# external imports | |
import feedparser | |
import requests | |
# config | |
INTERVAL = 15 * 60 | |
FEED = "http://thecodinglove.com/rss" | |
HOST = "https://api.hipchat.com" | |
ROOM_ID = "YOUR_ROOM_ID" | |
AUTH_TOKEN = "YOUR_AUTH_TOKEN" | |
MESSAGE = "<a href='{link}'>{title}</a><br /><br /><img src='{image}' />" | |
URL = "{HOST}/v2/room/{ROOM_ID}/notification?auth_token={AUTH_TOKEN}".format(**locals()) | |
# store the date of the last publication to identify new ones | |
last_date = None | |
# compiled re to extract the gif address out of the page of a post | |
image_cre = re.compile("(http\:\/\/tclhost\.com\/(.+)\.gif)") | |
def main(): | |
global last_date | |
# initially, set the last date | |
last_date = fetch_item(0)[0] | |
# start the loop | |
while 1: | |
fetch() | |
time.sleep(INTERVAL) | |
def fetch(): | |
global last_date | |
# fetch items since the last date | |
items = [] | |
n = 0 | |
while 1: | |
item = (date, _, _) = fetch_item(n) | |
if date <= last_date: | |
break | |
else: | |
items.append(item) | |
n += 1 | |
# obtain image links for all items and send messages | |
for item in items[::-1]: | |
date, title, link = item | |
image = fetch_image(link) | |
if image is not None: | |
send_message(title=title, link=link, image=image) | |
# update the last date | |
last_date = date | |
def fetch_item(n): | |
feed = feedparser.parse(FEED) | |
item = feed["entries"][n] | |
date = item["published_parsed"] | |
title = item["title"] | |
link = item["links"][0]["href"] | |
return date, title, link | |
def fetch_image(link): | |
res = requests.get(link) | |
if not (200 <= res.status_code <= 299): | |
return None | |
m = image_cre.search(res.text) | |
return m.group(1) if m else None | |
def send_message(**kwargs): | |
headers = { "Content-Type": "application/json" } | |
data = { | |
"color": "red", | |
"notify": False, | |
"message_format": "html", | |
"message": MESSAGE.format(**kwargs) | |
} | |
res = requests.post(URL, data=json.dumps(data), headers=headers) | |
print "sent (%s): %s" % (", ".join(kwargs.values()), res.status_code) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment