<s> '''<STR_LIT>''' <EOL> import requests <EOL> import simplejson as json <EOL> import time <EOL> import re <EOL> from bs4 import BeautifulSoup <EOL> import urlparse <EOL> import oauth2 <EOL> request_token_url = '<STR_LIT>' <EOL> authorize_url = '<STR_LIT>' <EOL> access_token_url = '<STR_LIT>' <EOL> user_uri = "<STR_LIT>" <EOL> blog_uri = "<STR_LIT>" <EOL> class TumblrOauthClient ( object ) : <EOL> '''<STR_LIT>''' <EOL> token = None <EOL> oauth_verifier = None <EOL> oauth_token = None <EOL> oauth_token_secret = None <EOL> is_authorized = False <EOL> access_token = None <EOL> def __init__ ( self , consumer_key , consumer_secret ) : <EOL> self . consumer_key = consumer_key <EOL> self . consumer_secret = consumer_secret <EOL> self . consumer = oauth2 . Consumer ( consumer_key , consumer_secret ) <EOL> def authorize_url ( self ) : <EOL> '''<STR_LIT>''' <EOL> client = oauth2 . Client ( self . consumer ) <EOL> resp , content = client . request ( request_token_url , "<STR_LIT:GET>" ) <EOL> if not self . oauth_token : <EOL> request_token = dict ( urlparse . parse_qsl ( content ) ) <EOL> self . oauth_token = request_token [ '<STR_LIT>' ] <EOL> self . oauth_token_secret = request_token [ '<STR_LIT>' ] <EOL> link = authorize_url + "<STR_LIT>" + self . oauth_token + "<STR_LIT>" <EOL> return link <EOL> def access_token_url ( self , oauth_verifier = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . is_authorized = True <EOL> token = oauth2 . Token ( self . oauth_token , self . oauth_token_secret ) <EOL> self . oauth_verifier = oauth_verifier <EOL> print self . oauth_verifier <EOL> token . set_verifier ( self . oauth_verifier ) <EOL> client = oauth2 . Client ( self . consumer , token ) <EOL> resp , content = client . request ( access_token_url , "<STR_LIT:POST>" ) <EOL> self . access_token = dict ( urlparse . parse_qsl ( content ) ) <EOL> self . token = oauth2 . Token ( self . access_token [ '<STR_LIT>' ] , self . access_token [ '<STR_LIT>' ] ) <EOL> def getUserInfo ( self ) : <EOL> '''<STR_LIT>''' <EOL> client = oauth2 . Client ( self . consumer , self . token ) <EOL> resp , content = client . request ( user_uri , "<STR_LIT:POST>" ) <EOL> if int ( resp [ '<STR_LIT:status>' ] ) != <NUM_LIT:200> : <EOL> raise Exception ( "<STR_LIT>" % resp [ '<STR_LIT:status>' ] ) <EOL> jsonlist = json . loads ( content ) <EOL> response = jsonlist [ '<STR_LIT>' ] <EOL> self . username = str ( response [ '<STR_LIT:user>' ] [ '<STR_LIT:name>' ] ) <EOL> user_info = response [ '<STR_LIT:user>' ] <EOL> total_blogs = len ( user_info [ '<STR_LIT>' ] ) <EOL> return user_info , total_blogs <EOL> def getBlogInfo ( self , user ) : <EOL> '''<STR_LIT>''' <EOL> blog_info = blog_uri + user + "<STR_LIT>" + self . consumer_key <EOL> req = requests . get ( blog_info ) <EOL> jsonlist = json . loads ( req . content ) <EOL> response = jsonlist [ '<STR_LIT>' ] <EOL> blog = response [ '<STR_LIT>' ] <EOL> blog [ '<STR_LIT>' ] = time . strftime ( '<STR_LIT>' , time . localtime ( blog [ '<STR_LIT>' ] ) ) <EOL> return blog <EOL> def getTaggedInfo ( self , tag ) : <EOL> '''<STR_LIT>''' <EOL> tagged_uri = "<STR_LIT>" + tag + "<STR_LIT>" + self . consumer_key + "<STR_LIT>" <EOL> req = requests . get ( tagged_uri ) <EOL> jsonlist = json . loads ( req . content ) <EOL> tags = [ ] <EOL> body = jsonlist [ '<STR_LIT>' ] <EOL> for blog in body : <EOL> for data in blog : <EOL> if data == "<STR_LIT>" : <EOL> for i in blog [ data ] : <EOL> m = re . match ( "<STR_LIT>" , i . lower ( ) ) <EOL> if not m : <EOL> tags . append ( i ) <EOL> return tags <EOL> def getTaggedBlog ( self , tag ) : <EOL> '''<STR_LIT>''' <EOL> tagged_uri = "<STR_LIT>" + tag + "<STR_LIT>" + self . consumer_key + "<STR_LIT>" <EOL> req = requests . get ( tagged_uri ) <EOL> jsonlist = json . loads ( req . content ) <EOL> body = jsonlist [ '<STR_LIT>' ] <EOL> tagtext = [ ] <EOL> for blog in body : <EOL> for data in blog : <EOL> if data == "<STR_LIT:body>" : <EOL> if blog [ data ] : <EOL> soup = BeautifulSoup ( blog [ data ] ) <EOL> text = soup . get_text ( ) <EOL> tagtext . append ( text ) <EOL> if data == "<STR_LIT>" : <EOL> if blog [ data ] : <EOL> soup = BeautifulSoup ( blog [ data ] ) <EOL> text = soup . get_text ( ) <EOL> tagtext . append ( text ) <EOL> return tagtext </s>
