-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstagramAPI.py
63 lines (52 loc) · 2.02 KB
/
InstagramAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from lib.instagram_lbs.instagram import client, subscriptions
from lib.instagram_lbs.instagram.client import InstagramAPI
import lib.instagram_lbs.instagram.simplejson as json
import urllib2
class Instagram_API(object):
def __init__(self,client_id,client_secret):
self.client_id=client_id
self.client_secret=client_secret
self.api = InstagramAPI(self.client_id, self.client_secret)
## Gets the user ID from the given username
def getUserID(self,username):
url='https://api.instagram.com/v1/users/search?q='+username+'&client_id='+self.client_id
response = urllib2.urlopen(url)
html = response.read()
dct=json.loads(html)
return dct['data'][0]['id']
#html=html.split('"')
#return html[len(html)-2]
## Method to get the photos from a keyword
def getPhotos(self, keyword, amount, last_photoid, new=True):
photos=[]
if keyword[0]=="@":
keyword=keyword.split('@',1)
uid=self.getUserID(keyword[1])
if new == True:
user_media = self.api.user_recent_media(user_id=uid, count=amount, min_id=last_photoid)
else:
user_media = self.api.user_recent_media(user_id=uid, count=amount, max_id=last_photoid)
url=user_media[1]
response = urllib2.urlopen(url)
html = response.read()
dct=json.loads(html)
for image in dct['data']:
url= image['images']['standard_resolution']['url']
print url
return dct
elif keyword[0]=="#":
keyword=keyword.split('#',1)
if new == True:
hash_media = self.api.tag_recent_media(count=amount, min_id=last_photoid, tag_name=keyword[1])
else:
hash_media = self.api.tag_recent_media(count=amount, max_id=last_photoid, tag_name=keyword[1])
url=hash_media[1]
response = urllib2.urlopen(url)
html = response.read()
dct=json.loads(html)
for image in dct['data']:
url= image['images']['standard_resolution']['url']
print url
return dct
else:
print "Error, you have to provide either an @username or an #hashtag"