-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
82 lines (76 loc) · 2.01 KB
/
api.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
import base64
import json
import requests
import os
from datetime import date
from dotenv import load_dotenv
load_dotenv()
Headers=(os.getenv('User'),os.getenv('token'))
def Analysis(url):
res=url.split('github.com')[-1]
URL = "https://api.github.com/repos"+res+"/commits"
r = requests.get(url = URL,auth=Headers)
data = r.json()
dates={}
timeline=[]
for i in data:
key=i["commit"]["author"]["date"].split('T')[0]
if key not in dates:
dates[key]=1
timeline.append(key)
else:
dates[key]+=1
a=[int(i) for i in timeline[-1].split('-')]
b=[int(i) for i in timeline[0].split('-')]
d0 = date(a[0], a[1], a[2])
d1 = date(b[0], b[1], b[2])
delta = d1 - d0
return {'start':timeline[-1], 'end':timeline[0], 'stats':dates, 'span': delta.days+1}
def read(url):
r = requests.get(url,auth=Headers)
r.raise_for_status()
data = r.json()
file_content = data['content']
file_content_encoding = data.get('encoding')
if file_content_encoding == 'base64':
file_content = base64.b64decode(file_content).decode()
else:
file_content=''
return file_content
def assets(URL):
r = requests.get(url = URL,auth=Headers)
data = r.json()
res=[]
valid=['py','js','html','css']
for i in data:
if i['type']=='file':
if i['name'].split('.')[-1] in valid:
res += [i['url']]
if i['type'] == 'dir':
res += assets(i['url'])
return res
def res(url):
res=url.split('github.com')[-1]
URL = "https://api.github.com/repos"+res+"/contents"
master={}
for i in assets(URL):
for j in read(i).split('\n'):
master[j.strip()]='0'
return master
def Compare(url1,url2):
repo1=res(url1)
repo2=res(url2)
matched=0
for i in repo1:
if i in repo2:
matched+=1
for i in repo2:
if i in repo1:
matched+=1
total=len(repo1)+len(repo2)
return { 'score' : matched/total }
def Rate():
URL = "https://api.github.com/rate_limit"
r = requests.get(url = URL,auth=Headers)
data = r.json()
return data