|
@@ -1,62 +1,74 @@
|
|
|
|
|
+# pylint: disable=invalid-name
|
|
|
|
|
+# pylint: disable=too-many-instance-attributes
|
|
|
|
|
+
|
|
|
|
|
+'''
|
|
|
|
|
+tumblr.py contains methods for authenticating
|
|
|
|
|
+the user via tumblr OAuth as well as methods
|
|
|
|
|
+for getting data from Tumblr API.
|
|
|
|
|
+'''
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
import requests
|
|
import requests
|
|
|
import simplejson as json
|
|
import simplejson as json
|
|
|
-import time
|
|
|
|
|
-import urllib
|
|
|
|
|
|
|
+import time
|
|
|
import re
|
|
import re
|
|
|
from bs4 import BeautifulSoup
|
|
from bs4 import BeautifulSoup
|
|
|
import urlparse
|
|
import urlparse
|
|
|
import oauth2
|
|
import oauth2
|
|
|
|
|
|
|
|
-request_token_url = 'http://www.tumblr.com/oauth/request_token'
|
|
|
|
|
-authorize_url = 'http://www.tumblr.com/oauth/authorize'
|
|
|
|
|
-access_token_url = 'http://www.tumblr.com/oauth/access_token'
|
|
|
|
|
|
|
+request_token_url = 'http://www.tumblr.com/oauth/request_token'
|
|
|
|
|
+authorize_url = 'http://www.tumblr.com/oauth/authorize'
|
|
|
|
|
+access_token_url = 'http://www.tumblr.com/oauth/access_token'
|
|
|
|
|
|
|
|
-user_uri = "http://api.tumblr.com/v2/user/info"
|
|
|
|
|
-blog_uri = "http://api.tumblr.com/v2/blog/"
|
|
|
|
|
|
|
+user_uri = "http://api.tumblr.com/v2/user/info"
|
|
|
|
|
+blog_uri = "http://api.tumblr.com/v2/blog/"
|
|
|
|
|
|
|
|
class TumblrOauthClient(object):
|
|
class TumblrOauthClient(object):
|
|
|
-
|
|
|
|
|
|
|
+ '''
|
|
|
|
|
+ Class responsible for authenticating the user
|
|
|
|
|
+ via Tumblr OAuth2.
|
|
|
|
|
+ '''
|
|
|
token = None
|
|
token = None
|
|
|
oauth_verifier = None
|
|
oauth_verifier = None
|
|
|
oauth_token = None
|
|
oauth_token = None
|
|
|
oauth_token_secret = None
|
|
oauth_token_secret = None
|
|
|
accessed = False
|
|
accessed = False
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
def __init__(self, consumer_key, consumer_secret):
|
|
def __init__(self, consumer_key, consumer_secret):
|
|
|
self.consumer_key = consumer_key
|
|
self.consumer_key = consumer_key
|
|
|
self.consumer_secret = consumer_secret
|
|
self.consumer_secret = consumer_secret
|
|
|
self.consumer = oauth2.Consumer(consumer_key, consumer_secret)
|
|
self.consumer = oauth2.Consumer(consumer_key, consumer_secret)
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
def authorize_url(self):
|
|
def authorize_url(self):
|
|
|
|
|
+ '''
|
|
|
|
|
+ Redirects user to authorize use.
|
|
|
|
|
+ '''
|
|
|
client = oauth2.Client(self.consumer)
|
|
client = oauth2.Client(self.consumer)
|
|
|
- resp, content = client.request(request_token_url, "GET")
|
|
|
|
|
|
|
+ content = client.request(request_token_url, "GET")
|
|
|
#parse content
|
|
#parse content
|
|
|
if not self.oauth_token:
|
|
if not self.oauth_token:
|
|
|
request_token = dict(urlparse.parse_qsl(content))
|
|
request_token = dict(urlparse.parse_qsl(content))
|
|
|
- self.oauth_token = request_token['oauth_token'] #'QBXdeeMKAnLzDbIG7dDNewTzRYyQoHZLbcn3bAFTCEFF5EXurl' #
|
|
|
|
|
- self.oauth_token_secret = request_token['oauth_token_secret']#'u10SuRl2nzS8vFK4K7UPQexAvbIFBFrZBjA79XDlgoXFxv9ZhO' #
|
|
|
|
|
- link = authorize_url+"?oauth_token="+self.oauth_token+"&redirect_uri=http%3A%2F%2Flocalhost%3A8000/hackathon/tumblr"
|
|
|
|
|
|
|
+ self.oauth_token = request_token['oauth_token']
|
|
|
|
|
+ self.oauth_token_secret = request_token['oauth_token_secret']
|
|
|
|
|
+ link = authorize_url + "?oauth_token=" + self.oauth_token + \
|
|
|
|
|
+ "&redirect_uri=http%3A%2F%2Flocalhost%3A8000/hackathon/tumblr"
|
|
|
return link
|
|
return link
|
|
|
|
|
|
|
|
|
|
|
|
|
def access_token_url(self, oauth_verifier=''):
|
|
def access_token_url(self, oauth_verifier=''):
|
|
|
|
|
+ '''
|
|
|
|
|
+ Returns an access token to the user.
|
|
|
|
|
+ '''
|
|
|
self.accessed = True
|
|
self.accessed = True
|
|
|
token = oauth2.Token(self.oauth_token, self.oauth_token_secret)
|
|
token = oauth2.Token(self.oauth_token, self.oauth_token_secret)
|
|
|
self.oauth_verifier = oauth_verifier
|
|
self.oauth_verifier = oauth_verifier
|
|
|
print self.oauth_verifier
|
|
print self.oauth_verifier
|
|
|
token.set_verifier(self.oauth_verifier)
|
|
token.set_verifier(self.oauth_verifier)
|
|
|
client = oauth2.Client(self.consumer, token)
|
|
client = oauth2.Client(self.consumer, token)
|
|
|
- resp, content = client.request(access_token_url,"POST")
|
|
|
|
|
|
|
+ content = client.request(access_token_url, "POST")
|
|
|
access_token = dict(urlparse.parse_qsl(content))
|
|
access_token = dict(urlparse.parse_qsl(content))
|
|
|
#set verified token
|
|
#set verified token
|
|
|
self.token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
|
|
self.token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
|
|
|
- #print self.token
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
def getUserInfo(self):
|
|
def getUserInfo(self):
|
|
|
''' Returns users information. '''
|
|
''' Returns users information. '''
|
|
@@ -79,14 +91,9 @@ class TumblrOauthClient(object):
|
|
|
''' Returns blogger's blog information '''
|
|
''' Returns blogger's blog information '''
|
|
|
blog_info = blog_uri + user + ".tumblr.com/info?api_key="+self.consumer_key
|
|
blog_info = blog_uri + user + ".tumblr.com/info?api_key="+self.consumer_key
|
|
|
req = requests.get(blog_info)
|
|
req = requests.get(blog_info)
|
|
|
-
|
|
|
|
|
- #if int(req.status_code) != 200:
|
|
|
|
|
- # raise Exception("Invalid response %s." % resp['status'])
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
jsonlist = json.loads(req.content)
|
|
jsonlist = json.loads(req.content)
|
|
|
response = jsonlist['response']
|
|
response = jsonlist['response']
|
|
|
- blog = response['blog']
|
|
|
|
|
|
|
+ blog = response['blog']
|
|
|
blog['updated'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(blog['updated']))
|
|
blog['updated'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(blog['updated']))
|
|
|
return blog
|
|
return blog
|
|
|
|
|
|
|
@@ -94,13 +101,11 @@ class TumblrOauthClient(object):
|
|
|
def getTaggedInfo(self, tag):
|
|
def getTaggedInfo(self, tag):
|
|
|
''' Return tags related to blog with certain tag. '''
|
|
''' Return tags related to blog with certain tag. '''
|
|
|
|
|
|
|
|
- tagged_uri = "http://api.tumblr.com/v2/tagged?tag="+tag+"&api_key="+self.consumer_key+"&limit=20"
|
|
|
|
|
|
|
+ tagged_uri = "http://api.tumblr.com/v2/tagged?tag=" + tag + "&api_key=" + \
|
|
|
|
|
+ self.consumer_key + "&limit=20"
|
|
|
req = requests.get(tagged_uri)
|
|
req = requests.get(tagged_uri)
|
|
|
jsonlist = json.loads(req.content)
|
|
jsonlist = json.loads(req.content)
|
|
|
-
|
|
|
|
|
tags = []
|
|
tags = []
|
|
|
-
|
|
|
|
|
- meta = jsonlist['meta']
|
|
|
|
|
body = jsonlist['response']
|
|
body = jsonlist['response']
|
|
|
for blog in body:
|
|
for blog in body:
|
|
|
for data in blog:
|
|
for data in blog:
|
|
@@ -109,19 +114,18 @@ class TumblrOauthClient(object):
|
|
|
for i in blog[data]:
|
|
for i in blog[data]:
|
|
|
m = re.match("(.*)(s*)s(t*)t(a*)a(r*)r(b*)b(u*)u(c*)c(k*)k(.*)", i.lower())
|
|
m = re.match("(.*)(s*)s(t*)t(a*)a(r*)r(b*)b(u*)u(c*)c(k*)k(.*)", i.lower())
|
|
|
if not m:
|
|
if not m:
|
|
|
- tags.append(i)
|
|
|
|
|
|
|
+ tags.append(i)
|
|
|
|
|
|
|
|
- return tags
|
|
|
|
|
|
|
+ return tags
|
|
|
|
|
|
|
|
|
|
|
|
|
def getTaggedBlog(self, tag):
|
|
def getTaggedBlog(self, tag):
|
|
|
''' Return the tagged blogs's captions or post.'''
|
|
''' Return the tagged blogs's captions or post.'''
|
|
|
-
|
|
|
|
|
- tagged_uri = "http://api.tumblr.com/v2/tagged?tag="+tag+"&api_key="+self.consumer_key+"&limit=20"
|
|
|
|
|
|
|
+
|
|
|
|
|
+ tagged_uri = "http://api.tumblr.com/v2/tagged?tag=" + tag + "&api_key=" + \
|
|
|
|
|
+ self.consumer_key + "&limit=20"
|
|
|
req = requests.get(tagged_uri)
|
|
req = requests.get(tagged_uri)
|
|
|
jsonlist = json.loads(req.content)
|
|
jsonlist = json.loads(req.content)
|
|
|
-
|
|
|
|
|
- meta = jsonlist['meta']
|
|
|
|
|
body = jsonlist['response']
|
|
body = jsonlist['response']
|
|
|
|
|
|
|
|
tagtext = []
|
|
tagtext = []
|
|
@@ -141,7 +145,7 @@ class TumblrOauthClient(object):
|
|
|
if blog[data]:
|
|
if blog[data]:
|
|
|
#print blog[data]
|
|
#print blog[data]
|
|
|
soup = BeautifulSoup(blog[data])
|
|
soup = BeautifulSoup(blog[data])
|
|
|
- text = soup.get_text()
|
|
|
|
|
|
|
+ text = soup.get_text()
|
|
|
tagtext.append(text)
|
|
tagtext.append(text)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return tagtext
|
|
return tagtext
|