吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1609|回复: 14
收起左侧

[Java 原创] 使用github action每天自动抓取bing壁纸并进行汇总

  [复制链接]
ameiz 发表于 2023-8-22 12:56
Wallpaper.java
[Java] 纯文本查看 复制代码
package net.ameizi.wallpaper;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Wallpaper {

    private static final Path readmePath = Paths.get("README.md");
    private static final Path wallpaperListPath = Paths.get("bing-wallpaper.md");

    // 请求API地址
    private static final String BING_API = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1618537156988&pid=hp&uhd=1&uhdwidth=3840&uhdheight=2160";
    // 最近7天
    private static final String BING_7DAYS_API = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=%d&n=1&nc=1618537156988&pid=hp&uhd=1&uhdwidth=3840&uhdheight=2160";
    // 图片访问地址
    private static final String BING_URL = "https://cn.bing.com%s";

    public static void main(String[] args) throws Exception {
        // 读取 wallpaper 列表
        List<Image> imageList = readWallPaperList();
        // imageList = last7Days(imageList);
        // 请求网络图片
        String resp = HttpUtil.get(BING_API);
        // 解析成 Image对象
        Image image = parse(resp);
        // 在imageList下标为0的位置插入image
        imageList.add(0, image);
        // 写入bing-wallpaper.md文件
        writeToWallPaperList(imageList);
        // 写入README.md文件
        writeToReadme(imageList);
    }

    /**
     * 请求最近7天的数据
     *
     * [url=home.php?mod=space&uid=952169]@Param[/url] imageList
     * @return
     */
    private static List<Image> last7Days(List<Image> imageList) {
        for (int i = 0; i < 8; i++) {
            String resp = HttpUtil.get(String.format(BING_7DAYS_API, i));
            imageList.add(parse(resp));
        }
        return imageList;
    }

    /**
     * 解析请求体json字符串
     *
     * @param resp
     * @return
     */
    private static Image parse(String resp) {
        JSONObject jsonObject = JSONUtil.parseObj(resp);
        JSONObject images = jsonObject.getJSONArray("images").get(0, JSONObject.class);

        String url = String.format(BING_URL, images.getStr("url"));
        url = url.substring(0, url.indexOf("&"));
        String desc = images.getStr("copyright");
        String enddate = images.getStr("enddate");

        LocalDate localDate = LocalDate.parse(enddate, DateTimeFormatter.BASIC_ISO_DATE);
        enddate = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);

        return new Image(enddate, desc, url);
    }

    /**
     * 解析 bing-wallpaper.md
     *
     * @return
     * @throws Exception
     */
    public static List<Image> readWallPaperList() throws Exception {
        List<Image> imageList = new ArrayList<>();
        Files.readAllLines(wallpaperListPath, StandardCharsets.UTF_8).forEach(line -> {
            Pattern pattern = Pattern.compile("(.*)\\s\\|.*\\[(.*)\\]\\((.*)\\)");
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                Image image = new Image(matcher.group(1), matcher.group(2), matcher.group(3));
                imageList.add(image);
            }
        });
        return imageList;
    }

    /**
     * 写入 bing-wallpaper.md
     *
     * @param imageList
     * @throws Exception
     */
    public static void writeToWallPaperList(List<Image> imageList) throws Exception {
        File wallpaper = wallpaperListPath.toFile();
        if (!wallpaper.exists()) {
            wallpaper.createNewFile();
        }
        FileUtil.writeUtf8String("## Bing Wallpaper", wallpaper);
        FileUtil.appendUtf8String(System.lineSeparator() + System.lineSeparator(), wallpaper);
        imageList.stream().distinct().collect(Collectors.toList()).forEach(item -> {
            FileUtil.appendUtf8String(item.markdown(), wallpaper);
        });
    }

    /**
     * 写入 README.md
     *
     * @param imageList
     * @throws Exception
     */
    public static void writeToReadme(List<Image> imageList) throws Exception {
        File readme = readmePath.toFile();
        if (!readme.exists()) {
            readme.createNewFile();
        }
        FileUtil.writeUtf8String("## Bing Wallpaper", readme);
        FileUtil.appendUtf8String(System.lineSeparator() + System.lineSeparator(), readme);
        // 取出第一个元素设为首图
        Image image = imageList.get(0);
        String top = String.format("![%s](%s)", image.getDesc(), image.largeUrl()) + System.lineSeparator();
        FileUtil.appendUtf8String(top, readme);
        // 设置描述内容
        String today = String.format("Today: [%s](%s)", image.getDesc(), image.getUrl()) + System.lineSeparator();
        FileUtil.appendUtf8String(today, readme);
        // 拼markdown表头
        FileUtil.appendUtf8String("|      |      |      |" + System.lineSeparator(), readme);
        FileUtil.appendUtf8String("| :--: | :--: | :--: |" + System.lineSeparator(), readme);
        List<Image> images = imageList.stream().distinct().collect(Collectors.toList());
        int i = 1;
        for (Image item : images) {
            // 写入markdown格式字符串
            FileUtil.appendUtf8String("|" + item.toString(), readme);
            // 每行三列,若刚整除,补每行末尾最后一个"|"
            if (i % 3 == 0) {
                FileUtil.appendUtf8String("|" + System.lineSeparator(), readme);
            }
            // 行数加1
            i++;
        }
        if (i % 3 != 1) {
            FileUtil.appendUtf8String("|" + System.lineSeparator(), readme);
        }
    }

}

