Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort/filter Google Code projects by stars?

Tags:

google-code

I would like to know what projects hosted on Google Code that have the most stars, especially when searching by label, like this:

https://code.google.com/hosting/search?q=label%3aAndroid

like image 987
CodeMonkey Avatar asked May 30 '13 07:05

CodeMonkey


1 Answers

Sort by stars is not supported on the project search page. Was able to write some page scrapping code to get the required information.

Hope it helps.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class ReadGoogleProjectSortByStars {

    public static void main(String[] args) throws IOException {
        String urlPath = "https://code.google.com/hosting/search?q=label%3AAndroid&filter=0&mode=&start=";
        // urlPath = "https://code.google.com/hosting/search?q=label%3AAndroid+stackoverflow&projectsearch=Search+projects&filter=0&mode=&start=";
        int start = 0;
        List<Project> projects = new ArrayList<Project>();
        boolean done = false;
        while(!done) {
            String urlStr = urlPath + start;
            URL url = new URL(urlStr);
            BufferedReader in = new BufferedReader(
            new InputStreamReader(url.openStream()));

            String inputLine;
            String projectUrl = null, stars = null;
            while ((inputLine = in.readLine()) != null) {
                int urlIndex = -1, starIndex = -1;
                if(inputLine.contains(" style=\"font-size:medium\">") && (urlIndex = inputLine.indexOf(" href=\"/p/")) != -1) {
                    if(projectUrl != null) {
                        Project project = new Project();
                        project.url = projectUrl;
                        project.stars = "0";
                        projects.add(project);
                    }
                    String projectTempUrl = inputLine.substring(urlIndex + " href=\"/p/".length());
                    projectUrl = "https://code.google.com/p/" + projectTempUrl.substring(0, projectTempUrl.indexOf("\""));
                }
                if((starIndex = inputLine.indexOf("id=\"star_count-")) != -1) {
                    stars = inputLine.substring(inputLine.indexOf(">") + 1, inputLine.indexOf("</span>"));
                    Project project = new Project();
                    project.url = projectUrl;
                    project.stars = stars;
                    projects.add(project);
                    projectUrl = stars = null;
                }
                if(inputLine.contains(" - did not generate any results.")) {
                    done = true;
                    break;
                }
            }
            in.close();
            start +=10;
            if(projectUrl != null) {
                Project project = new Project();
                project.url = projectUrl;
                project.stars = "0";
                projects.add(project);
            }
        }
        Collections.sort(projects, new Comparator<Project>() {

            @Override
            public int compare(Project project1, Project project2) {
                Integer stars1 = Integer.parseInt(project1.stars);
                Integer stars2 = Integer.parseInt(project2.stars);
                return -stars1.compareTo(stars2);
            }

        });
        System.out.println("Total projects:" +projects.size());
        for (Project project : projects) {
            System.out.println(project.url + ":" + project.stars);
        }
    }
}

class Project {
    String url;
    String stars;
}
like image 150
krishnakumarp Avatar answered Oct 05 '22 03:10

krishnakumarp