diff --git a/hackathon_starter/hackathon/scripts/instagram.py b/hackathon_starter/hackathon/scripts/instagram.py new file mode 100644 index 0000000..cb5fe62 --- /dev/null +++ b/hackathon_starter/hackathon/scripts/instagram.py @@ -0,0 +1,60 @@ +import requests +import urllib +import urllib2 +import json +import simplejson as json2 + +authorization_url = 'https://api.instagram.com/oauth/authorize/?client_id=' +access_token_url = 'https://api.instagram.com/oauth/access_token' + +class InstagramOauthClient(object): + + access_token = None + user_data = None + + def __init__(self, client_id, client_secret): + self.client_id = client_id + self.client_secret = client_secret + + def get_authorize_url(self): + ''' Obtains the authorization url. ''' + auth_url = authorization_url + self.client_id +'&redirect_uri=http://localhost:8000/hackathon/instagram&response_type=code' + return auth_url + + def get_access_token(self, code): + ''' Obtains access token. ''' + + auth_setting = {'client_id': self.client_id, + 'client_secret': self.client_secret, + 'grant_type': 'authorization_code', + 'redirect_uri': 'http://localhost:8000/hackathon/instagram', + 'code': code + } + + auth_setting_url = urllib.urlencode(auth_setting) + req = urllib2.Request(access_token_url, auth_setting_url) + content = urllib2.urlopen(req) + jsonlist = json.load(content) + self.access_token = jsonlist['access_token'] + self.user_data = jsonlist['user'] + print self.access_token + + + def get_tagged_media(self, tag): + ''' Get recent tagged media. ''' + tagged_media_url = 'https://api.instagram.com/v1/tags/'+tag+'/media/recent?access_token='+self.access_token# +'&count=2' + req = requests.get(tagged_media_url) + content = json2.loads(req.content) + data = content['data'] + + while len(data) <= 100: + next_url= content['pagination']['next_url'] + req = requests.get(next_url) + content = json2.loads(req.content) + for i in content['data']: + data.append(i) + print len(data) + return data + + + diff --git a/hackathon_starter/hackathon/scripts/tumblr.py b/hackathon_starter/hackathon/scripts/tumblr.py index f6d4481..276e572 100644 --- a/hackathon_starter/hackathon/scripts/tumblr.py +++ b/hackathon_starter/hackathon/scripts/tumblr.py @@ -10,59 +10,54 @@ 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/" class TumblrOauthClient(object): token = None - oauth_token_secret = None oauth_verifier = None + oauth_token = None + oauth_token_secret = None + accessed = False - def __init__(self, consumer_key, consumer_secret, oauth_token =''): + + def __init__(self, consumer_key, consumer_secret): self.consumer_key = consumer_key self.consumer_secret = consumer_secret self.consumer = oauth2.Consumer(consumer_key, consumer_secret) - self.oauth_token = oauth_token + - def get_authorize_url(self): + def authorize_url(self): client = oauth2.Client(self.consumer) resp, content = client.request(request_token_url, "GET") - - #if int(resp['status']) != 200: - # raise Exception("Invalid response %s." % resp['status']) - #parse content if not self.oauth_token: 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' - + 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" return link -#""" - def get_access_token_url(self, oauth_verifier): - #print "verifier" - self.oauth_verifier = oauth_verifier + def access_token_url(self, oauth_verifier=''): + self.accessed = True token = oauth2.Token(self.oauth_token, self.oauth_token_secret) + self.oauth_verifier = oauth_verifier + print self.oauth_verifier token.set_verifier(self.oauth_verifier) - client = oauth2.Client(self.consumer, token) resp, content = client.request(access_token_url,"POST") - - #print resp['status'] - access_token = dict(urlparse.parse_qsl(content)) - #print access_token - #set verified token self.token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret']) + #print self.token + + - def getUserInfo(self): ''' Returns users information. ''' client = oauth2.Client(self.consumer, self.token) @@ -75,25 +70,27 @@ def getUserInfo(self): jsonlist = json.loads(content) response = jsonlist['response'] user_info = response['user'] + total_blogs = len(user_info['blogs']) #print user_info - return user_info + return user_info, total_blogs + def getBlogInfo(self, user): ''' Returns blogger's blog information ''' blog_info = blog_uri + user + ".tumblr.com/info?api_key="+self.consumer_key req = requests.get(blog_info) - if int(req.status_code) != 200: - raise Exception("Invalid response %s." % resp['status']) + #if int(req.status_code) != 200: + # raise Exception("Invalid response %s." % resp['status']) jsonlist = json.loads(req.content) response = jsonlist['response'] blog = response['blog'] blog['updated'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(blog['updated'])) - return blog + def getTaggedInfo(self, tag): ''' Return tags related to blog with certain tag. ''' @@ -116,6 +113,7 @@ def getTaggedInfo(self, tag): return tags + def getTaggedBlog(self, tag): ''' Return the tagged blogs's captions or post.''' @@ -147,4 +145,3 @@ def getTaggedBlog(self, tag): tagtext.append(text) return tagtext -#""" \ No newline at end of file diff --git a/hackathon_starter/hackathon/templates/hackathon/api_examples.html b/hackathon_starter/hackathon/templates/hackathon/api_examples.html index a786380..9caace5 100644 --- a/hackathon_starter/hackathon/templates/hackathon/api_examples.html +++ b/hackathon_starter/hackathon/templates/hackathon/api_examples.html @@ -9,6 +9,7 @@
+ diff --git a/hackathon_starter/hackathon/templates/hackathon/instagram.html b/hackathon_starter/hackathon/templates/hackathon/instagram.html new file mode 100644 index 0000000..8abae4c --- /dev/null +++ b/hackathon_starter/hackathon/templates/hackathon/instagram.html @@ -0,0 +1,17 @@ + + + + {% include 'hackathon/base.html' %} +Name | +{{userinfo.name}} | +
---|---|
Total Blogs | +{{total_blog}} | +