Skip to content

Instantly share code, notes, and snippets.

@HokieGeek
Last active May 25, 2021 13:49
Show Gist options
  • Save HokieGeek/fa9acf46a6bcda0f1faf1c11c2dbb3f1 to your computer and use it in GitHub Desktop.
Save HokieGeek/fa9acf46a6bcda0f1faf1c11c2dbb3f1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"strings"
"git.sr.ht/~hokiegeek/htmlscrape"
)
func main() {
for i := 1; i < len(os.Args); i++ {
pkg := os.Args[i]
url := fmt.Sprintf("https://pkg.go.dev/%s?tab=licenses", pkg)
doc, err := htmlscrape.New(url)
if err != nil {
fmt.Printf("could not scrape package: %s: %v\n", pkg, err)
continue
}
div := htmlscrape.FindNode(doc, htmlscrape.NewNodeMatcher().Attr("id", "#lic-0"))
if div == nil {
div = htmlscrape.FindNode(doc, htmlscrape.NewNodeMatcher().Attr("id", "#LICENSE.txt"))
if div == nil {
div = htmlscrape.FindNode(doc, htmlscrape.NewNodeMatcher().Attr("id", "#LICENSE.md"))
}
}
var license string
txt := htmlscrape.FindNode(div, htmlscrape.NewNodeMatcher().Text())
switch {
case txt == nil:
license = "not found"
default:
license = strings.ReplaceAll(txt.Data, ",", ";")
}
fmt.Printf("%s,%s,%s\n", pkg, license, url)
}
}
@HokieGeek
Copy link
Author

HokieGeek commented Jul 1, 2020

go list -m all | awk '{ printf("%s@%s\n", $1, $2) }' | xargs go run golics.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment