Skip to content

Instantly share code, notes, and snippets.

@sebmaynard
Last active Dec 19, 2015
Embed
What would you like to do?
Generate an images-only feed for the Dilbert comic.
# update dilbert rss every morning at 2am
0 2 * * * /home/user/bin/update_dilbert.sh
#!/usr/bin/env python
# quick install for the dependencies:
# apt-get install python-pip python-dateutil
# pip install beautifulsoup4 requests feedparser webhelpers
import requests
from bs4 import BeautifulSoup
import feedparser
from webhelpers.feedgenerator import Atom1Feed
from datetime import datetime
from time import mktime
from dateutil import parser
base = "http://www.dilbert.com"
feed = "http://feed.dilbert.com/dilbert/daily_strip"
rss = feedparser.parse(feed)
feed_updated = datetime.fromtimestamp(mktime(rss["feed"]["updated_parsed"]))
output = Atom1Feed(title=u"Dilbert Images Only",
link=base,
description=u"Get the dilbert images")
for entry in rss["entries"]:
link = entry["link"]
title = entry["title"]
soup = BeautifulSoup(requests.get(entry["link"]).content)
img_wrapper = soup.findAll("div", {'class': 'STR_Image' })[0]
img_link = base + img_wrapper.find("img")["src"]
output.add_item(title=title,
link=link,
description="<a href='" + link + "'><img src='" + img_link + "' /></a>",
pubdate=parser.parse(entry["published"]))
print output.writeString('utf-8')
#!/bin/bash
cd /home/user/bin/
python dilbert.py > /home/user/public_html/other/dilbert.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment