Last active
May 25, 2021 13:49
-
-
Save HokieGeek/fa9acf46a6bcda0f1faf1c11c2dbb3f1 to your computer and use it in GitHub Desktop.
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
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go list -m all | awk '{ printf("%s@%s\n", $1, $2) }' | xargs go run golics.go