-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_gist_info.py
78 lines (63 loc) · 1.79 KB
/
crawl_gist_info.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
#__author__ = "Ramesh Chandra"
import urllib
import json
import re
#This script make an API call to github
#json response gist.json
#for more info . https://api.github.com/users/rameshchandrasiraura/gists
def hitAPI(url):
#create http request
response = urllib.urlopen(url)
#get data from json response
data = json.loads(response.read())
#print data
return data
def storeJSON(data):
#write json response to text file
filename = "gist_info.json"
#json in proper format
jsondata = json.dumps(data, indent=4, skipkeys=True, sort_keys=True)
#open file to write
fd = open(filename, 'w')
#to convert ascii
jsondata = jsondata.decode('utf-8')
#write jsondata to file
fd.write(jsondata)
#close file
fd.close()
#check validity of github handle
def get_handle(attempt=0):
attempt += 1
#Prepare URLs with github handle
print '\nEnter your github handle'
handle = raw_input()
#print 'rameshchandrasiraura'
#handle = 'rameshchandrasiraura'
git_user_name_regex = '^\w+-?\w+(?!-)$'
if re.search(git_user_name_regex,handle):
print 'Valid Handle : ',handle
return handle
else:
print 'Invalid Handle :',handle
if attempt>3:
return ''
get_handle(attempt)
#driver function
def main():
print "Getting Url of your gists..."
handle = get_handle()
if handle=='':
print 'Script stopped'
print 'more than 3 attempts with invalid handle'
exit()
url = "https://api.github.com/users/"+handle+"/gists"
print 'getting gists url...'
#create API CALL
data = hitAPI(url)
#Store JSON into File
storeJSON(data)
print "Done"
#main function
if __name__ == "__main__":
main()