Get the list of all public repositories from Github Using Java

You may want to dump all public repositories from Github. First you need to get the list of the names of repositories. The following code can do that for you.

There is a limit of number of requests from one machine. So you may want to create a token first, to raise the limit, so that you can get as many as possible.

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
 
public class GitHubRepository {
 
	public static void main(String[] args) {
		for (int i = 0; i < 10000;) {
			getEach(i);
			i = i + 200;
		}
	}
 
	public static void getEach(int since) {
		String url = "https://api.github.com/repositories?since=" + since+"&access_token=you-own-token";
		//String url = "https://api.github.com/repositories?since=" + since;
		try {
			CloseableHttpClient httpClient = HttpClientBuilder.create().build();
			HttpGet request = new HttpGet(url);
			request.addHeader("content-type", "application/json");
			HttpResponse result = httpClient.execute(request);
			String json = EntityUtils.toString(result.getEntity(), "UTF-8");
 
			System.out.println(json);
 
			JsonElement jelement = new JsonParser().parse(json);
			JsonArray jarr = jelement.getAsJsonArray();
			for (int i = 0; i < jarr.size(); i++) {
				JsonObject jo = (JsonObject) jarr.get(i);
				String fullName = jo.get("full_name").toString();
				fullName = fullName.substring(1, fullName.length()-1);
				System.out.println(fullName);
			}
 
		} catch (IOException ex) {
			System.out.println(ex.getStackTrace());
		}
	}
}

The required jar files include the following:
httpclient-4.x.x.jar
httpcore-4.x.x.jar
gson-2.1.jar

2 thoughts on “Get the list of all public repositories from Github Using Java”

Leave a Comment