Skip to content

Instantly share code, notes, and snippets.

@gMan1990
Last active December 26, 2018 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gMan1990/c67f1e8664f323d34fbf8eee821d018a to your computer and use it in GitHub Desktop.
Save gMan1990/c67f1e8664f323d34fbf8eee821d018a to your computer and use it in GitHub Desktop.
获取github repo与它的forks的每个分支的最后提交时间
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {
public static void main(String[] args) {
ExecutorService commonThreadPool = Executors.newFixedThreadPool(10);
ExecutorService insideThreadPool = Executors.newFixedThreadPool(10);
try {
Elements forks = Jsoup
.connect("https://github.com/constverum/ProxyBroker/network/members").get()
.select("#network>div.repo a:last-child");
List<String> timeBranchList = Collections
.synchronizedList(new ArrayList<>(forks.size() * 3));
CountDownLatch latch = new CountDownLatch(forks.size());
for (Element fork : forks) {
commonThreadPool.execute(() -> {
try {
execute(fork, insideThreadPool, timeBranchList);
} finally {
latch.countDown();
}
});
}
latch.await();
timeBranchList.sort(null);
System.out.println(StringUtils.join(timeBranchList, '\n'));
commonThreadPool.shutdown();
insideThreadPool.shutdown();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private static void execute(Element fork, ExecutorService threadPool,
List<String> timeBranchList) {
try {
Document forkDoc = Jsoup.connect("https://github.com" + fork.attr("href")).get();
Elements branches = forkDoc.select("tab-container>div.select-menu-list").get(0)
.select("a");
CountDownLatch latch = new CountDownLatch(branches.size());
for (Element branch : branches) {
threadPool.execute(() -> {
try {
Document forkDocToUse;
if ("true".equals(branch.attr("aria-selected"))) {
forkDocToUse = forkDoc;
} else {
forkDocToUse = Jsoup.connect("https://github.com" + branch.attr("href"))
.get();
}
Elements relativeTime = forkDocToUse.select("relative-time");
if (!relativeTime.isEmpty()) {
timeBranchList.add(
relativeTime.attr("datetime") + '\t' + forkDocToUse.baseUri());
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
latch.countDown();
}
});
}
latch.await();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment