codeberg.org/pebbe/errors
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/pebbe/util"
|
||||
e "codeberg.org/pebbe/errors"
|
||||
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
@@ -27,8 +27,8 @@ type ItemT struct {
|
||||
}
|
||||
|
||||
var (
|
||||
x = util.CheckErr
|
||||
w = util.WarnErr
|
||||
p = e.PanicErr
|
||||
w = e.WarnErr
|
||||
agent = "AhrefsBot/7.0"
|
||||
)
|
||||
|
||||
@@ -45,21 +45,31 @@ func fileDate(filename string) string {
|
||||
s := string(b)
|
||||
i1 := strings.Index(s, "<unixTime>") + 10
|
||||
i2 := strings.Index(s, "</unixTime>")
|
||||
if i2 < i1 {
|
||||
return ""
|
||||
}
|
||||
return s[i1:i2]
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
if e.Panicked() {
|
||||
_ = recover()
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
resp, err := http.Get("https://feeds.nos.nl/nosnieuwsalgemeen")
|
||||
x(err)
|
||||
p(err)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
x(err)
|
||||
x(resp.Body.Close())
|
||||
p(err)
|
||||
p(resp.Body.Close())
|
||||
|
||||
var rss Rss
|
||||
x(xml.Unmarshal(body, &rss))
|
||||
p(xml.Unmarshal(body, &rss))
|
||||
|
||||
if len(rss.Items) == 0 {
|
||||
x(fmt.Errorf("len(rss.Items) == 0"))
|
||||
p(fmt.Errorf("len(rss.Items) == 0"))
|
||||
}
|
||||
|
||||
for _, item := range rss.Items {
|
||||
@@ -74,7 +84,7 @@ func main() {
|
||||
break
|
||||
}
|
||||
}
|
||||
x(err)
|
||||
p(err)
|
||||
year, week := t.ISOWeek()
|
||||
dirname := fmt.Sprintf("/net/corpora/nlnieuws/NOS/%d/%02d", year, week)
|
||||
if exists(dirname + "/lock") {
|
||||
@@ -85,22 +95,24 @@ func main() {
|
||||
ts := fmt.Sprintf("%d", t.Unix())
|
||||
needUpdate := fileDate(filename+".xml") != ts
|
||||
|
||||
x(os.MkdirAll(dirname, 0777))
|
||||
fp, err := os.Create(filename + ".xml")
|
||||
x(err)
|
||||
_, err = fp.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<item>\n")
|
||||
x(err)
|
||||
_, err = fmt.Fprintf(fp, "<unixTime>%d</unixTime>", t.Unix())
|
||||
x(err)
|
||||
_, err = fp.Write(item.Data)
|
||||
x(err)
|
||||
_, err = fp.WriteString("</item>\n")
|
||||
x(err)
|
||||
x(fp.Close())
|
||||
x(os.Chtimes(filename+".xml", t, t))
|
||||
if !doArticle(filename, item.Link, t, needUpdate) {
|
||||
x(os.Remove(filename + ".xml"))
|
||||
}
|
||||
p(os.MkdirAll(dirname, 0777))
|
||||
func() {
|
||||
var ok bool
|
||||
defer func() {
|
||||
if !ok {
|
||||
_ = os.Remove(filename + ".xml")
|
||||
}
|
||||
}()
|
||||
fp, err := os.Create(filename + ".xml")
|
||||
p(err)
|
||||
p(fp.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<item>\n"))
|
||||
p(fmt.Fprintf(fp, "<unixTime>%d</unixTime>", t.Unix()))
|
||||
p(fp.Write(item.Data))
|
||||
p(fp.WriteString("</item>\n"))
|
||||
p(fp.Close())
|
||||
p(os.Chtimes(filename+".xml", t, t))
|
||||
ok = doArticle(filename, item.Link, t, needUpdate)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,15 +130,15 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
x(err)
|
||||
p(err)
|
||||
req.Header.Set("User-Agent", agent)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
x(err)
|
||||
p(err)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
x(err)
|
||||
x(resp.Body.Close())
|
||||
p(err)
|
||||
p(resp.Body.Close())
|
||||
|
||||
s := string(body)
|
||||
|
||||
@@ -147,27 +159,24 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
|
||||
_ = w(fmt.Errorf("script jsonld not found: %s", url))
|
||||
|
||||
fp, err := os.Create(filename + ".err")
|
||||
x(err)
|
||||
_, err = fmt.Fprintf(fp, "script jsonld not found: %s\n", url)
|
||||
x(err)
|
||||
x(fp.Close())
|
||||
x(os.Chtimes(filename+".err", timestamp, timestamp))
|
||||
p(err)
|
||||
p(fmt.Fprintf(fp, "script jsonld not found: %s\n", url))
|
||||
p(fp.Close())
|
||||
p(os.Chtimes(filename+".err", timestamp, timestamp))
|
||||
|
||||
fp, err = os.Create(filename + ".html")
|
||||
x(err)
|
||||
_, err = fp.Write(body)
|
||||
x(err)
|
||||
x(fp.Close())
|
||||
x(os.Chtimes(filename+".html", timestamp, timestamp))
|
||||
p(err)
|
||||
p(fp.Write(body))
|
||||
p(fp.Close())
|
||||
p(os.Chtimes(filename+".html", timestamp, timestamp))
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
fp, err := os.Create(filename + ".json")
|
||||
x(err)
|
||||
_, err = fp.WriteString(s)
|
||||
x(err)
|
||||
x(fp.Close())
|
||||
x(os.Chtimes(filename+".json", timestamp, timestamp))
|
||||
p(err)
|
||||
p(fp.WriteString(s))
|
||||
p(fp.Close())
|
||||
p(os.Chtimes(filename+".json", timestamp, timestamp))
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user