判断相同则不需要跟新

This commit is contained in:
LingZhaoHui 2021-03-28 22:20:23 +08:00
parent b468e94c51
commit 4cbfeb50e8
3 changed files with 32 additions and 10 deletions

View File

@ -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<p align=\"center\"><img alt=\"${title}\" src=\"${favicon}\"></p><h2 align=\"center\">" +
"${title}\n" +
"</h2>\n" +

View File

@ -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");

View File

@ -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;