-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.cgi
executable file
·51 lines (39 loc) · 1.11 KB
/
api.cgi
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
#!/usr/bin/python
from py.InstagramController import InstagramController
from traceback import format_exc # Stack traces
from cgi import FieldStorage # Query keys
from json import dumps
def get_keys():
""" Retrieves key/value pairs from query, puts in dict """
form = FieldStorage()
keys = {}
for key in form.keys():
keys[key] = form[key].value
return keys
def main():
keys = get_keys()
if 'method' not in keys:
raise Exception("Required 'method' not found")
# Recent
if keys['method'] == 'recent':
return dumps(InstagramController.get_recent())
if 'user' not in keys:
raise Exception("Required 'user' not found")
controller = InstagramController(keys['user'].lower())
# Requeting a user for the first time
if keys['method'] == 'init':
return controller.user.serialize()
# Asking to fetch more photos for a user
elif keys['method'] == 'more':
return dumps(controller.get_posts())
if __name__ == '__main__':
print "Content-Type: application/json"
print ""
try:
print main()
except Exception, e:
print dumps({
'error': str(e),
'stack': str(format_exc())
})
print "\n\n"