Skip to content

Commit

Permalink
feat: 新增功能
Browse files Browse the repository at this point in the history
1. 添加写入文件时,文件名带日期格式输出
2. workflows 代码添加删除旧备份文件操作
  • Loading branch information
sudojia committed May 19, 2024
1 parent a869f6a commit cbbd174
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
32 changes: 26 additions & 6 deletions .github/workflows/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,37 @@ jobs:
restore-keys: |
${{ runner.os }}-maven-
- name: Set Git Config
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Build with Maven
env:
PASSWORD: ${{ secrets.PASSWORD }}
TWIKOO_URL: ${{ secrets.TWIKOO_URL }}
run: |
mvn compile exec:java -Dexec.mainClass="cn.imzjw.comment.TwikooBackups" -Dexec.args="${PASSWORD} ${TWIKOO_URL}"
- name: Commit and push
- name: Delete old backup files in repository
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add /home/runner/work/comment_backups/comment_backups/twikoo-comment.json
git commit -m "Add twikoo-comment.json" || echo "No changes to commit"
git push
FILES=$(ls /home/runner/work/comment_backups/comment_backups/twikoo-comment*.json)
if [ -n "$FILES" ]; then
git rm $FILES
FILE_NAMES=$(echo "$FILES" | awk -F'/' '{print $NF}')
git commit -m "Delete Old File $FILE_NAMES" || echo "No old backup files to delete"
else
echo "No old backup files to delete"
fi
- name: Clean up and commit new file
run: |
git add /home/runner/work/comment_backups/comment_backups/twikoo-comment*.json
git commit -m "Add twikoo-comment-$(date +%Y-%m-%d).json" || echo "No changes to commit"
git push
- name: Delete Workflow Runs
uses: Mattraks/delete-workflow-runs@main
with:
retain_days: 1
keep_minimum_runs: 1
10 changes: 8 additions & 2 deletions src/main/java/cn/imzjw/comment/TwikooBackups.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -36,7 +38,7 @@ public class TwikooBackups {
/**
* json 名称
*/
private static final String TWIKOO_COMMENT_JSON = "twikoo-comment.json";
private static final String TWIKOO_COMMENT_JSON = "twikoo-comment";
/**
* 请求头,指定发送的内容类型为 JSON
*/
Expand Down Expand Up @@ -113,12 +115,16 @@ public static void requestTwikoo(String password, String twikooUrl) {
*/
private static void saveToFile(String content) {
try {
// 获取当前日期
String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// 构建新的文件名,包含日期
String fileNameWithDate = TWIKOO_COMMENT_JSON + "-" + currentDate + ".json";
// 解析原始 JSON 字符串,提取 "data" 数组
JSONArray dataArray = new JSONObject(content).getJSONArray("data");
// 将 JSON 数组转换为字符串
String data = dataArray.toString();
// 写入数组字符串到文件
File file = new File(TWIKOO_COMMENT_JSON);
File file = new File(fileNameWithDate);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(data.getBytes());
}
Expand Down
1 change: 0 additions & 1 deletion twikoo-comment.json

This file was deleted.

0 comments on commit cbbd174

Please sign in to comment.