zoukankan      html  css  js  c++  java
  • Github API

    Web API

    web api是网站的一部分,用于与使用非常具体的URL请求特定信息的程序交互,这种请求被称为API调用。请求的数据将以易于处理的格式(如JSON或CSV)返回;依赖于外部数据源的大多数应用程序都依赖于API调用,如集成社交媒体网站的应用程序。

    Github的API

    https://api.github.com/search/repositories?q=language:python&sort=stars

    第一部分(https://api.github.com/)将请求发送到Github网站响应API调用的部分;search/repositories让API搜索github上所有的仓库,问号指出要传递一个实参,q=开始指定查询,获取语言为Python的仓库的信息,最后一部分指定将项目按其获得的星级排序。如果incomplete_results的值为false,证明请求成功,true代表GitHub无法全面处理该API。

    API大多存在速率限制,即在特定时间内执行的请求数存在限制,在浏览器中输入https://api.github.com/rate_limit,可获得GitHub的限制;search表示每分钟限制10个请求,reset值指的是配额将重置的Unix时间或新纪元时间(1970年1月1日午夜后多少秒),注册获得API秘钥之后,配额将高很多。

    {
      "resources": {
        "core": {
          "limit": 60,
          "remaining": 60,
          "reset": 1542466950
        },
        "search": {
          "limit": 10,
          "remaining": 10,
          "reset": 1542463410
        },
        "graphql": {
          "limit": 0,
          "remaining": 0,
          "reset": 1542466950
        }
      },
      "rate": {
        "limit": 60,
        "remaining": 60,
        "reset": 1542466950
      }
    }
    
    
    --将项目名存储在一个列表中,星级、描述、链接放在一个字典里,使用pygal绘制直方图,每个条形柱会存储一个连接,点击可直接转到所在网页。
    hist = pygal.Bar()
    hist.add('',plot_dicts)
    import requests
    import pygal
    from pygal.style import LightenStyle as LS,LightColorizedStyle as LCS
    #执行API调用,并存储响应,以星级排序
    url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
    r = requests.get(url)
    #状态码200表示请求成功
    print('Status code:',r.status_code)
    #将API响应返回的JSON格式的信息转换为一个Python字典
    respose_dict = r.json()
    print('Total repositories:',respose_dict['total_count'])#一共多少个仓库
    #items是一个包含很多字典的列表,每一个字典都包含一个仓库的信息
    repo_dicts = respose_dict['items']
    #研究第一个仓库
    repo_dict = repo_dicts[0]
    print('Keys:',len(repo_dict))
    print('
    Name:',repo_dict['name'])#项目名称
    print('Owner:',repo_dict['owner']['login'])#项目所有者登录名
    print('Stars:',repo_dict['stargazers_count'])#多少个星的评级
    print('Repository:',repo_dict['html_url'])#GitHub仓库的URL
    print('Created:',repo_dict['created_at'])#创建时间
    print('Updated:',repo_dict['updated_at'])#最后一次更新时间
    print('Description:',repo_dict['description'])#打印仓库描述
    #2015年GitHub上星级最高的项目,收藏人数16000
    for rp_dict in repo_dicts:
        if rp_dict['name'] == 'httpie':
            #rp_index = repo_dicts.index(rp_dict)
            print('
    Owner:', rp_dict['owner']['login'])
            print('Stars:', rp_dict['stargazers_count'])
            print('Created:', rp_dict['created_at'])
            print('Updated:', rp_dict['updated_at'])
    #将项目名存储在一个列表中,星级、描述、链接放在一个字典里
    names,plot_dicts = [],[]
    for r_dict in repo_dicts:
        names.append(r_dict['name'])
        if r_dict['description']:
            plot_dict = {
                'value':r_dict['stargazers_count'],
                'label':r_dict['description'],
                'xlink':r_dict['html_url']
                }
            plot_dicts.append(plot_dict)
        else:
            plot_dict = {
                'value': r_dict['stargazers_count'],
                'label': 'None',
                'xlink': r_dict['html_url']
                }
            plot_dicts.append(plot_dict)
    #将GitHub上Python项目的星级和名称可视化
    my_style = LS('#119911',base_style=LCS)#指定颜色和样式
    #绘制直方图,创建一个Config实例,存储所有的配置
    my_config = pygal.Config()
    my_config.x_label_rotation = (-45) #x轴标签旋转-45度
    my_config.show_legend = False #隐藏图例
    my_config.title_font_size = 25 #标题大小
    '''
    主标签是Y轴上为5000整数倍的刻度,副标签是X轴上的项目
    名称和Y轴上的大部分数字,但是显示效果并非如此
    '''
    my_config.label_font_size = 18 #副标签大小
    my_config.major_label_font_size = 30 #主标签大小
    #将较长的标签缩短为15个字符,鼠标指向时显示完整名称
    my_config.truncate_label = 15
    my_config.show_y_guides = False #隐藏图表中的水平线,包括x轴
    my_config.width = 1000 #设置图表宽度
    hist = pygal.Bar(my_config,style=my_style)
    hist.title = 'Most-Popular Python Projects on GitHub'
    hist.x_labels = names
    hist.add('',plot_dicts)#图例名称为空
    hist.render_to_file(r'images
    ames_stars.svg')
    # 直接渲染到浏览器,需要安装lxml
    #hist.render_in_browser()
     
  • 相关阅读:
    Linux GCC常用命令
    用两个栈实现一个队列(转载)
    BMP格式介绍(一)
    图像处理笔记(1): bmp文件结构处理与显示
    大数据竞赛平台——Kaggle 入门篇
    图像识别中的深度学习
    面向对象中的抽象和具体类的理解
    全排列算法
    少有程序员读的书
    照片回执
  • 原文地址:https://www.cnblogs.com/charliedaifu/p/9975904.html
Copyright © 2011-2022 走看看