我是如何白嫖 Github 服务器自动抓取每日必应壁纸的?(二)

简介: 我是如何白嫖 Github 服务器自动抓取每日必应壁纸的?

必应壁纸爬虫

上面分析出了必应壁纸的 API ,那么就不难写一个自动爬取当天必应壁纸的自动化程序。

  1. 请求必应壁纸 API。
  2. JSON 解析出图片 URL。

这里网络请求使用 Java 原生写法,JSON 解析使用了 FASTJSON ,代码简单直接放上来了。

/**
 * <p>
 * 网络请求操作工具类
 *
 * @author niujinpeng
 * @link https://github.com/niumoo
 */
public class HttpUtls {
    /**
     * 获取 HTTP 连接
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static HttpURLConnection getHttpUrlConnection(String url) throws IOException {
        URL httpUrl = new URL(url);
        HttpURLConnection httpConnection = (HttpURLConnection)httpUrl.openConnection();
        httpConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
        return httpConnection;
    }
    /**
     * 请求指定 URL 的内容
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String getHttpContent(String url) throws IOException {
        HttpURLConnection httpUrlConnection = getHttpUrlConnection(url);
        StringBuilder stringBuilder = new StringBuilder();
        // 获得输入流
        try (InputStream input = httpUrlConnection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(
            input);) {
            byte[] buffer = new byte[1024];
            int len = -1;
            // 读到文件末尾则返回-1
            while ((len = bis.read(buffer)) != -1) {
                stringBuilder.append(new String(buffer, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpUrlConnection.disconnect();
        }
        return stringBuilder.toString();
    }
}

如果觉得 Java 原生网络请求写法繁琐,也可以使用 OkHTTP 进行请求。请求到响应结果之后,使用 FASTJSON 解析响应的结果。

/**
 * @author niujinpeng
 * @link https://github.com/niumoo
 */
public class Wallpaper {
    // BING API
    private static String BING_API = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1612409408851&pid=hp&FORM=BEHPTB&uhd=1&uhdwidth=3840&uhdheight=2160";
    private static String BING_URL = "https://cn.bing.com";
    public static void main(String[] args) throws IOException {
        String httpContent = HttpUtls.getHttpContent(BING_API);
        JSONObject jsonObject = JSON.parseObject(httpContent);
        JSONArray jsonArray = jsonObject.getJSONArray("images");
        // 图片地址
        String url = BING_URL + (String)jsonArray.getJSONObject(0).get("url");
        url = url.substring(0, url.indexOf("&"));
        // 图片时间
        String enddate = (String)jsonArray.getJSONObject(0).get("enddate");
        // 图片版权
        String copyright = (String)jsonArray.getJSONObject(0).get("copyright");
        // 格式化为 MD 格式
        String text = String.format("%s | [%s](%s) ", enddate, copyright, url) + System.lineSeparator();
        System.out.println(text);
        // 写入 MD 文件
        Path path = Paths.get("README.md");
        if (!Files.exists(path)) {
            Files.createFile(path);
        }
        List<String> allLines = Files.readAllLines(path);
        allLines.set(0, text);
        Files.write(path, "## Bing Wallpaper".getBytes());
        Files.write(path, System.lineSeparator().getBytes(), StandardOpenOption.APPEND);
        Files.write(path, allLines, StandardOpenOption.APPEND);
    }
}

运行之后就可以得到必应网站当天的壁纸信息。

20210226 | [挂在锦里街上的红灯笼,中国成都 (© Philippe LEJEANVRE/Getty Images)](https://cn.bing.com/th?id=OHR.JinliStreet_ZH-CN3020276206_UHD.jpg)

Github Actions

如果我们想要收集每天的必应壁纸,岂不是每天都要运行一次爬虫程序?这显然太麻烦了。如果有个定时任务每天自动执行一次,岂不妙哉?但是挂在服务器上还需要购买一台虚拟主机,实在得不偿失。

这时机智的我突然想到何不利用 Github Actions 功能呢?Github Actions 可以执行多种常见环境的程序,而且可以定时触发,免费好用,实在是妙,心中默默的也为微软竖起了大拇指。

微信图片_20220414165621.jpg

img

下面会简单介绍一下 Github Actions 的使用,更多的关于 Github Actions 的概念和使用的场景就不介绍了,我们只要知道利用 Github Actions 功能,可以让我们在指定的事件触发(代码提交事件或者定时或者其他)时,可以运行指定的程序就好了。

如果想了解更多的相关资料,可以直接参考 Github Actions 官方文档,也可以参考其他的相关中文教程,链接这里已经放在文章末尾了。

Github Actions 体验

在 Github 仓库页面的 Actions 页签下可以创建 Github Actions 配置,这里创建一个官方提供的简单示例进行演示。

微信图片_20220414165625.jpgGithub Actions

创建后可以得到一个官方编写好的 Actions Demo,功能就是输出几个字符串。

微信图片_20220414165628.jpgGitHub Actions

简单介绍一下图中 Actions 配置文件中的的一些概念。

  1. on 指定此 Actions 的触发机制,这里的 pushpull_request 说明在代码提交和代码合并时会触发。
  2. jobs 代表一个任务,一个 Actions workflows 可以有多个 jobs 构成。
  3. runs-on 指定运行 Actions 的系统环境,这里是 ubuntu.
  4. steps 代表当前 jobs 任务的执行步骤。示例里先检出了仓库,然后echo 了几个字符串。

保存提交这个文件到仓库,因为配置里配置了触发机制有 push,所以这时也会触发这个任务。

微信图片_20220414165630.jpgGithub Actions

Github Actions 定时抓取必应壁纸

已经简单体验了 Github Actions 的使用方式,还记得我们上面编写了一个简单的必应壁纸 Java 版爬虫吗?如果我们把爬虫代码提交到仓库,然后使用 Github Actions 功能定时检出仓库运行 Java 代码抓取壁纸,再写入壁纸到仓库,一套下来无服务器零成本岂不是很好?

先直接附上写好的 Github 仓库地址:https://github.com/niumoo/bing-wallpaper ,已经可以每天自动抓取当天必应壁纸。

下面是关于 Actions 内容的一些说明。

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
  schedule:
    # 定时执行,Runs at 17:00 UTC every day
    - cron:  '0 17 * * *'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Build with Maven
      run: mvn -B package --file pom.xml
    - name: Run Java Application
      run: java -jar target/bing-wallpaper-jar-with-dependencies.jar
    - name: Commit files
      run: |
         git config --local user.email "your_github_email@126.com"
         git config --local user.name "your_github_name"
         git add README.md
         git commit -m "update readme.md"
    - name: Push changes
      uses:  ad-m/github-push-action@master
      with:
         github_token: ${{ secrets.MY_GIT_TOKEN }}
         branch: main

配置中定时在每天 UTC 时间 17 点运行一次,从 steps 可以看到执行步骤。

  1. 检出代码。
  2. 设置 Java 环境为 JDK 1.8.
  3. maven 编译打包。
  4. 运行打包后的 Java 程序(程序中把获取到的壁纸写入到了 README.md 文件)。
  5. 提交文件到 Github 仓库。

配置中最后还使用了一个参数 {{ secrets.MY_GIT_TOKEN }} ,这是一个用于识别是否有提交 Github权限的密文,这个密文可以在 Github 网站 -> 点击头像 -> Settings -> Developer settings -> Personal access tokens 这里创建,或者直接访问 https://github.com/settings/tokens/new 创建,创建时勾选 repo 权限。保存后可以得到你的密文。

微信图片_20220414165633.jpgGithub personal access tokens

复制这串密文,配置到自己创建 Actions 的仓库。

微信图片_20220414165636.jpgGithub Actions Secrets

至此,仓库和配置都已经完成,每天自动抓取必应首页壁纸写入到 README.md 文件,下图是抓取的效果。

微信图片_20220414165639.jpgbing-wallpaper

Github 仓库地址:https://github.com/niumoo/bing-wallpaper

参考

[1] https://docs.github.com/en/actions/quickstart

[2] https://github.com/niumoo/bing-wallpaper

相关文章
|
2月前
|
程序员 SEO
手把手教你搭建属于自己的网站(获取被动收入),无需服务器,使用github托管
手把手教你搭建属于自己的网站(获取被动收入),无需服务器,使用github托管
83 2
手把手教你搭建属于自己的网站(获取被动收入),无需服务器,使用github托管
|
2天前
|
Linux 网络安全 开发工具
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
10 0
|
2月前
|
安全 Shell 网络安全
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
131 0
|
6月前
|
Linux 网络安全 开发工具
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
|
11月前
|
IDE Linux 开发工具
git clone 从GitHub上下载项目到服务器上运行+创建虚拟环境
可先进入需要放置文件的路径之下
948 0
|
域名解析 网络协议 前端开发
不用买服务器,如何用github搭建网站
不用买服务器,如何用github搭建网站
694 0
|
PHP
github与gitee代码自动同步到服务器实现PHP项目自动部署webhooks
github与gitee代码自动同步到服务器实现PHP项目自动部署webhooks
400 0
github与gitee代码自动同步到服务器实现PHP项目自动部署webhooks
|
Ubuntu Linux PHP
如何搭建一个GitHub在自己的服务器上?
如何搭建一个GitHub在自己的服务器上?   下面推荐几种好用的工具或者软件:   GitLab支持在Ubuntu/Debian/CentOS上安装,社区版是免费的(Community Edition)https://about.
1863 0
|
1月前
|
人工智能 文字识别 异构计算
关于github开源ocr项目的疑问
小白尝试Python OCR学习,遇到报错。尝试Paddle OCR部署失败,Tesseract OCR在Colab误操作后恢复失败。EasyOCR在Colab和阿里天池Notebook成功,但GPU资源不足。其他平台部署不顺,决定使用WebUI或阿里云轻应用。求教OCR项目部署到本地及简单OCR项目推荐。
29 2
|
1月前
|
人工智能 自然语言处理 iOS开发
『GitHub项目圈选19』推荐5款本周 让人爱不释手 的开源项目
『GitHub项目圈选19』推荐5款本周 让人爱不释手 的开源项目