-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
113 lines (87 loc) · 3.33 KB
/
helper.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
from linkpredict import rooted_pagerank
from bs4 import BeautifulSoup
import requests as rq
import networkx as nx
import os
import csv
def build_graph(root, graphsize = 1000):
visited = set()
unvisited = [root]
gitgraph = nx.DiGraph()
gitgraph.add_node(root)
filename = "data/" + str(root) + "_graph.txt"
if os.path.isfile(filename):
fp = open(filename, "r")
reader = csv.reader(fp, delimiter = "\t")
for line in reader:
gitgraph.add_edge(line[0], line[1])
fp.close()
return gitgraph
url1 = "https://github.com/" + root
userpage = rq.get(url1)
if userpage.status_code != 200:
return False
while len(gitgraph) < graphsize and len(unvisited) != 0:
newnode = unvisited[0]
url2 = "https://github.com/" + newnode + "/following"
# print("Visiting " + url2)
resource = rq.get(url2)
follower_raw = resource.text
follower_soup = BeautifulSoup(follower_raw)
preprocess_list = follower_soup.find_all("a", class_ = "d-inline-block")
try:
names = [user['href'][1:] for user in preprocess_list]
# print(names)
for amala in names[2:]:
gitgraph.add_edge(newnode, amala)
if amala not in visited:
unvisited.append(amala)
except:
print("Unable to find folowees for " + newnode)
visited.add(unvisited.pop(0))
url = "https://github.com/" + newnode + "/followers"
# print("Visiting " + url)
resource = rq.get(url)
follower_raw = resource.text
follower_soup = BeautifulSoup(follower_raw)
preprocess_list = follower_soup.find_all("a", class_ = "d-inline-block")
try:
names = [user['href'][1:] for user in preprocess_list]
# print(names)
for hrudaya in names[2:]:
gitgraph.add_edge(hrudaya, newnode)
if hrudaya not in visited:
unvisited.append(hrudaya)
except:
print("Unable to find folowers for " + newnode)
github_graph = list(gitgraph.edges())
links = open(filename, "w")
for link in github_graph:
links.write(link[0] + "\t" + link[1] + "\n")
links.close()
return gitgraph
def get_recommendations(graph, node, n = 10):
recommendation_list = rooted_pagerank(graph, node, 0.3, 1e-6)
get_val = lambda key: recommendation_list[key]
top_recommendations = sorted(recommendation_list.keys(), key = get_val, reverse = True)
print("Cleared Checkpoint")
recommends = []
found = 0
i = 0
while len(recommends) < n:
if i < len(top_recommendations):
if not graph.has_edge(node, top_recommendations[i]) and node != top_recommendations[i]:
recommends.append((top_recommendations[i], recommendation_list[top_recommendations[i]]))
i += 1
print("recommending")
return recommends
def build_graph_from_file(file_object):
dirpath = os.getcwd()
file_object.save(os.path.join(dirpath, "temp.sv"))
file_object = open("temp.sv", "r")
graph = nx.DiGraph()
reader = csv.reader(file_object, delimiter = "\t")
for line in reader:
graph.add_edge(line[0], line[1])
file_object.close()
return graph