all repos — dotfiles @ 816b900c50a953bed3bce77c89b941ac18c6ff79

linux dotfiles

bin(go): add fetchtitle.go
Prithu Goswami pg@prithu.dev
Tue, 12 Mar 2024 08:22:57 +0530
commit

816b900c50a953bed3bce77c89b941ac18c6ff79

parent

891917c42b235ef0e572cfaf38cad6f8db006f1d

1 files changed, 34 insertions(+), 0 deletions(-)

jump to
A bin/go/fetchtitle.go

@@ -0,0 +1,34 @@

+package main + +import ( + "bytes" + "fmt" + "io" + "net/http" + "os" + "regexp" +) + +func main() { + re := regexp.MustCompile(`<title>(.*?)</title>`) + command := os.Args[0] + urls := os.Args[1:] + + for _, url := range urls { + resp, err := http.Get(url) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %v\n", command, err) + os.Exit(1) + } + defer resp.Body.Close() + b := bytes.NewBuffer([]byte{}) + _, err = io.Copy(b, resp.Body) + matches := re.FindSubmatch(b.Bytes()) + fmt.Printf("%s", matches[1]) + + if err != nil { + fmt.Fprintf(os.Stderr, "%s: reading %s: %v\n", command, url, err) + os.Exit(1) + } + } +}