-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
140 lines (101 loc) · 4.46 KB
/
github.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
133
134
135
136
137
138
139
import http.server
import socketserver
import requests
import networkx as nx
def get_data(username, endpoint, page=1, per_page=100):
url = f'https://api.github.com/users/{username}/{endpoint}?page={page}&per_page={per_page}'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_following(username):
following = []
page = 1
while True:
data = get_data(username, 'following', page)
if not data:
break
for user in data:
following.append(user['login'])
page += 1
return following
def fetch_user(username):
followers = get_data(username, 'followers')
followings = get_data(username, 'following')
return username, followers, followings
def create_network(usernames):
grph = nx.DiGraph()
for username in usernames:
try:
print(f'Fetching user data for {username}')
username, followers, followings = fetch_user(username)
grph.add_node(username)
for follower in followers:
name = follower['login']
grph.add_node(name)
grph.add_edge(name, username)
for following in followings:
name = following['login']
grph.add_node(name)
grph.add_edge(username, name)
except Exception as e:
print(f'Error ~> {e}')
return grph
def recommender(grph, user):
grph = grph.to_undirected()
rcmnds = {}
for node in grph.nodes:
if node == user or grph.has_edge(user, node):
continue
neighbors = len(list(nx.common_neighbors(grph, user, node)))
if neighbors > 0:
rcmnds[node] = neighbors
final_list = sorted(rcmnds.items(), key=lambda item: item[1], reverse=True)
return final_list
class ServerSetup(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
print(f'Received request for recommendations for user: {USERNAME}')
followings = get_following(USERNAME)
grph = create_network(followings + [USERNAME])
recommended = recommender(grph, USERNAME)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"""<html>
<head>
<title>GitHub Recommendations</title>
<style>
body { background-color: #1a1a1a; color: #fff; font-family: Arial, sans-serif; }
.card { background-color: #333; padding: 15px; margin: 10px; border-radius: 5px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
.avatar { width: 50px; height: 50px; border-radius: 50%; }
</style>
</head>
<body>""")
self.wfile.write(f'<h1 style="text-align: center;">Suggested for {USERNAME}</h1>'.encode('utf-8'))
self.wfile.write(b'<div style="display: flex; flex-wrap: wrap; justify-content: center;">')
for user, score in recommended:
user_data = get_data(user, f'../{user}')
profile_link = user_data['html_url']
profile_pic = user_data['avatar_url']
img = f'<img src="{profile_pic}" alt="{user}" class="avatar">'
self.wfile.write(b'<div class="card">')
self.wfile.write(f'<a href="{profile_link}" style="color: #fff; text-decoration: none;">'.encode('utf-8'))
self.wfile.write(f'{img} {user}</a>'.encode('utf-8'))
self.wfile.write(b'</div>')
self.wfile.write(b'</div></body></html>')
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Not found.')
USERNAME = input("> Enter your GitHub username: ")
TOKEN = input(""">Enter your github Personal Access Token :
(you can get your own token from https://github.com/settings/tokens)
""")
headers = {'Authorization': f'token {TOKEN}'}
PORT = 8000
Handler = ServerSetup
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Server started on port {PORT}")
print(f"Open this URL ~> http://127.0.0.1:{PORT}")
httpd.serve_forever()