-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (96 loc) · 3.35 KB
/
app.py
File metadata and controls
116 lines (96 loc) · 3.35 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
import csv
import requests
import networkx as nx
from flask import Flask, render_template, jsonify
from datetime import datetime
from functools import lru_cache
import gspread
from google.oauth2.service_account import Credentials
app = Flask(__name__)
# Configure the Google Sheets API credentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = Credentials.from_service_account_file('web3bridge.json', scopes=scope)
# Cache the results of the get_user_repos function
@lru_cache(maxsize=128)
def get_user_repos(username):
url = f"https://api.github.com/users/{username}/repos"
headers = {
'Authorization': 'Bearer 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',
}
response = requests.get(url, headers=headers)
print('response',response)
if response.status_code == 200:
repos = response.json()
print('repos:',repos)
return repos
else:
print("Error:", response.text)
return []
def create_github_graph():
sheet_url = 'https://docs.google.com/spreadsheets/d/1x5aImcjMQShiM9XP7ouy7w6Yizu3Qkeu1gE4ipi-Apw/edit#gid=0'
sheet_name = 'github_analytics'
client = gspread.authorize(credentials)
spreadsheet = client.open_by_url(sheet_url)
print(spreadsheet)
worksheet = spreadsheet.get_worksheet(0)
rows = worksheet.get_all_values()
print('rows :',rows)
header = rows[0]
rows = rows[1:]
repos_data = []
for row in rows:
if len(row) >= 3:
email = row[1]
username = row[2]
print("username:" ,username)
github_repos = get_user_repos(username)
for repo in github_repos:
if "message" not in repo:
created_date = repo["created_at"]
created_date = datetime.strptime(created_date, "%Y-%m-%dT%H:%M:%SZ")
if created_date >= datetime(2021, 6, 1):
repo["email"] = email
repo["username"] = username
repos_data.append(repo.copy())
else:
print(f"Error: {repo['message']}")
G = nx.Graph()
for repo in repos_data:
G.add_node(repo["name"], email=repo["email"], size=3)
G.add_edge(repo["username"], repo["name"])
for username in set(repo["username"] for repo in repos_data):
G.add_node(username, email="", size=5, color="red")
user_repos = [repo["name"] for repo in repos_data if repo["username"] == username]
for repo in user_repos:
G.add_edge(username, repo, color="red")
return G
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data')
def data():
G = create_github_graph()
graph_data = {
'nodes': [],
'links': []
}
for node in G.nodes():
if 'email' in G.nodes[node]:
graph_data['nodes'].append({
'id': node,
'group': 'repo',
'email': G.nodes[node]['email']
})
else:
graph_data['nodes'].append({
'id': node,
'group': 'user'
})
for edge in G.edges():
graph_data['links'].append({
'source': edge[0],
'target': edge[1]
})
return jsonify(graph_data)
if __name__ == "__main__":
app.run()