ci.yml
[Plain Text] 纯文本查看 复制代码
# 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:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: 0 */6 * * * 
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          java-version: '8'
          distribution: 'adopt'
      - 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 "sxyx2008@163.com"
          git config --local user.name "github-actions"
          git add README.md
          git add bing-wallpaper.md
          git commit -m "Update README.md & bing-wallpaper.md"
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}



最终效果如下:
Snipaste_2023-08-22_12-50-19.png


完整代码:https://github.com/ameizi/bing-wallpaper



Snipaste_2023-08-22_12-54-24.png

免费评分

参与人数 5吾爱币 +12 热心值 +4 收起 理由
unbounded + 1 + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
declandragon + 1 我很赞同!
wkdxz + 2 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
呵呵0214 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

declandragon 发表于 2023-8-22 17:44
感谢楼主,我这边搞成了 https://github.com/declandragon/bing-wallpaper

步骤
1、fork 楼主的仓库
2、修改了一个邮箱 https://github.com/declandragon/bing-wallpaper/blob/main/.github/workflows/ci.yml#L33
3、仓库的 settings - actions - General - Workflow permissions - 选择  Read and write permissions  - save
4、仓库的 actions,好像有个 enable Workflow ,然后左侧 Java CI with Maven - 右侧 Run workflow

执行完成就 ok 啦
13599383608 发表于 2023-8-22 15:36
wkdxz 发表于 2023-8-22 15:56
感谢分享,楼主出一期配置github action的图文,肯定受欢迎
sosaid 发表于 2023-8-22 16:40
请问能把图片打包分享一下吗?
 楼主| ameiz 发表于 2023-8-22 17:24
sosaid 发表于 2023-8-22 16:40
请问能把图片打包分享一下吗?

可以写个python爬虫把我的那个汇总页抓一遍就好了。
 楼主| ameiz 发表于 2023-8-22 17:25
wkdxz 发表于 2023-8-22 15:56
感谢分享,楼主出一期配置github action的图文,肯定受欢迎

在当前这个项目中github action不需要做额外的配置。
declandragon 发表于 2023-8-22 17:45
wkdxz 发表于 2023-8-22 15:56
感谢分享,楼主出一期配置github action的图文,肯定受欢迎

看下七楼哦,我写了一下操作步骤  
hubspring 发表于 2023-8-23 09:13
这个牛了,Java爬虫
15705107305 发表于 2023-8-23 14:14
可以写个python爬虫把我的那个汇总页抓一遍就好了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-2 17:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表