以前,装作很好学的样子,fork了不少别的代码,然鹅一个都没有认真学习下,就放哪儿了几年,实在无法忍受自己github库的杂乱,想着去整理下,当70多个库,一个个删除,预估得1个多小时。在网上查了下别的方法,也结合别人写的做了有一些尝试,现在将内容写成博客记录。
文档有关链接如下: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