From 8af597fdcf19b044a31947134d017350b8557252 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 21 Jan 2022 12:18:00 +0100 Subject: [PATCH] Initial commit --- cmd/elpaisreader/elpaisreader.go | 63 ++++++++++++++++++++++++++++++++ go.mod | 3 ++ 2 files changed, 66 insertions(+) create mode 100644 cmd/elpaisreader/elpaisreader.go create mode 100644 go.mod diff --git a/cmd/elpaisreader/elpaisreader.go b/cmd/elpaisreader/elpaisreader.go new file mode 100644 index 0000000..3ceb26d --- /dev/null +++ b/cmd/elpaisreader/elpaisreader.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" +) + +const beginningToken = "\"articleBody\"" +const endToken = ",\"keywords\":" + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "USAGE: %s URL\n", os.Args[0]) + os.Exit(-1) + } + + url := os.Args[1] + + if url == "-h" || url == "--help" { + fmt.Fprintf(os.Stderr, "USAGE: %s URL\n", os.Args[0]) + os.Exit(2) + } + + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + theLine := "" + for _, line := range strings.Split(string(body), "\n") { + if strings.Contains(line, beginningToken) { + theLine = line + } + } + + if theLine == "" { + panic("articleBody not found") + } + + index := strings.Index(theLine, beginningToken) + endIndex := strings.Index(theLine, endToken) + + if index == -1 || endIndex == -1 { + panic("cannot determine limits") + } + + if index > endIndex { + panic("end found before beginning") + } + + theLine = theLine[index+len(beginningToken)+2 : endIndex-1] + + fmt.Println(theLine) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3fae15e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.ctrlz.es/mgdelacroix/elpaisreader + +go 1.17