Initial commit

This commit is contained in:
Miguel de la Cruz 2022-01-21 12:18:00 +01:00
commit 8af597fdcf
2 changed files with 66 additions and 0 deletions

View File

@ -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)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.ctrlz.es/mgdelacroix/elpaisreader
go 1.17