696 字
2 分钟
pygithub教程
pygithub的使用
众所周知啊,有时候我们想将文件推送到github得使用git。但是我们不可能在程序中每次修改完文件就git add,git commit ,git push🙌。这样做简直太累人了!尤其是对于我这种懒批,每次更新一个文章就得手动推送的。所以,在项目运行过程中,我们可以直接使用pygithub脚本来实现文件的推送。
官方文档:https://pygithub.readthedocs.io/en/stable/introduction.html
一、介绍
PyGithub 是一个强大的 Python 库,用于与 GitHub API 进行交互。它可以帮助开发者自动化许多常见的操作,如创建和管理仓库、处理issues和pull requests等。虽然他是非官方的一个库,但是却是最主流的👍
cmd里面运行以下命令即可安装
pip install PyGithub二、使用前的配置
首先,既然砸门要操作github远程仓库的文件,那么我们就一定需要github的token,这个可以直接百度,跟着操作就行!👌
- 权限建议:
repo:读写私有/公开仓库user:读取用户信息admin:repo_hook:管理 Webhook(可选)
三、初始化客户端
使用如下方式(推荐)进行初始化
from github import Github,Authauth = Auth.Token("your_github_token")#这里填入上面你从github拿到的token,记得不要硬编码!g = Github(auth=auth)print(g.get_user().login)#如果返回了你的GitHub用户名那你就成功啦!四、深入学习
1.用户
#获取自己me=g.get_user()print(me.login)#打印用户名print(me.email)#打印邮箱print(me.public_repos)#打印公开仓库!#还可以获取别人的信息a = g.get_user("别人的用户名")for repo in a.get_repos():#获取对方的所有项目 print(repo.name, repo.stargazers_count)#打印项目名称以及star数量当然还有一堆获取到用户的方法,这里不做展示了,有需要的大佬可以去官方文档里面查。😎
2.仓库
这是很常用的一个功能,我们修改文件的操作也主要是跟这个功能有关。
#获取仓库repo=g.get_repo("Big-fat-dog/mizuki_backstage")#参数是用户名/项目名称仓库信息(只读!)
| 操作 | 代码 | 返回值 |
|---|---|---|
| 仓库名 | repo.name | "Hello-World" |
| 描述 | repo.description | "My first repo" |
| 星星数 | repo.stargazers_count | 1234 |
| Fork 数 | repo.forks_count | 56 |
| 默认分支 | repo.default_branch | "main" |
| 仓库 URL | repo.html_url | "https://github.com/... |
#我们可以打印仓库信息pritn(repo.name,repo.forks_count)3.Issue和Pull Requests管理
#获取所有的开放的issueissues=repo.get_issues(state="open")for issue in issues: print(f"#{issue.number}: {issue.title}")#创建issuerepo.create_issue( title="Bug report", body="Something went wrong...", assignee="fatdog" # 可选,提出人)#获取prpulls = repo.get_pulls(state="open")for pr in pulls: print(f"PR #{pr.number}: {pr.title}")4.文件操作
这个一定要谨慎呀!!!😭😭😭😭😭别问为什么,说多了都是泪!
#读取文件file = repo.get_contents("README.md")print(file.decoded_content.decode())
#创建文件#文件不能存在,否则会报错!repo.create_file( path="new_file.md", # 文件路径(相对仓库根目录) message="Add new file", # 提交信息 content="# Hello", # 文件内容(字符串) branch="main" # 分支(可选))
#更新文件(要提供sha)# 先获取文件 SHAcontents = repo.get_contents("existing.md")repo.update_file( path="existing.md", message="Update file", content="# Updated", sha=contents.sha, # ← 必须!否则报 422 branch="main")
#删除文件contents = repo.get_contents("old_file.md")repo.delete_file( path="old_file.md", message="Delete file", sha=contents.sha # ← 必须!)
#读取文件内容content_file = repo.get_contents("anime.ts", ref=branch_name)print(content_file.decoded_content.decode("utf-8"))读取到的文件**它是 PyGithub 库里的 ContentFile 类实例。**pygithub将文件封装成了这个类,他具有很多属性和方法,用于操作github仓库里的文件。
他有如下api:
获取内容 (最常用!🔥)
| 属性/方法 | 说明 | 返回值类型 | 提示!😎 |
|---|---|---|---|
.decoded_content | 神器! 自动处理 Base64 解码,返回字节流 (bytes)。 | bytes | 99% 的情况都用它! 别再手动 base64.b64decode() 了,累不累! |
.content | 原始内容,通常是 Base64 编码的字符串。 | str | 除非你要自己手动解码,否则别碰它!用了还得自己转码,多此一举! |
.encoding | 告诉你是怎么编码的 (通常是 "base64")。 | str | 一般不用管,decoded_content 已经帮你搞定了。 |
5.release管理
# 创建 Releaserepo.create_git_release( tag="v1.0.0", name="Version 1.0", message="Initial stable release", draft=False, prerelease=False)
# 获取最新 Releaselatest = repo.get_latest_release()print(latest.tag_name, latest.published_at)6.分支与commit
# 获取分支branch = repo.get_branch("main")print(branch.commit.sha)
# 获取提交历史commits = repo.get_commits()for commit in commits[:5]: print(commit.sha, commit.commit.message)参考文章:https://jishuzhan.net/article/2013902672270245889

部分信息可能已经过时









