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

直接把我图片给删除了!👍
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.

It straight-up deleted my images! 👍
文件存储在 GitHub 虽然访问比较困难,但是起码东西是在的。为了能够自动化执行下载图片和上传图片到 GitHub 的过程,就需要通过调用 API 来实现。
创建仓库 和 上传文件的操作都需要使用 access_token ,在执行代码之前首先到 https://github.com/settings/tokens 去生成一个token。
接下来就可以按照文档的说明创建仓库和上传图片了。
需要注意的是,对于上传的文件内容要进行 Base64 编码。

根据文档编写上传文件的 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.

Here is the Python code for uploading files, written according to the documentation:
1 | import json |
更简单的方法是使用 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 | from github import Github |
自动下载图片和转存图片的完整代码可以到我的仓库中进行查看。
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 🤏


