Created
August 26, 2011 17:48
-
-
Save jcarbaugh/1173957 to your computer and use it in GitHub Desktop.
Generate an RSS feed from your Tumblr dashboard and push it to S3.
This file contains hidden or 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
| """ | |
| Create an RSS feed from your Tumblr dashboard and write to S3. | |
| Run with -x command to write to stdout instead of pushing to S3. | |
| Requirements: | |
| boto | |
| python-s3file | |
| PyRSS2Gen | |
| """ | |
| from s3file import s3open | |
| from urllib import urlencode | |
| import datetime | |
| import json | |
| import PyRSS2Gen | |
| import re | |
| import sys | |
| import urllib2 | |
| # Put your Tumblr credentials here... | |
| TUMBLR_EMAIL = "" | |
| TUMBLR_PASSWORD = "" | |
| # and your S3 keys go here. | |
| S3_KEY = "" | |
| S3_SECRET = "" | |
| # Full URL to file on S3. This one is an example... change it. | |
| S3_URL = "http://mybucket.s3.amazonaws.com/feeds/tumblr-dashbord.rss" | |
| # really bad tag stripper | |
| TAG_STRIPPER = re.compile(r'<.*?>') | |
| # Tumblr client | |
| class Tumblr(object): | |
| def __init__(self, email=None, password=None): | |
| self.email = email | |
| self.password = password | |
| def _call(self, url, params=None, auth=True): | |
| params = params.copy() if params else {} | |
| if auth and self.email and self.password: | |
| params['email'] = self.email | |
| params['password'] = self.password | |
| resp = urllib2.urlopen(url, data=urlencode(params)) | |
| else: | |
| resp = urllib2.urlopen(url) | |
| return resp.read() | |
| def dashboard(self, **kwargs): | |
| url = "http://www.tumblr.com/api/dashboard/json" | |
| resp = self._call(url, params=kwargs) | |
| return json.loads(resp.strip()[22:-1]) | |
| # utility methods | |
| def parse_datetime(dt): | |
| return datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S %Z") | |
| def titleify(t): | |
| t = TAG_STRIPPER.sub('', t) | |
| if len(t) > 60: | |
| t = t[:57] + "..." | |
| return t | |
| # post handlers | |
| def default_handler(post): | |
| return { | |
| "title": post['slug'], | |
| "description": "", | |
| } | |
| def post_handler(post): | |
| return { | |
| "title": post['regular-title'], | |
| "description": post['regular-body'], | |
| } | |
| def photo_handler(post): | |
| description = """<img src="%s">\n<p>%s</p>""" % (post['photo-url-400'], post['photo-caption']) | |
| return { | |
| "title": post['photo-caption'], | |
| "description": description, | |
| } | |
| def quote_handler(post): | |
| description = """<blockquote>%s</blockquote>\n<p>%s</p>""" % (post['quote-text'], post['quote-source']) | |
| return { | |
| "title": post['quote-text'], | |
| "description": description, | |
| } | |
| def link_handler(post): | |
| description = """<a href="%s">%s</a>\n\n%s""" % (post['link-url'], post['link-text'], post['link-description']) | |
| return { | |
| "link": post['link-url'], | |
| "title": post['link-text'], | |
| "description": description, | |
| } | |
| def conversation_handler(post): | |
| pass | |
| def video_handler(post): | |
| description = """%s\n<p>%s</p>""" % (post['video-player'], post['video-caption']) | |
| return { | |
| "title": post['video-caption'], | |
| "description": description, | |
| } | |
| def audio_handler(post): | |
| pass | |
| def answer_handler(post): | |
| return { | |
| "title": post['question'], | |
| "description": post['answer'], | |
| } | |
| POST_HANDLERS = { | |
| 'regular': post_handler, | |
| 'photo': photo_handler, | |
| 'quote': quote_handler, | |
| 'link': link_handler, | |
| 'conversation': conversation_handler, | |
| 'video': video_handler, | |
| 'audio': audio_handler, | |
| 'answer': answer_handler, | |
| } | |
| # main jazz | |
| if __name__ == "__main__": | |
| t = Tumblr(TUMBLR_EMAIL, TUMBLR_PASSWORD) | |
| dashboard = t.dashboard() | |
| items = [] | |
| for post in dashboard['posts']: | |
| handler = POST_HANDLERS.get(post['type'], default_handler) | |
| params = handler(post) | |
| if params: | |
| if not 'link' in params: | |
| params['link'] = post['url'] | |
| params['guid'] = PyRSS2Gen.Guid(post['url']) | |
| params['pubDate'] = parse_datetime(post['date-gmt']) | |
| if params['title']: | |
| params['title'] = "%s: %s" % (post['tumblelog']['name'], titleify(params['title'])) | |
| else: | |
| params['title'] = post['tumblelog']['name'] | |
| items.append(PyRSS2Gen.RSSItem(**params)) | |
| else: | |
| print "NO HANDLER!!!!", post | |
| rss = PyRSS2Gen.RSS2( | |
| title="Tumblr Dashboard", | |
| link="http://www.tumblr.com/dashboard", | |
| description="Tumblr dashboard feed", | |
| lastBuildDate=datetime.datetime.utcnow(), | |
| items=items, | |
| ) | |
| if "-x" in sys.argv: | |
| rss.write_xml(sys.stdout) | |
| else: | |
| s3 = s3open(S3_URL, S3_KEY, S3_SECRET) | |
| rss.write_xml(s3) | |
| s3.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment