-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitHubCrawler.py
More file actions
139 lines (109 loc) · 3.42 KB
/
gitHubCrawler.py
File metadata and controls
139 lines (109 loc) · 3.42 KB
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
133
134
135
136
137
138
139
import requests
from bs4 import BeautifulSoup
import time
import numpy
import pandas as pd
def emojiDecode(description):
output = (description
.encode('utf-16', 'surrogatepass')
.decode('utf-16')
.encode("raw_unicode_escape")
.decode("latin_1")
)
return output
def get_page_repo_list(URL,page):
page = str(page)
url_to_call = URL.format(page)
response = requests.get(url_to_call, headers = {'User-Agent': "Mozilla/5.0"})
if response.status_code != 200:
print('connection failed')
return
html_content = response.content
dom = BeautifulSoup(html_content, 'html.parser')
repoList = dom.select("ul.repo-list li")
return repoList
def is_int(temp):
try:
int(temp)
return 1
except:
return 0
def is_Star(temp):
if "." in temp or is_int(temp):
return 1
else:
return 0
def is_lang(temp):
langs = ['Jupyter Notebook','Python','R','HTML','JavaScript','Java','CSS','Rebol','TeX','Shell']
return temp in langs
def is_license(temp):
return "Updated" not in temp
def get_elements(section):
al = section.find_all("div", {"class" : "mr-3"})
if len(al) == 4:
return [div.get_text().replace("\n", "").strip() for div in al]
result = [0,0,0,0]
for div in al:
temp = div.get_text().replace("\n", "").strip()
if is_Star(temp):
result[0] = temp
pass
elif is_lang(temp):
result[1] = temp
pass
elif is_license(temp):
result[2] = temp
pass
else:
result[3] = temp
return result
def clean_stars(stars):
stars = str(stars)
if '.' in stars:
stars = stars.replace(".", "").replace("k", "00")
stars = stars.replace("k", "000")
return stars
def clean_elements(elements):
elements[0] = clean_stars(elements[0])
elements[3] = elements[3].replace("Updated", "").strip()
return elements
def extractData(repo):
name = repo.a.attrs["href"]
try:
description = repo.p.get_text().replace("\n", "").strip()
except:
description = "NA"
description = emojiDecode(description)
section = repo.find("div", {"class" : ""})
features = get_elements(section)
features = clean_elements(features)
features.insert(0,name)
features.insert(1,description)
return features
def get_URLs():
f = open("tempURLS.txt", "r")
listURLs = [x for x in f]
return listURLs
def save_data(data,URLCOUNT,count):
a = numpy.asarray(data)
cols = ["URLCODE","Name", "Descr","Stars","MajLang","License","LastUpdated"]
pd.DataFrame(a).to_csv("GitHub_DS_Repos{}-{}.csv".format(URLCOUNT,count), header=cols)
def driver():
URLCOUNT = 1
for URL in get_URLs():
data = []
count = 0
for page in range(1,101):
time.sleep(20)
repoList = get_page_repo_list(URL,page)
for repo in repoList:
temp = extractData(repo)
temp.insert(0,URLCOUNT)
data.append(temp)
print(URLCOUNT,page)
if page % 5 == 0:
count += 1
save_data(data,URLCOUNT,count)
URLCOUNT += 1
if __name__ == "__main__":
driver()