This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding script to find popular, but unused projects
Issue: #14
- Loading branch information
Showing
180 changed files
with
61,045 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2015, Tim Heckman | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of linode-netint nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2018 The Gofrs. All rights reserved. | ||
// Use of this source code is governed by the BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
// finder is a command line tool (CLI) used to find stale projects | ||
// on Github (those without recent commits, issues, etc) and rank | ||
// them by their godoc import counts. | ||
// | ||
// Godoc.org import counts are public and computed by godoc.org as | ||
// it indexes the public Go repositories. | ||
package main // import "github.com/gofrs/help-requests/cmd/finder | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"math" | ||
"net/http" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/google/go-github/github" | ||
"golang.org/x/net/html" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var ( | ||
flagCount = flag.Int("count", 25, "How many (Github) projects to lookup") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
ghClient, err := createGithubClient(context.Background()) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "ERROR: problem creating github client: %v", err) | ||
} | ||
|
||
opts := github.ListOptions{ | ||
PerPage: *flagCount, | ||
Page: 1, | ||
} | ||
|
||
query := "stars:>100 pushed:<2018-01-01 language:Go" | ||
repoRes, res, err := ghClient.Search.Repositories(context.Background(), query, &github.SearchOptions{ | ||
Sort: "stars", | ||
Order: "desc", | ||
ListOptions: opts, | ||
}) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "ERROR: problem reading github repositories: %v", err) | ||
} | ||
res.Close = true | ||
|
||
type row struct { | ||
text string | ||
importCount int | ||
} | ||
var rows []row | ||
for i := range repoRes.Repositories { | ||
repo := repoRes.Repositories[i] | ||
|
||
cleanName := strings.Replace(*repo.HTMLURL, `https://`, "", 1) | ||
|
||
// TODO(adam): goroutines + sync.WaitGroup | ||
importers, err := scrapeGodocImports(cleanName) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "ERROR: problem grabbing %s godoc importers: %v\n", cleanName, err) | ||
} | ||
|
||
days := int(math.Abs(float64(repo.PushedAt.Sub(time.Now()).Hours()) / 24.0)) | ||
line := fmt.Sprintf("%s\t%d\t%d\t%d\n", cleanName, *repo.StargazersCount, days, importers) | ||
rows = append(rows, row{ | ||
text: line, | ||
importCount: importers, | ||
}) | ||
} | ||
sort.Slice(rows, func(i, j int) bool { return rows[i].importCount < rows[j].importCount }) | ||
|
||
// Write (sorted) output | ||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) | ||
fmt.Fprintf(w, "name\tstars\tlast commit (days)\timporters\n") | ||
defer w.Flush() | ||
for i := range rows { | ||
// we're going to write the rows in reverse | ||
// this will output them in desc order | ||
fmt.Fprintf(w, rows[len(rows)-i-1].text) | ||
} | ||
} | ||
|
||
func createGithubClient(ctx context.Context) (*github.Client, error) { | ||
v := os.Getenv("GITHUB_TOKEN") | ||
if v == "" { | ||
return nil, errors.New("environment variable GITHUB_TOKEN is required") | ||
} | ||
ts := oauth2.StaticTokenSource(&oauth2.Token{ | ||
AccessToken: v, | ||
}) | ||
tc := oauth2.NewClient(ctx, ts) | ||
return github.NewClient(tc), nil | ||
} | ||
|
||
func scrapeGodocImports(importPath string) (int, error) { | ||
req, err := http.NewRequest("GET", "https://godoc.org/"+importPath, nil) | ||
if err != nil { | ||
return -1, fmt.Errorf("problem loading godoc.org: %v", err) | ||
} | ||
req.Header.Set("User-Agent", "Gofrs popstalerepo bot") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return -1, fmt.Errorf("problem loading %s: %v", req.URL, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// recursive search, from /x/net/html docs | ||
var f func(n *html.Node) (int, error) | ||
f = func(n *html.Node) (int, error) { | ||
if n.Type == html.ElementNode && n.Data == "a" { | ||
for _, a := range n.Attr { | ||
// TODO(adam): we should try and refresh importers | ||
// when running into errors. | ||
if a.Key == "href" && strings.Contains(a.Val, "?importers") { | ||
parts := strings.Fields(n.FirstChild.Data) | ||
n, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return -1, fmt.Errorf("couldn't parse %q: %v", parts[0], err) | ||
} | ||
return n, nil | ||
} | ||
} | ||
} | ||
for c := n.FirstChild; c != nil; c = c.NextSibling { | ||
n, err := f(c) | ||
if err == nil && n > 0 { | ||
return n, err | ||
} | ||
} | ||
return -1, errors.New(`didn't find <a href="?importers">`) | ||
} | ||
|
||
doc, err := html.Parse(resp.Body) | ||
if err != nil { | ||
return -1, fmt.Errorf("couldn't parse html: %v", err) | ||
} | ||
return f(doc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/gofrs/help-requests | ||
|
||
require ( | ||
github.com/golang/protobuf v1.2.0 // indirect | ||
github.com/google/go-github v17.0.0+incompatible | ||
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect | ||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 | ||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be | ||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect | ||
google.golang.org/appengine v1.1.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= | ||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= | ||
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= | ||
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0= | ||
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI= | ||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs= | ||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= | ||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= | ||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs= | ||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.