-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (74 loc) · 3.43 KB
/
main.py
File metadata and controls
104 lines (74 loc) · 3.43 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
# Import FastAPI and requests libraries
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from httpx import AsyncClient
import requests , json, datetime
# Create an app instance
app = FastAPI()
data=[] # I am Important
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory = "templates")
# Define a route that takes a JSON body with the organization name
@app.post("/repos")
def get_org_repos(body: dict):
org = body.get("org") # getting org name from body
return getToppers(org)
@app.get("/", response_class=HTMLResponse)
async def show_form(request : Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/get_items")
async def get_items():
return {"items": data}
@app.post("/insert")
async def insert_data( request : Request ):
new_entry = await request.form()
data.clear()
data.append(new_entry)
x=getToppers(data[0]["name"])
# return x
return templates.TemplateResponse("display_data.html", {"request": request, "data": json.dumps(x),"cname":data[0]["name"]})
@app.post("/track-commits/")
async def track_commits(request: Request):
# async def track_commits(organization: str, repository: str, start_date: datetime.date, end_date: datetime.date):
new_entry = await request.form()
organization = new_entry["organization"]
repository = new_entry["repository"]
start_date = new_entry["start_date"]
end_date = new_entry["end_date"]
format = '%Y-%m-%d'
start_str = datetime.datetime.strptime(str(start_date), format)
end_str = datetime.datetime.strptime(str(end_date), format)
github_api_url = f"https://api.github.com/repos/{organization}/{repository}/commits"
print(new_entry)
async with AsyncClient() as client:
response = await client.get(github_api_url)
commits = response.json()
dates = [] # List to store dates
commits_count = [] # List to store daily commit counts
for commit in range(len(commits)):
commit_date = commits[commit]["commit"]["author"]["date"][:10]
datetime_str = datetime.datetime.strptime(commit_date, format)
if start_str <= datetime_str <= end_str:
dates.append((commit_date))
return templates.TemplateResponse("line.html", {"request": request, "dates": json.dumps(dates)})
def getToppers(org):
if org:
# Make a request to the GitHub API to get the repositories of the organization
response = requests.get(f"https://api.github.com/search/repositories?q=org:{org}&sort=stars&order=desc")
result = []
# Check if the response is successful
if response.json()["incomplete_results"] != True:
data = response.json()["items"][:10]
result={"results" : []}
for i in range(10):
newData={"name": data[i]["name"], "stars": data[i]["stargazers_count"]}
result["results"].append(newData)
return result
else:
# Return an error message if the response is not successful
return {"error": f"Could not get the repositories of {org}"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=80)