Skip to content

Commit

Permalink
initial tumblr nonoauth working
Browse files Browse the repository at this point in the history
  • Loading branch information
nouyang-curoverse committed Mar 25, 2012
1 parent 159e033 commit 434ffe5
Showing 1 changed file with 72 additions and 15 deletions.
87 changes: 72 additions & 15 deletions tumblr_api.py
@@ -1,22 +1,79 @@
#!/usr/bin/python
import tumblpy as Tumblpy
#!/usr/bin/env python

from config import TUMBLR_API_KEY, TUMBLR_API_SECRET
"""
Python wrapper, tumblr, list photos on nyancatwashere tumblr.
API Documentation:
http://www.tumblr.com/docs/en/api/v2#posts
"""

from config import TUMBLR_API_KEY

API_KEY = TUMBLR_API_KEY
API_SECRET = TUMBLR_API_SECRET

t = Tumblpy(app_key = API_KEY,
app_secret = API_SECRET,
callback_url = 'http://example.com/callback/')

auth_props = t.get_authentication_tokens()
auth_url = auth_props['auth_url']
try:
import json
except ImportError: # pragma: no cover
# For older versions of Python.
import simplejson as json

try:
from urllib import urlencode
except ImportError: # pragma: no cover
# For Python 3.
from urllib.parse import urlencode

try:
from urllib import quote
except ImportError: # pragma: no cover
# For Python 3.
from urllib.parse import quote

try:
from urllib2 import urlopen
except ImportError: # pragma: no cover
# For Python 3.
from urllib.request import urlopen

import requests


class Tumblr_API(object):
"""Hacked single-use wrapper for Tumblr API"""

def __init__(self):
"""Base URLs should have no '/' at the end"""
self.base_url = ''

def call_api(self, directory):
url_list = [self.base_url]
url_list.append('/posts/photo?&api_key=%s' % API_KEY)
url_list.append('&%s' % str(directory))
url = ''.join(url_list)
print url
r = requests.get(url)
j = json.loads(r.content)
#proposals = j["proposals"]
#results = [x["title"] for x in proposals]
#return results


class Posts(Tumblr_API):

def __init__(self):
self.base_url = 'http://api.tumblr.com/v2/blog/'

oauth_token = auth_props['oauth_token']
oauth_token_secret = auth_props['oauth_token_secret']
def PhotosFromUser(self, username):
"""
http://api.tumblr.com/v2/blog/nyancatwashere.tumblr.com/posts/photo?&api_key=
"""
url = '%s.tumblr.com' % (username)
return self.call_api(url)

t = Tumblpy(app_key = API_KEY,
app_secret = API_SECRET,
oauth_token=session['tumblr_session_keys']['oauth_token'],
oauth_token_secret=session['tumblr_session_keys']['oauth_token_secret'])
""" to use:
import tumblr_api
from tumblr_api import (Tumblr_API, Posts)
testAPI = tumblr_api.Posts()
testAPI.PhotosFromUser('nyancatwashere')
"""

0 comments on commit 434ffe5

Please sign in to comment.