test for get all reps ion github #1

Merged
zeekling merged 1 commits from all_reps into master 2022-04-09 15:24:30 +00:00
4 changed files with 66 additions and 8 deletions

View File

@ -1,5 +1,7 @@
package com.zeekling.blog;
import org.json.JSONArray;
/**
* @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0
@ -11,4 +13,6 @@ public interface BlogUpdateService {
int update();
JSONArray getGitHubRepos();
}

View File

@ -7,15 +7,22 @@ import com.zeekling.util.ConfigureUtil;
import com.zeekling.util.FeedXmlUtil;
import com.zeekling.util.FileUtils;
import com.zeekling.util.GitHubs;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
/**
* @author zeekling [lingzhaohui@zeekling.cn]
@ -112,4 +119,19 @@ public class BlogUpdateServiceImpl implements BlogUpdateService {
return ret;
}
@Override
public JSONArray getGitHubRepos() {
if (blogConfigure == null) {
return null;
}
final JSONObject gitHubUser = GitHubs.getGitHubUser(blogConfigure.getPat());
if (null == gitHubUser) {
LOGGER.error("login failed");
return null;
}
LOGGER.info("login success");
final String myGitHubID = gitHubUser.optString("login");
return GitHubs.getGitHubRepos(myGitHubID);
}
}

View File

@ -47,19 +47,14 @@ public final class GitHubs {
*/
public static JSONArray getGitHubRepos(final String githubUserId) {
try {
final HttpResponse res = HttpRequest.get("https://hacpai.com/github/repos?id=" + githubUserId).trustAllCerts(true).
connectionTimeout(3000).timeout(7000).header("User-Agent", GitHubConstants.USER_AGENT).send();
final HttpResponse res = HttpRequest.get("https://api.github.com/users/" + githubUserId + "/repos").
connectionTimeout(20000).timeout(60000).header("User-Agent", GitHubConstants.USER_AGENT).send();
if (200 != res.statusCode()) {
LOGGER.error("error code:" + res.statusCode());
return null;
}
res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(GitHubConstants.CODE)) {
return null;
}
final JSONObject data = result.optJSONObject(GitHubConstants.DATA);
return data.optJSONArray("githubrepos");
return new JSONArray(res.bodyText());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub repos failed", e);
return null;

View File

@ -1,5 +1,11 @@
package com.zeekling.util;
import com.zeekling.blog.BlogUpdateService;
import com.zeekling.blog.BlogUpdateServiceImpl;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.annotations.Test;
/**
* @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0
@ -8,6 +14,37 @@ package com.zeekling.util;
*/
public class GitHubsTest {
@Test
public void getAll() {
String configPath = "/home/zeekling/project/ling/github_zeekling/src/main/resources/blog.properties";
BlogUpdateService updateService = new BlogUpdateServiceImpl(configPath);
JSONArray result = updateService.getGitHubRepos();
JSONArray compatibleResult = new JSONArray();
for (int i = 0; i < result.length(); i++){
JSONObject resultObject = result.optJSONObject(i);
JSONObject compatibleObject = new JSONObject();
if (resultObject.getBoolean("fork")) {
continue;
}
compatibleObject.put("githubrepoId", resultObject.optString("id"));
compatibleObject.put("githubrepoStatus", 0);
compatibleObject.put("oId", "" + System.currentTimeMillis());
compatibleObject.put("githubrepoDescription", resultObject.optString("description"));
compatibleObject.put("githubrepoHomepage", resultObject.optString("homepage"));
compatibleObject.put("githubrepoForksCount", resultObject.optLong("forks_count"));
compatibleObject.put("githubrepoOwnerId", resultObject.optJSONObject("owner").optString("id"));
compatibleObject.put("githubrepoStargazersCount", resultObject.optLong("stargazers_count"));
compatibleObject.put("githubrepoWatchersCount", resultObject.optLong("watchers_count"));
compatibleObject.put("githubrepoOwnerLogin", resultObject.optJSONObject("owner").optString("login"));
compatibleObject.put("githubrepoHTMLURL", resultObject.optString("html_url"));
compatibleObject.put("githubrepoLanguage", resultObject.optString("language"));
compatibleObject.put("githubrepoName", resultObject.optString("name"));
compatibleObject.put("githubrepoFullName", resultObject.optString("full_name"));
compatibleResult.put(compatibleObject);
}
System.out.println(compatibleResult);
}
}