Skip to content
Permalink
Browse files

play with fetching a GitHub login from a website URL

  • Loading branch information...
dmitshur committed Oct 7, 2019
1 parent 31474f0 commit f9554aad8c4344b63d9647f08761480e28f37c72
Showing with 57 additions and 0 deletions.
  1. +57 −0 274/main.go
@@ -0,0 +1,57 @@
// Play with using go/microformats to fetch
// a GitHub login from a website URL.
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"

"willnorris.com/go/microformats"
)

func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalln("usage: argument 1 must be website URL")
}
websiteURL := flag.Arg(0)

login, err := fetchGitHubLogin(websiteURL)
if err != nil {
log.Fatalln(err)
}
fmt.Println(login)
}

func fetchGitHubLogin(websiteURL string) (string, error) {
u, err := url.Parse(websiteURL)
if err != nil {
return "", err
}
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return "", fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
}
data := microformats.Parse(resp.Body, u)
for _, me := range data.Rels["me"] {
if !strings.HasPrefix(me, "https://github.com/") {
continue
}
login := me[len("https://github.com/"):]
if i := strings.Index(login, "/"); i >= 0 {
login = login[:i]
}
return login, nil
}
return "", os.ErrNotExist
}

0 comments on commit f9554aa

Please sign in to comment.
You can’t perform that action at this time.