import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
def download_data(url: str):
"""下载api数据"""
response = requests.get(url, timeout=None)
print("响应状态码为: " + str(response.status_code))
resp_json = response.json()
items = resp_json["items"]
rep_names, rep_star_count, rep_fork_count, rep_open_issue_count = [], [], [], []
for item in items:
rep_names.append(item["name"])
rep_star_count.append(item["stargazers_count"])
rep_fork_count.append(item["forks_count"])
rep_open_issue_count.append(item["open_issues_count"])
return {"rep_names": rep_names, "rep_star_count": rep_star_count, "forks_count": rep_fork_count,
"open_issues_count": rep_open_issue_count}
def draw(json_date: dict):
"""绘图"""
my_style = LS('#333366', base_style=LCS)
# x轴文本旋转45度,展示图例
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=True)
chart.title = "Github上最受欢迎的python项目"
chart.x_labels = json_date["rep_names"]
chart.add("star数量", json_date["rep_star_count"])
chart.add("fork数量", json_date["forks_count"])
chart.add("open issue数量", json_date["open_issues_count"])
chart.render_to_file('github-python.svg')
url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
result = download_data(url)
draw(result)