iT邦幫忙

2021 iThome 鐵人賽

DAY 22
1
Software Development

從 JavaScript 角度學 Python系列 第 22

從 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": "octocat@github.com",
  "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

關於兔兔們

兔法無邊


上一篇
從 JavaScript 角度學 Python(21) - Requests
下一篇
從 JavaScript 角度學 Python(23) - Class
系列文
從 JavaScript 角度學 Python31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言