Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 7822e1fc0531421d847b331c6c574ff74d4568e7 @zachwill committed Jun 18, 2012
Showing with 88 additions and 0 deletions.
  1. +4 −0 README.md
  2. +3 −0 requirements.txt
  3. +36 −0 setup.py
  4. +1 −0 tumblr/__init__.py
  5. +44 −0 tumblr/api.py
@@ -0,0 +1,4 @@
+Tumblr
+======
+
+A Python API wrapper for using the Tumblr API and Tumblr's OAuth system.
@@ -0,0 +1,3 @@
+requests
+requests-oauth
+simplejson
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+from setuptools import setup
+
+setup(
+ name="tumblr",
+ version="0.1",
+ description="An easy-to-use Python wrapper for the Tumblr API.",
+ author="Zach Williams",
+ author_email="hey@zachwill.com",
+ url="http://github.com/zachwill/tumblr",
+ license="MIT",
+ classifiers=[
+ 'Development Status :: 4 - Beta',
+ 'Environment :: Console',
+ 'Intended Audience :: Developers',
+ 'Intended Audience :: End Users/Desktop',
+ 'License :: OSI Approved :: MIT License',
+ 'Natural Language :: English',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 2.6',
+ 'Programming Language :: Python :: 2.7',
+ 'Topic :: Scientific/Engineering :: Information Analysis',
+ 'Topic :: Software Development :: Libraries :: Python Modules',
+ 'Topic :: Utilities'
+ ],
+ packages=[
+ "tumblr"
+ ],
+ install_requires=[
+ "requests",
+ "requests-oauth",
+ "simplejson"
+ ]
+)
@@ -0,0 +1 @@
+from api import *
@@ -0,0 +1,44 @@
+"""
+Functionality for Tumblr's API.
+"""
+
+import os
+import requests as req
+import simplejson as json
+from oauth_hook import OAuthHook
+
+
+def auth():
+ """Return an authorized requests client."""
+ key = os.getenv('TUMBLR_CONSUMER_KEY')
+ secret = os.getenv('TUMBLR_CONSUMER_SECRET')
+ oauth_token = os.getenv('TUMBLR_OAUTH_KEY')
+ oauth_secret = os.getenv('TUMBLR_OAUTH_SECRET')
+ oauth = OAuthHook(key, secret, oauth_token, oauth_secret, True)
+ return req.session(hooks={'pre_request': oauth})
+
+
+def endpoint(name, *args):
+ """Return a Tumblr API endpoint."""
+ name = "%s.tumblr.com" % name
+ url = ["http://api.tumblr.com/v2/blog", name]
+ url.extend(args)
+ return "/".join(url)
+
+
+def post(name, *args, **params):
+ """Perform a POST request on the Tumblr API."""
+ client = auth()
+ url = endpoint(name, *args)
+ response = client.post(url, params=params)
+ return json.loads(response.text)
+
+
+def submit(name, **params):
+ """Submit a blog post."""
+ if 'type' not in params:
+ params['type'] = 'text'
+ if 'tags' in params:
+ tags = params.pop('tags')
+ params['tags'] = ','.join(tags)
+ return post(name, 'post', **params)

0 comments on commit 7822e1f

Please sign in to comment.