为了给我的资源分享站GitHub主页增加一个花里胡哨的Readme,利用 GitHub 上提供的下载 Pixiv 日榜的代码每天定时下载 P 站的日榜,并选择前三张放在 Readme 里。日榜每天都更新,图片每天都要下载,我又是一个喜欢屯东西的人,那么就面临了一个下载下来的图片保存到哪里的问题。一开始想的自然是存到阿里云盘,但是国内提供的服务就是没有隐私可言,存在别人那里的东西都会被人看光光,至少是被阿里云的鉴黄算法看光光😂,于是就出现了下面这种情况。

image-20220209003041597

直接把我图片给删除了!👍

To add a fancy Readme to my resource-sharing site and my GitHub homepage, I used the code GitHub provides for downloading the Pixiv daily ranking to automatically download the daily ranking every day, then put the top three images in the Readme. The daily ranking updates every day, so the images need to be downloaded every day — and since I’m the kind of person who likes to hoard things, I was faced with the question of where to store the downloaded images. At first I naturally thought of Alibaba Cloud Drive, but domestic services simply have no privacy — anything stored on someone else’s server gets seen by everyone, at least by Alibaba Cloud’s porn-detection algorithm 😂, and that’s how the situation below came about.

image-20220209003041597

It straight-up deleted my images! 👍

文件存储在 GitHub 虽然访问比较困难,但是起码东西是在的。为了能够自动化执行下载图片和上传图片到 GitHub 的过程,就需要通过调用 API 来实现。

GitHub API 官方文档

创建仓库 和 上传文件的操作都需要使用 access_token ,在执行代码之前首先到 https://github.com/settings/tokens 去生成一个token。

接下来就可以按照文档的说明创建仓库和上传图片了。

需要注意的是,对于上传的文件内容要进行 Base64 编码。

image-20220209004211780

根据文档编写上传文件的 Python 代码:

Storing files on GitHub is a bit hard to access, but at least the files are still there. To automate downloading images and uploading them to GitHub, I needed to do it by calling the API.

GitHub API official documentation

Both creating a repository and uploading files require an access_token — before running the code, first go to https://github.com/settings/tokens to generate a token.

Then you can follow the documentation to create a repository and upload images.

Note that the content of the file being uploaded must be Base64-encoded.

image-20220209004211780

Here is the Python code for uploading files, written according to the documentation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
import base64
import requests

# 获取文件的 Base64 编码结果
def get_content(file_path):
with open(file_path,'rb') as f:
f_data = f.read()
data_b64 = base64.b64encode(f_data).decode('utf-8') # 将二进制文件编码后转换为字符串形式
return data_b64

def upload_file(content,file_name,repo_name,token):
url = f"https://api.github.com/repos/{repo_name}/contents/{file_name}"
headers = {"Authorization": "token " + token}
data = {
"message": "upload file",
"committer":{
"name": "username",
"email": "xx@xx.com"
},
"content": content
}
req = requests.put(url=url, data=data, headers=headers)

if __name__ == '__main__':
token = ''
f_data = get_content("./pic.jpg")
upload_file(f_data,"pic.jpg","UserName/RepoName",token)

更简单的方法是使用 PyGithub 提供的封装接口

首先安装 PyGithub 包

An easier way is to use the wrapper interface provided by PyGithub

First install the PyGithub package

1
pip install PyGithub

在代码中直接调用 create_file 上传文件。

In the code, just call create_file to upload the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from github import Github

token = ''

g = Github(token)

user = g.get_user()

repo = user.create_repo("仓库名")

# 如果是已经存在的仓库,可以使用get_repo来获得repo对象
# repo = g.get_repo('用户名/仓库名')

# 读取文件并上传
with open('./pic.jpg','rb') as f:
data = f.read()
# 不需要进行Base64编码,编码过程在create_file内部已经完成了
repo.create_file('文件名','提交的message',data)

自动下载图片和转存图片的完整代码可以到我的仓库中进行查看。

https://github.com/WANG-Guangxin/Download-Pixiv-Ranking

全程白嫖 Github Action 资源。

拿捏🤏

The complete code for automatically downloading images and re-uploading them can be found in my repository.

https://github.com/WANG-Guangxin/Download-Pixiv-Ranking

I freeloaded on GitHub Actions resources the whole way.

Got it handled 🤏