github获取必应每日大图并推送到其他仓库

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:1 年前

如题

之前博客首页(虽然好久没更新了)的必应图片用的是他人的接口,有一天突然发现首页黑了,然后一看,果然,别人的东西终究是不安全,说寄就寄了。
最安全的办法就是自己实现,除非服务器寄了。计划白嫖github的服务器,github现已背靠微软,微软寄的可能性不大,所以放宽心使用好了。

获取每日必应壁纸(代码很简单就几行,反正也是抄来的😎)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re
import json
from datetime import datetime
from urllib.parse import urljoin

import requests

url = 'https://cn.bing.com'

headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}

res = requests.get(url, headers=headers)
res.encoding = res.apparent_encoding
ret = re.search("var _model =(\{.*?\});", res.text)
if ret:
data = json.loads(ret.group(1))
image_content = data['MediaContents'][0]['ImageContent']

image_url = urljoin(url, image_content['Image']['Url'])
r = requests.get(image_url)
with open('today.png', 'wb') as f:
f.write(r.content)

使用github action定时执行

在GitHub上执行Action有以下步骤:

  1. 创建一个包含Workflow定义的YAML文件。这个文件将描述Action的触发条件、任务序列和执行环境等信息。
  2. 将YAML文件保存在你的代码仓库的.github/workflows目录下,确保文件名以.yml.yaml结尾。
  3. 推送这个文件到你的GitHub仓库,触发一个版本控制操作。
  4. GitHub会自动检测到新的Workflow文件,并根据你定义的触发条件启动Action运行。
  5. 在Action运行过程中,GitHub会根据你的定义,分配符合条件的执行环境,并执行定义的任务序列。
  6. 你可以在GitHub的Actions页面上查看Action运行的状态、日志和结果。
    另外,你也可以手动在GitHub上执行Action。在你的仓库页面上,选择”Actions”标签,然后选择你想运行的Action,在Action页面上点击”Run workflow”按钮手动触发执行。
    需要注意的是,GitHub Actions的执行需要满足一些条件,如正确的配置文件、权限设置和触发条件等。请参考GitHub的官方文档和使用指南以获取更详细的信息和指导。

配置文件信息如下(名称可以随意取,另外就是必须放到项目根目录下的 .github/workflows/run.yml)

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
29
30
31
32
33
34
35
36
37
38
39
name: Bing Today Img

on:
schedule:
- cron: '0 21 * * *' # 国际时间 21 点,估计北京时间 5点

# on:
# push:
# branches: [ main ]


jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: run
run: |
python bingTodayImg.py

- name: commit
run: |
git config user.name github-actions
git config user.email github-actions@github.com
git add .
git commit -m "generated"
git push

另外就是要设置下github-actions bot的权限,不然无法推送(没有权限)。设置方法为:在该仓库的Settings下找到Actions,对General下的Workflow permissions进行读写权限的勾选即可。

每日必应大图的接口地址为:https://raw.githubusercontent.com/shubihu/BingTodayImg/main/today.png

改款

上述的接口地址国内不太好访问,本身博客也是在github上部署的,便想着直接把图片推送到博客的仓库。推送到其他仓库需要权限,权限设置方法如下:
token 的生成需要到这里:个人头像 -> Settings -> Developer settings -> Personal access tokens,点击 Generate new token。这一步需要输入密码,然后我们可以选择所需权限去生成一个token。
为了安全考虑,这个token生成之后只会可见一次,因为后面的步骤会使用,所以我们需要做好保存。
注意这个 token 是用户级别的,它可以用于访问修改该账户名下的任意仓库。
为了让 Github Action 可以访问到这个token,需要给它做一个配置。配置路径是:在该仓库下的 Settings(注意这个是仓库下的设置而非个人下的设置) -> Secrets -> Actions 点击 New repository secret。
Name 的话可以命名为 ACCESS_TOKEN,Value 为上一步生成的访问 token。这里配置的任意内容都可以通过Github Action 访问到,且是加密的,创建之后只能看到 Name 看不到 Value。

改款后的配置信息:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Bing Today Img

on:
schedule:
- cron: '0 21 * * *' # 国际时间 21 点,估计北京时间 5点

# on:
# push:
# branches: [ main ]


jobs:
build:

runs-on: ubuntu-latest

env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: run
run: |
python bingTodayImg.py

- name: commit
run: |
git config user.name github-actions
git config user.email github-actions@github.com
git add today.png
git commit -m "generated"
git push

git config --global user.email "jrwjb@sina.com"
git config --global user.name "shubihu"
git clone https://${ACCESS_TOKEN}@github.com/shubihu/shubihu.github.io.git repo
cp today.png repo/
cd repo
git add today.png
git commit -m "Add today.png"
git push

另外博客仓库也添加了个Actions任务:

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
29
30
31
32
33
34
35
36
name: Get Bing Today Img

# on:
# push:
# branches: [ main ]

on:
schedule:
- cron: '0 */2 * * *' # 每隔两个小时

jobs:
build:

runs-on: ubuntu-latest

env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

steps:
- uses: actions/checkout@v3

- name: commit
run: |
if [[ ! -f "today.png" ]]; then
git config --global user.email "jrwjb@sina.com"
git config --global user.name "shubihu"
git clone https://${ACCESS_TOKEN}@github.com/shubihu/BingTodayImg.git repo
cp repo/today.png ./

git config user.name github-actions
git config user.email github-actions@github.com
git add today.png
git commit -m "generated"
git push
fi

这样就不完美解决了!

参考:
https://mp.weixin.qq.com/s/oRXqNL48wZ0IbrfmNNOTGg
https://github.com/mouday/wallpaper-database/tree/main
https://github.com/licoded/BaiduFanyi_Crawler/issues/1


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!