以前,裝作很好學的樣子,fork了不少别的代碼,然鵝一個都沒有認真學習下,就放哪兒了幾年,實在無法忍受自己github庫的雜亂,想着去整理下,當70多個庫,一個個删除,預估得1個多小時。在網上查了下别的方法,也結合别人寫的做了有一些嘗試,現在将内容寫成部落格記錄。
生成删除權限的toekn
-
建立token
打開https://github.com/settings/tokens頁面,點選Generate new token 按鈕,選中delete_repo
-
複制token
完成上一步後會得到一個token,複制留用。
參考github開發文檔
文檔有關連結如下:https://docs.github.com/en/rest/reference/repos#delete-a-repository
Delelte API 接口如下:
/repos/{owner}/{repo}
複制
官網删除的案例:
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world
複制
改為python案例後:
# encoding=utf-8
from time import sleep
import requests
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token xxx", # 此處貼token
"X-OAuth-Scopes": "repo"
}
with open('/Users/zhang/Desktop/repos.txt', 'r', encoding='utf-8') as f:
data = f.readlines()
url = "https://api.github.com/repos/{}/{}"
urls = []
for line in data:
name, repo = line.strip().split("/")
urls.append(url.format(name, repo))
for l in urls:
logger.info('删除repos連結:%s' % l)
res = requests.delete(url=l, headers=headers)
logger.info('删除狀态:%s' % res)
複制
注意:repos.txt格式如下:
zhangwe/nlp
zhangwe/math
zhangwe/line
複制