判断相同则不需要跟新

This commit is contained in:
LingZhaoHui 2021-03-29 21:17:56 +08:00
parent 4cbfeb50e8
commit 73b132c68d
8 changed files with 118 additions and 47 deletions

View File

@ -5,6 +5,7 @@ import com.rometools.rome.io.FeedException;
import com.zeekling.conf.BlogConfigure; import com.zeekling.conf.BlogConfigure;
import com.zeekling.util.ConfigureUtil; import com.zeekling.util.ConfigureUtil;
import com.zeekling.util.FeedXmlUtil; import com.zeekling.util.FeedXmlUtil;
import com.zeekling.util.FileUtils;
import com.zeekling.util.GitHubs; import com.zeekling.util.GitHubs;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.apache.log4j.LogManager; import org.apache.log4j.LogManager;
@ -14,8 +15,6 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -30,8 +29,6 @@ public class BlogUpdateServiceImpl implements BlogUpdateService {
private BlogConfigure blogConfigure = null; private BlogConfigure blogConfigure = null;
private static String HTML_REGEX="<[^>]+>";
public BlogUpdateServiceImpl(String confPath) { public BlogUpdateServiceImpl(String confPath) {
// init blogConfigure // init blogConfigure
try { try {
@ -61,15 +58,16 @@ public class BlogUpdateServiceImpl implements BlogUpdateService {
} }
final String readme = genSoloBlogReadme(loginName + "/" + blogConfigure.getRepoName()); final String readme = genSoloBlogReadme(loginName + "/" + blogConfigure.getRepoName());
LOGGER.log(Level.INFO, "begin get README.md"); LOGGER.log(Level.INFO, "begin get README.md");
final String oldReadname = GitHubs.getGitHubFile("README.md", blogConfigure.getRepoName(), blogConfigure.getPat(), "master"); final String oldReadme = FileUtils.readFile(blogConfigure.getGithubTmp() + "/README.md");
if (oldReadname != null && oldReadname.equals(readme)) { if (oldReadme != null && oldReadme.equals(readme)) {
LOGGER.log(Level.INFO, "do not need to update file:README.md"); LOGGER.info("not need update readme.");
return 0; return 0;
} }
ok = GitHubs.updateFile(blogConfigure.getPat(), loginName, blogConfigure.getRepoName(), "README.md", readme.getBytes(StandardCharsets.UTF_8)); ok = GitHubs.updateFile(blogConfigure.getPat(), loginName, blogConfigure.getRepoName(), "README.md", readme.getBytes(StandardCharsets.UTF_8));
if (ok) { if (ok) {
LOGGER.log(Level.INFO, "Exported public articles to your repo [" + blogConfigure.getRepoName() + "]"); LOGGER.log(Level.INFO, "Exported public articles to your repo [" + blogConfigure.getRepoName() + "]");
} }
FileUtils.saveDataToFile(blogConfigure.getGithubTmp() + "/README.md", readme);
return 0; return 0;
} }

View File

@ -28,8 +28,19 @@ public final class BlogConfigure {
@Value(name = "blog.rss") @Value(name = "blog.rss")
private String rss; private String rss;
@Value(name = "github.tmp")
private String githubTmp;
public String getClientTitle() { public String getGithubTmp() {
return githubTmp;
}
public void setGithubTmp(String githubTmp) {
this.githubTmp = githubTmp;
}
public String getClientTitle() {
return clientTitle; return clientTitle;
} }
@ -84,4 +95,6 @@ public final class BlogConfigure {
public void setRss(String rss) { public void setRss(String rss) {
this.rss = rss; this.rss = rss;
} }
} }

View File

@ -6,7 +6,6 @@ import org.apache.commons.lang.StringUtils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Properties; import java.util.Properties;
@ -28,8 +27,8 @@ public final class ConfigureUtil {
return properties; return properties;
} }
@SuppressWarnings("ReflectionForUnavailableAnnotation") @SuppressWarnings("deprecation")
public static <T> T getNewInstants(String filePath, Class<T> tClass) public static <T> T getNewInstants(String filePath, Class<T> tClass)
throws IllegalAccessException, InstantiationException, IOException { throws IllegalAccessException, InstantiationException, IOException {
T t = tClass.newInstance(); T t = tClass.newInstance();
Properties properties = readProperties(filePath); Properties properties = readProperties(filePath);

View File

@ -0,0 +1,66 @@
package com.zeekling.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public final class FileUtils {
public static void saveDataToFile(String filePath, String context) {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
writer.write(context);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String readFile(String filePath) {
File file = new File(filePath);
if (file == null || !file.exists()) {
return null;
}
BufferedReader reader = null;
StringBuilder res = new StringBuilder();
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while ((tempString = reader.readLine()) != null){
res.append(tempString).append("\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res.toString();
}
}

View File

@ -66,28 +66,6 @@ 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. * Updates a file by the specified personal access token, GitHub login name, repo name, file path and file content.
* *

View File

@ -1,7 +1,8 @@
blog.title=小令童鞋 blog.title=ZEEKLING
blog.subTitle=梅干菜你个小酥饼哦。 blog.subTitle=Stay simple, stay naive.
blog.home=https://www/zeekling.cn blog.home=https://www/zeekling.cn
blog.rss=https://www.zeekling.cn/rss.xml blog.rss=https://www.zeekling.cn/rss.xml
blog.favicon=https://img.zeekling.cn/images/2020/02/23/logo.th.png blog.favicon=https://img.zeekling.cn/images/2020/02/23/logo.th.png
github.pat=068783457fa6e084aad1342ed2730f33a0255b97 github.pat=068783457fa6e084aad1342ed2730f33a0255b97
github.repoName=zeekling github.repoName=zeekling
github.tmp=/tmp

View File

@ -14,19 +14,14 @@ public class BlogUpdateTest {
@Test @Test
public void updateGitHub(){ public void updateGitHub(){
String pat = "c9e0b35a43858edbb75bf3bcb4731b8227a82687"; String configPath = "/home/zeek/project/github_zeekling/src/main/resources/blog.properties";
final JSONObject gitHubUser = GitHubs.getGitHubUser(pat); BlogUpdateService updateService = new BlogUpdateServiceImpl(configPath);
if (null == gitHubUser) { int res = updateService.update();
if (res == 0){
System.out.println("update github success!");
return; return;
} }
System.out.println("update github failed!");
final String loginName = gitHubUser.optString("login");
final String repoName = "zeekling";
boolean ok = GitHubs.createOrUpdateGitHubRepo(pat, loginName, repoName, "跟新主页", "https://www.zeekling.cn/");
if (!ok) {
return;
}
// final String readme = genSoloBlogReadme(clientTitle, clientSubtitle, preference.optString(Option.ID_C_FAVICON_URL), loginName + "/" + repoName);
} }
} }

View File

@ -0,0 +1,21 @@
package com.zeekling.util;
import org.testng.Assert;
import org.testng.annotations.Test;
public class FileUtilsTest {
private String context = "test";
@Test
public void writeFile() {
FileUtils.saveDataToFile("/tmp/README.md", context);
}
@Test
public void readFile() {
String fileContext = FileUtils.readFile("/tmp/README.md");
System.out.println(fileContext);
}
}