<s> '''<STR_LIT>''' <EOL> import requests <EOL> import urllib <EOL> import urllib2 <EOL> import json <EOL> import simplejson as json2 <EOL> import googlemaps <EOL> from django . conf import settings <EOL> from datetime import datetime <EOL> AUTHORIZATION_URL = '<STR_LIT>' <EOL> ACCESS_TOKEN_URL = '<STR_LIT>' <EOL> class InstagramOauthClient ( object ) : <EOL> '''<STR_LIT>''' <EOL> access_token = None <EOL> user_data = None <EOL> is_authorized = False <EOL> def __init__ ( self , client_id , client_secret ) : <EOL> '''<STR_LIT>''' <EOL> self . client_id = client_id <EOL> self . client_secret = client_secret <EOL> def get_authorize_url ( self ) : <EOL> '''<STR_LIT>''' <EOL> redirectUri = '<STR_LIT>' <EOL> authURL = AUTHORIZATION_URL + self . client_id + redirectUri <EOL> return authURL <EOL> def get_access_token ( self , code ) : <EOL> '''<STR_LIT>''' <EOL> authSetting = { <EOL> '<STR_LIT>' : self . client_id , <EOL> '<STR_LIT>' : self . client_secret , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : code } <EOL> authSettingUrl = urllib . urlencode ( authSetting ) <EOL> req = urllib2 . Request ( ACCESS_TOKEN_URL , authSettingUrl ) <EOL> content = urllib2 . urlopen ( req ) <EOL> jsonlist = json . load ( content ) <EOL> self . access_token = jsonlist [ '<STR_LIT>' ] <EOL> self . user_data = jsonlist [ '<STR_LIT:user>' ] <EOL> self . is_authorized = True <EOL> def searchForLocation ( address ) : <EOL> '''<STR_LIT>''' <EOL> gmaps = googlemaps . Client ( key = settings . GOOGLEMAP_API_KEY ) <EOL> geocodeResult = gmaps . geocode ( address ) <EOL> if geocodeResult : <EOL> location = geocodeResult [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT:location>' ] <EOL> return location <EOL> def getTaggedMedia ( tag , accessToken ) : <EOL> '''<STR_LIT>''' <EOL> tagUri = '<STR_LIT>' <EOL> taggedMediaUrl = tagUri + tag + '<STR_LIT>' + accessToken <EOL> req = requests . get ( taggedMediaUrl ) <EOL> content = json2 . loads ( req . content ) <EOL> data = content [ '<STR_LIT:data>' ] <EOL> while len ( data ) <= <NUM_LIT:100> : <EOL> nextUrl = content [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> req = requests . get ( nextUrl ) <EOL> content = json2 . loads ( req . content ) <EOL> for i in content [ '<STR_LIT:data>' ] : <EOL> data . append ( i ) <EOL> return data <EOL> def getUserInfo ( accessToken ) : <EOL> '''<STR_LIT>''' <EOL> userInfo = '<STR_LIT>' + accessToken <EOL> req = requests . get ( userInfo ) <EOL> content = json2 . loads ( req . content ) <EOL> data = content [ '<STR_LIT:data>' ] <EOL> return data <EOL> def getUserMedia ( userId , accessToken ) : <EOL> '''<STR_LIT>''' <EOL> userMediaUri = '<STR_LIT>' + str ( userId ) <EOL> userMedia = userMediaUri + '<STR_LIT>' + accessToken <EOL> req = requests . get ( userMedia ) <EOL> content = json2 . loads ( req . content ) <EOL> data = content [ '<STR_LIT:data>' ] <EOL> return data <EOL> def searchLocationIds ( lat , lng , accessToken ) : <EOL> '''<STR_LIT>''' <EOL> locIdUri = '<STR_LIT>' + str ( lat ) <EOL> location = locIdUri + '<STR_LIT>' + str ( lng ) + '<STR_LIT>' + accessToken + '<STR_LIT>' <EOL> req = requests . get ( location ) <EOL> data = json2 . loads ( req . content ) <EOL> listOfIds = [ ] <EOL> if data [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] != <NUM_LIT:200> : <EOL> raise Exception ( "<STR_LIT>" % data [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] ) <EOL> searchIds = data [ '<STR_LIT:data>' ] <EOL> for data in searchIds : <EOL> for i in data : <EOL> if i == '<STR_LIT:id>' : <EOL> listOfIds . append ( data [ i ] ) <EOL> return listOfIds <EOL> def searchLocationMedia ( listOfLocationIds , accessToken ) : <EOL> '''<STR_LIT>''' <EOL> media = [ ] <EOL> locationUri = '<STR_LIT>' <EOL> for location in listOfLocationIds : <EOL> mediaByLocation = locationUri + location + '<STR_LIT>' + accessToken <EOL> req = requests . get ( mediaByLocation ) <EOL> contentAll = json2 . loads ( req . content ) <EOL> if contentAll [ '<STR_LIT>' ] : <EOL> tempMedia = [ ] <EOL> nextUrl = contentAll [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> req = requests . get ( nextUrl ) <EOL> content = json2 . loads ( req . content ) <EOL> for i in content [ '<STR_LIT:data>' ] : <EOL> i [ '<STR_LIT>' ] = datetime . fromtimestamp ( int ( i [ '<STR_LIT>' ] ) ) <EOL> i [ '<STR_LIT>' ] = i [ '<STR_LIT>' ] . strftime ( '<STR_LIT>' ) <EOL> tempMedia . append ( i ) <EOL> media += [ tempMedia ] <EOL> else : <EOL> for i in contentAll [ '<STR_LIT:data>' ] : <EOL> for data in i : <EOL> if data == '<STR_LIT>' : <EOL> i [ data ] = datetime . fromtimestamp ( int ( i [ data ] ) ) <EOL> i [ data ] = i [ data ] . strftime ( '<STR_LIT>' ) <EOL> media . append ( contentAll [ '<STR_LIT:data>' ] ) <EOL> return media </s>
