-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (68 loc) · 2.77 KB
/
main.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
import os
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import List
import requests
@dataclass
class Repo:
name: str
description: str
url: str
stars: str
forks: str
issues: str
created_at: str
updated_at: str
def main():
result = []
repos = file_read()
headers = {
"Authorization": f"token {os.environ.get('GTOKEN')}",
"Content-Type": "application/json"
}
for repo in repos:
url = "https://api.github.com/repos/" + repo
resp = requests.get(url, headers=headers)
status_code, body = resp.status_code, resp.json()
if status_code != 200:
print(f"{status_code} : {body}")
else:
result.append(format_body(body))
file_write(sort_by_stars(result))
return
def format_body(body):
data = Repo(**{
"name": body["name"],
"description": body["description"],
"url": body["html_url"],
"stars": body["stargazers_count"],
"forks": body["forks"],
"issues": body["open_issues"],
"created_at": body["created_at"],
"updated_at": body["updated_at"]
})
print(data)
return data
def sort_by_stars(repos: List[Repo]):
def by_stars(elem):
return elem.stars
sorted_repos = sorted(repos, key=by_stars, reverse=True)
print("Sorted repos.")
return sorted_repos
def file_read():
with open("repos.txt", "r") as reader:
repos = reader.read().splitlines()
print("Accessed repos.txt")
return repos
def file_write(repos):
with open("README.md", "w") as writer:
body = f"""# Top Python Web Frameworks\n\n[](https://github.com/sunnysid3up/python-web-frameworks/actions/workflows/update.yml)\n\nA list of popular Python web frameworks ranked by the number of GitHub stars, automatically updated every week.\n\nLast update: {datetime.now(tz=timezone.utc).strftime("%m/%d/%Y, %H:%M:%S")} (UTC)
| Name | Description | Stars | Forks | Issues | First Commit | Last Commit |
|---------------|----------------------|---------------------------|----------------|----------------------|---------------------|---------------------|"""
for repo in repos:
body += f"\n| [{repo.name}]({repo.url}) | {repo.description.replace(' |', '.')} | {repo.stars} | {repo.forks} | {repo.issues} | {repo.created_at[:4]} | {repo.updated_at[:-10]} |"
body += "\n\n## Contribute \n\nCreate an issue or pull request if you would like to add more frameworks :)"
writer.write(body)
print("Updated README.")
if __name__ == "__main__":
main()