判断相同则不需要跟新
This commit is contained in:
parent
4cbfeb50e8
commit
73b132c68d
@ -5,6 +5,7 @@ import com.rometools.rome.io.FeedException;
|
||||
import com.zeekling.conf.BlogConfigure;
|
||||
import com.zeekling.util.ConfigureUtil;
|
||||
import com.zeekling.util.FeedXmlUtil;
|
||||
import com.zeekling.util.FileUtils;
|
||||
import com.zeekling.util.GitHubs;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.LogManager;
|
||||
@ -14,8 +15,6 @@ import org.json.JSONObject;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -30,8 +29,6 @@ public class BlogUpdateServiceImpl implements BlogUpdateService {
|
||||
|
||||
private BlogConfigure blogConfigure = null;
|
||||
|
||||
private static String HTML_REGEX="<[^>]+>";
|
||||
|
||||
public BlogUpdateServiceImpl(String confPath) {
|
||||
// init blogConfigure
|
||||
try {
|
||||
@ -61,15 +58,16 @@ public class BlogUpdateServiceImpl implements BlogUpdateService {
|
||||
}
|
||||
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;
|
||||
final String oldReadme = FileUtils.readFile(blogConfigure.getGithubTmp() + "/README.md");
|
||||
if (oldReadme != null && oldReadme.equals(readme)) {
|
||||
LOGGER.info("not need update readme.");
|
||||
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() + "]");
|
||||
}
|
||||
FileUtils.saveDataToFile(blogConfigure.getGithubTmp() + "/README.md", readme);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -28,8 +28,19 @@ public final class BlogConfigure {
|
||||
|
||||
@Value(name = "blog.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;
|
||||
}
|
||||
|
||||
@ -84,4 +95,6 @@ public final class BlogConfigure {
|
||||
public void setRss(String rss) {
|
||||
this.rss = rss;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import org.apache.commons.lang.StringUtils;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -28,8 +27,8 @@ public final class ConfigureUtil {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ReflectionForUnavailableAnnotation")
|
||||
public static <T> T getNewInstants(String filePath, Class<T> tClass)
|
||||
@SuppressWarnings("deprecation")
|
||||
public static <T> T getNewInstants(String filePath, Class<T> tClass)
|
||||
throws IllegalAccessException, InstantiationException, IOException {
|
||||
T t = tClass.newInstance();
|
||||
Properties properties = readProperties(filePath);
|
||||
|
66
src/main/java/com/zeekling/util/FileUtils.java
Normal file
66
src/main/java/com/zeekling/util/FileUtils.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
@ -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.
|
||||
*
|
||||
|
@ -1,7 +1,8 @@
|
||||
blog.title=小令童鞋
|
||||
blog.subTitle=梅干菜你个小酥饼哦。
|
||||
blog.title=ZEEKLING
|
||||
blog.subTitle=Stay simple, stay naive.
|
||||
blog.home=https://www/zeekling.cn
|
||||
blog.rss=https://www.zeekling.cn/rss.xml
|
||||
blog.favicon=https://img.zeekling.cn/images/2020/02/23/logo.th.png
|
||||
github.pat=068783457fa6e084aad1342ed2730f33a0255b97
|
||||
github.repoName=zeekling
|
||||
github.tmp=/tmp
|
@ -14,19 +14,14 @@ public class BlogUpdateTest {
|
||||
|
||||
@Test
|
||||
public void updateGitHub(){
|
||||
String pat = "c9e0b35a43858edbb75bf3bcb4731b8227a82687";
|
||||
final JSONObject gitHubUser = GitHubs.getGitHubUser(pat);
|
||||
if (null == gitHubUser) {
|
||||
String configPath = "/home/zeek/project/github_zeekling/src/main/resources/blog.properties";
|
||||
BlogUpdateService updateService = new BlogUpdateServiceImpl(configPath);
|
||||
int res = updateService.update();
|
||||
if (res == 0){
|
||||
System.out.println("update github success!");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
System.out.println("update github failed!");
|
||||
}
|
||||
|
||||
}
|
||||
|
21
src/test/java/com/zeekling/util/FileUtilsTest.java
Normal file
21
src/test/java/com/zeekling/util/FileUtilsTest.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user