从 JavaScript 角度学 Python(22) - GitHub API

前言

前面我们已经学习了不少的 Python 新知识,所以也差不多该到了实作一下前面的知识点,这样子才能够确实的吸收观念。

作业需求

那麽这一章节到底要实作什麽东西呢?绝对不是无聊的 BMI 计算机了啦~

前面会介绍 Requests 套件的主要原因是因为我们要来接一些 API 罗!等等,先不要那麽兴奋

https://ithelp.ithome.com.tw/upload/images/20210921/20119486DKq4iLrVIV.png

那麽我们要做什麽呢?这边我们会实作串接 GitHub API 的部分,所以很明显这边将会使用到前面章节所学的 Requests 套件,其他功能需求呢?我就不卖关子了,让我们直接来看需求吧!

  • 需要串接 GitHub API
  • 可以自定义想要查询的 GitHub 帐号
  • 输入要查询的 GitHub 帐号後回传该 GitHub 按过 Star 的专案
  • 查询完毕後要将结果转换成 JSON 档案

可以的话可以试着先自己做做看,如果真的没有想法在参考以下我的思考逻辑与解决方式,当然以下解答不是绝对,而是一个参考依据而已。

那麽这边也为了方面制作所以这边就提供 GitHub API 路径与 GitHub 文件,减少各位在茫茫大海中捞文件:

准备实作

首先透过上面的需求我们可以了解到我们将会使用 GitHub API 文件取得使用者 Star 过的专案,因此一开始我们必须先阅读一下 GitHub API 文件。

GitHub API

基本上上面需求我们可以了解到将会需要使用跟使用者相关的 API,毕竟我们要找使用者按过的 Star 专案,因此在 https://docs.github.com/cn/rest/ 页面可以看到一个 用户 的字眼:

点进去之後在前面看到相关的 AJAX 回传结果,如下:

{
  "login": "octocat",
  "id": 1,
  "node_id": "MDQ6VXNlcjE=",
  "avatar_url": "https://github.com/images/error/octocat_happy.gif",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "site_admin": false,
  "name": "monalisa octocat",
  "company": "GitHub",
  "blog": "https://github.com/blog",
  "location": "San Francisco",
  "email": "[email protected]",
  "hireable": false,
  "bio": "There once was...",
  "twitter_username": "monatheoctocat",
  "public_repos": 2,
  "public_gists": 1,
  "followers": 20,
  "following": 0,
  "created_at": "2008-01-14T04:33:35Z",
  "updated_at": "2008-01-14T04:33:35Z",
  "private_gists": 81,
  "total_private_repos": 100,
  "owned_private_repos": 100,
  "disk_usage": 10000,
  "collaborators": 8,
  "two_factor_authentication": true,
  "plan": {
    "name": "Medium",
    "space": 400,
    "private_repos": 20,
    "collaborators": 0
  }
}

上面可以看到使用者相关的资讯都非常的清楚,而这边我们只需要注意一个属性就好,也就是 starred_url 这个属性就是该使用者按过 Star 的 API,但是不知道为什麽他的范例 Url 後面会多了 {/owner}{/repo},所以这边就直接忽略,我们只需要看这一段就好:

{
  "starred_url": "https://api.github.com/users/octocat/starred",
}

只要将 octocat 替换成你的 GitHub 帐号,或者是你想查询的使用者 GitHub 帐号,例如我的:

{
  "starred_url": "https://api.github.com/users/hsiangfeng/starred",
}

这样你就可以直接点上面连结网址打开来查看结果,或者你也可以试着使用 Postman 神器来测试戳一下,如果对於 Postman 不熟悉的话可以阅读一下这一篇,因此这边就不继续着墨如何使用 Postman 了。

那麽透过上面阅读 API 文件之後,我们可以知道我们所需要的 API 路径是:https://api.github.com/users/{{ username }}/starred,这样就可以准备进入撰写程序码阶段罗。

撰写程序码

首先请建立一个资料夹叫做 get-github,然後我们要先建立虚拟环境

python3 -m venv .venv

接下来在需求中有提到需要跟 GitHub 做 API 请求,因此要针对该虚拟环境安装相关套件:

pip3 install requests2

那麽另一个需求则是 可以自定义想要查询的 GitHub 帐号 所以就会使用到 input 方法与 requests 请求,所以前面就不多说了直接搭配 API Url 来看完成结果:

import requests

username = str(input('请输入你要查询的使用者帐号:'))

r = requests.get(f'https://api.github.com/users/{username}/starred')

接下来就是写入 JSON 的部分,那麽为了确保请求是成功的状态下,所以要再补上一段判断,确保 AJAX 请求是成功的才开始写入到 JSON 档案,但是这边要注意 requests 处理後的 JSON 格式是 Python 读得懂的 list 格式,所以要存入到 .json 之前必须额外处理过:

import requests
import json

username = str(input('请输入你要查询的使用者帐号:'))

r = requests.get(f'https://api.github.com/users/{username}/starred')

if r.status_code == 200:
  starList = open('starList.json', 'w+')
  starList.write(json.dumps(r.json()))
  starList.close()
else:
  print('取得 API 过程发生错误。')

那麽这边就很简单的完成了一个取得别人 star 过的专案小工具,是不是很简单呢?还可以有

今天这一份程序码将会放在这个储存库:https://github.com/hsiangfeng/javascript-to-python

作者的话

以往其实我都习惯去 7-11 买一些微波食品都做中餐或者是晚餐,可是最近发现全家的微波食品比 7-11 好吃很多,结果从此就很爱买全家的微波食品,但是全家离我家好远 QQ

关於兔兔们

兔法无边


<<:  【从零开始的Swift开发心路历程-Day11】XIB

>>:  [Day22]C# 鸡础观念- 物件导向(oop)~物件(Object)

[C 语言笔记--Day08] Thread

大纲 什麽是 thread ? Thread Creation Thread Termination...

Cloud Armor

关於Cloud Armor安全政策 首先也许从字面上或许不好意会什麽是Cloud Armor呢?其实...

33岁转职者的前端笔记-DAY 21 英寸转公分单位转换器练习笔记

基本语法笔记 四舍五入: Math.round(); 无条件进位: Math.ceil(); 无条件...

30天打造品牌特色电商网站 Day.2 电商网站比较分析

认识过网站的基础後,接下来可以多观察生活中的范例做练习,选择几个成效不错的网站去做比较与分析,以下整...

DAY 6 ROS 通讯架构2

前言 今天我们要来讲 ROS 中最为核心的部分,ROS 提供了 四种通讯方式,分别为 Topic、S...