From 4cbfeb50e8758a0c26e3e99f87fb3a65ae4152ed Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 28 Mar 2021 22:20:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=9B=B8=E5=90=8C=E5=88=99?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E8=B7=9F=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zeekling/blog/BlogUpdateServiceImpl.java | 14 +++++----- src/main/java/com/zeekling/util/GitHubs.java | 26 ++++++++++++++++--- .../com/zeekling/blog/BlogUpdateTest.java | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/zeekling/blog/BlogUpdateServiceImpl.java b/src/main/java/com/zeekling/blog/BlogUpdateServiceImpl.java index 4ed0f68..549047a 100644 --- a/src/main/java/com/zeekling/blog/BlogUpdateServiceImpl.java +++ b/src/main/java/com/zeekling/blog/BlogUpdateServiceImpl.java @@ -16,9 +16,7 @@ import java.net.MalformedURLException; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.HashSet; import java.util.List; -import java.util.Set; /** * @author zeekling [lingzhaohui@zeekling.cn] @@ -62,6 +60,12 @@ public class BlogUpdateServiceImpl implements BlogUpdateService { return -1; } final String readme = genSoloBlogReadme(loginName + "/" + blogConfigure.getRepoName()); + LOGGER.log(Level.INFO, "begin get README.md"); + final String oldReadname = GitHubs.getGitHubFile("README.md", blogConfigure.getRepoName(), blogConfigure.getPat(), "master"); + if (oldReadname != null && oldReadname.equals(readme)) { + LOGGER.log(Level.INFO, "do not need to update file:README.md"); + return 0; + } ok = GitHubs.updateFile(blogConfigure.getPat(), loginName, blogConfigure.getRepoName(), "README.md", readme.getBytes(StandardCharsets.UTF_8)); if (ok) { LOGGER.log(Level.INFO, "Exported public articles to your repo [" + blogConfigure.getRepoName() + "]"); @@ -82,15 +86,13 @@ public class BlogUpdateServiceImpl implements BlogUpdateService { } catch (FeedException | MalformedURLException e) { e.printStackTrace(); } - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String currentDate = format.format(new Date()); bodyBuilder.append("\n\n"); - String ret = "### Hey \uD83D\uDC4B, I'm [Zeek Ling](https://www.zeekling.cn)! \n" + + String ret = "### Hey \uD83D\uDC4B, I'm [ZEEKLING](https://www.zeekling.cn)! \n" + "![Github Stats](https://github-readme-stats.vercel.app/api?username=zeekling&show_icons=true) \n" + "### 我在[小令童鞋](https://www.zeekling.cn)的近期动态\n" + "\n" + - "⭐️ Star [个人主页](https://github.com/zeekling/zeekling) 后会自动更新,最近更新时间:`" + currentDate + "`\n" + + "⭐️ Star [个人主页](https://github.com/zeekling/zeekling) 后会自动更新" + "\n

\"${title}\"

" + "${title}\n" + "

\n" + diff --git a/src/main/java/com/zeekling/util/GitHubs.java b/src/main/java/com/zeekling/util/GitHubs.java index 8e06c1c..a7fc3ac 100644 --- a/src/main/java/com/zeekling/util/GitHubs.java +++ b/src/main/java/com/zeekling/util/GitHubs.java @@ -66,6 +66,28 @@ public final class GitHubs { } } + public static String getGitHubFile(final String filePath, final String fullRepoName, final String pat, final String branchName) { + try { + final HttpResponse res = HttpRequest.get("https://raw.githubusercontent.com/" + fullRepoName + "/" + branchName + "/" + filePath) + .header("Authorization", "token " + pat) + .connectionTimeout(7000) + .timeout(60000) + .header("User-Agent", GitHubConstants.USER_AGENT) + .send(); + int code = res.statusCode(); + res.charset("UTF-8"); + String responseBody = res.bodyText(); + if (200 != code) { + LOGGER.log(Level.ERROR, "Get git tree of file [" + filePath + "] failed: " + responseBody); + return null; + } + return responseBody; + } catch (final Exception e) { + LOGGER.log(Level.ERROR, "Gets GitHub file failed", e); + } + return null; + } + /** * Updates a file by the specified personal access token, GitHub login name, repo name, file path and file content. * @@ -89,9 +111,7 @@ public final class GitHubs { return false; } - final JSONObject body = new JSONObject(). - put("message", ":memo: 更新博客"). - put("content", Base64.getEncoder().encodeToString(content)); + final JSONObject body = new JSONObject().put("message", ":memo: 自动更新博客").put("content", Base64.getEncoder().encodeToString(content)); if (200 == statusCode) { final JSONObject responseData = new JSONObject(responseBody); final JSONArray tree = responseData.optJSONArray("tree"); diff --git a/src/test/java/com/zeekling/blog/BlogUpdateTest.java b/src/test/java/com/zeekling/blog/BlogUpdateTest.java index 364131f..e076fe9 100644 --- a/src/test/java/com/zeekling/blog/BlogUpdateTest.java +++ b/src/test/java/com/zeekling/blog/BlogUpdateTest.java @@ -14,7 +14,7 @@ public class BlogUpdateTest { @Test public void updateGitHub(){ - String pat = "068783457fa6e084aad1342ed2730f33a0255b97"; + String pat = "c9e0b35a43858edbb75bf3bcb4731b8227a82687"; final JSONObject gitHubUser = GitHubs.getGitHubUser(pat); if (null == gitHubUser) { return;