-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecGitStars.py
132 lines (110 loc) · 3.75 KB
/
specGitStars.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os, re, sys, json
import ssl
import requests.packages.urllib3.util.ssl_
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL'
#count file output
outCountFile = 'outCount.xls'
lastopenfile = ''
#if count fix num
iscountvalid = 1
outPutNum = 10
#specific index
global fromIndex
#index
count = 0
#match the star text
def matchStarText(text):
find = 0
searchObj = re.search( r'.*stargazers">.*', text, re.M|re.I)
if searchObj:
find = 1
return find
#match the star text
def matchForkText(text):
find = 0
searchObj = re.search( r'.*class="social-count">.*', text, re.M|re.I)
if searchObj:
find = 1
return find
#drop the special char in line nums
def formatNumber(text):
text = text.strip()
text = text.replace(',','')
return text
#open spec file read git url
def openAndReadGitUrl(path,count):
json_file = open(path, "r")
json_data = json.load(json_file)
gitname = json_data["name"]
giturl = 'none'
if 'git' in json_data["source"]:
giturl = json_data["source"]["git"]
print ("%s\t%s" %(gitname,giturl))
if giturl != 'none' and giturl.startswith('http'):
gitsummary = 'none'
if 'summary' in json_data:
gitsummary = json_data['summary'].replace('\n','')
gitDescription = 'none'
if 'description' in json_data:
gitDescription = json_data['description'].replace('\n','')
gitstar = '-'
gitfork = '-'
try:
fp = requests.get(giturl,timeout=15)
if fp.status_code == requests.codes.ok:
match = 0
for line in fp.iter_lines():
line = line.decode('utf-8')
if match == 1:
gitstar = formatNumber(line)
match = 0
if match == 2:
gitfork = formatNumber(line)
break
if matchStarText(line) == 1:
match = 1
if matchForkText(line) == 1:
match = 2
out = '%d\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n' %(count,gitname,gitstar,gitfork,giturl,gitsummary,gitDescription)
out = out.encode('ascii', 'ignore')
print (out.decode('utf-8'))
file = open(outCountFile,'a')
out = out.decode('utf-8')
file.write(out)
except requests.exceptions.HTTPError as e:
print ("ERROR:%s" %(e))
except requests.exceptions.Timeout as e:
print ("ERROR:%s" %(e))
except requests.exceptions.RequestException as e:
print ("ERROR:%s" %(e))
# print all spec file
def listFileList(rootDir):
global lastopenfile, count
for file in os.listdir(rootDir):
if iscountvalid and count >= outPutNum:
break
path = os.path.join(rootDir, file)
if os.path.isdir(path):
dirs = os.listdir(path)
newpath = os.path.join(path, dirs[len(dirs)-1])
if os.path.isdir(newpath):
listFileList(newpath)
else:
if (count+1) >= fromIndex:
openAndReadGitUrl(path, count+1)
else:
print ("index %d was jumped...fromIndex is %d" %(count+1,fromIndex))
count += 1
if __name__ == '__main__':
argc = len(sys.argv)
if argc == 2:
if os.path.exists(outCountFile):
os.remove(outCountFile)
fromIndex = 1
listFileList(sys.argv[1])
else:
if argc == 3:
fromIndex = int(sys.argv[2])
listFileList(sys.argv[1])
else:
print ('input error, please input path like (python specGitStars.py ~/.cocoapods/repos/master/Specs)')