如何按星级排序/过滤Google代码项目?

时间:2013-05-30 07:31:23

标签: google-code

我想知道Google代码上托管的哪些项目拥有最多的星标,特别是在按标签搜索时,如下所示:

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

2 个答案:

答案 0 :(得分:7)

项目搜索页面不支持按星标排序。能够编写一些页面报废代码来获取所需信息。

希望它有所帮助。

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;
}

答案 1 :(得分:0)

我会说使用&sort=stars就像支持谷歌代码一样,但它并不能很好地运作。我不确定不幸的是......