天天看點

用pygal分析一些github項目

通路github速度慢的修改hosts:

151.101.72.133 assets-cdn.github.com

151.101.229.194 github.global.ssl.fastly.net

import json,sys,os,time,aiohttp,asyncio
from pygal_maps_world.i18n import COUNTRIES
import pygal
#擷取python倉庫資訊url
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
async def fetch(session:aiohttp.ClientSession,url:str):
    async with session.get(url) as resp:
        with open('git.json','wb') as fd:
            while 1:
                chunk = await resp.content.read(8192)
                if not chunk:
                    break
                fd.write(chunk)
                fd.flush()

async def download_json(url):
    async with aiohttp.ClientSession() as session:
        await fetch(session,url)

#異步擷取,這裡好像沒必要
# lp = asyncio.get_event_loop()
# lp.run_until_complete(download_json(url))
# lp.close()
fd = open('git.json',encoding='utf-8')
json_data = json.load(fd)
fd.close()
#所有資料在items裡
items_dict = json_data['items']
#提取名字
pop_names = []
#提取星星數 , 描述, 連結
pop_dict = []
for d in items_dict:
    pop_names.append(d['name'])
    pop_dict.append({'value':d['stargazers_count'],'label':str(d['description']),'xlink':d['html_url']})
chart = pygal.Bar(rounded_bars = 5)
chart.title = '倉庫項目'
chart.add('',pop_dict)
#X軸描述
chart.x_labels = pop_names
chart.render_to_file('resp.svg')