Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Java API - search repos

Tags:

github-api

I want to search Github repos based on size and keyword parameters. Here is the Java code I wrote:

package searchTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.SearchRepository;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.RepositoryService;

public class GithubSearch {
    private static Map<String, String> searchQuery = new HashMap<String, String>();

    public static void main(String[] args) throws IOException {
        GitHubClient client = new GitHubClient();
        client.setCredentials("username", "password");
        RepositoryService service = new RepositoryService(client);
        searchQuery.put("keyword","microsoft"); 
        searchQuery.put("size","304");
        List<SearchRepository> searchRes = service.searchRepositories(searchQuery);
        System.out.println("Search result "+searchRes.toString());
    }   
}

But the output I get is empty:

Search result []

I checked the GitHub Java API javadoc, but could not find a solution.

Thanks!

like image 959
Evgeny Arbatov Avatar asked Jul 04 '12 09:07

Evgeny Arbatov


People also ask

Can you search a repo in GitHub?

You can find repos in two ways: Type “14ers-git” in the github.com search bar to find the repository.

How do I get my GitHub API repository?

Hitting https://api.github.com/users/USERNAME/repos will list public repositories for the user USERNAME.


2 Answers

Try jcabi-github (I'm one of its developers):

final Github github = new RtGithub();
final JsonResponse resp = github.entry()
    .uri().path("/search/repositories")
    .queryParam("q", "java").back()
    .fetch()
    .as(JsonResponse.class);
final List<JsonObject> items = resp.json().readObject()
    .getJsonArray("items")
    .getValuesAs(JsonObject.class);
for (final JsonObject item : items) {
    System.out.println(
        String.format(
            "repository found: %s",
            item.get("full_name").toString()
        )
    );
}

Full example is here: https://github.com/jcabi/jcabi-github/tree/master/examples/search-repos

like image 115
yegor256 Avatar answered Oct 12 '22 22:10

yegor256


I ran sample code like below without size for keyword "java"

Map<String, String> searchQuery = new HashMap<String, String>();
searchQuery.put("keyword", "java");
List<SearchRepository> searchRes = null;
try {
    searchRes = service.searchRepositories(searchQuery);
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println("Search result "+searchRes.toString());

Output came like :

    Search result [michaelriha/WASC-Essay, sayems/Keyword-Driven-Framework, youdevise/karg, frinknet/DBG.js, andorp/testkeys, jpolitz/defaults.js, lsmith/article--javascript-this, qiaoyu314/TwitterMiner, cookieglitch/SearchToDB_Twitter, snoooze03/costOfVolatile, dziaduZLasu/java-critical-section-synchro, tvalletta/this-presentation, youdevise/jpoeisis, Sixtease/RtkInputMethod, metabrain/java8-plugin-persitent-local-vars, atestology/roark, jsmreese/pureshare-javascript-api, kentcdodds/genie, TARUNTEJAKOPPULA/Runnung-java-code-with-another-java-program,  aleSuglia/SitesLook, neetsdkasu/LittleLisp, odbol/Port-Java-to-C, artfulgeek/Hybrid-Search-and-Delivery-of-Learning-Objects, itcuties/Java-XStream-Complex-Example, codeheadzxc/TomsMovieFinder, truongsinh/generator, nsdevaraj/SwizDAO, dineshkummarc/jameleon-test-suite-3.3-RC1]

Now added size 196 to query search :

Map<String, String> searchQuery = new HashMap<String, String>();
searchQuery.put("keyword", "java");
searchQuery.put("size", "196");
List<SearchRepository> searchRes = null;
try {
    searchRes = service.searchRepositories(searchQuery);
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("Search result "+searchRes.toString());

and output came like :

    Search result [michaelriha/WASC-Essay]

I think "size" parameter is not to fix the number of search limit , but it is to get only those repositories with that size.

like image 44
Sanjiv Avatar answered Oct 13 '22 00:10

Sanjiv