263 lines
5.4 KiB
Go
263 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
e "codeberg.org/pebbe/errors"
|
|
|
|
u "git.web.rug.nl/p209327/nlnieuws/internal/util"
|
|
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"html"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Rss struct {
|
|
XMLName xml.Name `xml:"rss"`
|
|
Items []ItemT `xml:"channel>item"`
|
|
}
|
|
|
|
type ItemT struct {
|
|
PubDate string `xml:"pubDate"`
|
|
UnixTime int64 `xml:"unixTime"`
|
|
Guid string `xml:"guid"`
|
|
Link string `xml:"link"`
|
|
Data []byte `xml:",innerxml"`
|
|
}
|
|
|
|
type Doc struct {
|
|
Type string `json:"@type"`
|
|
Cat string `json:"articleSection"`
|
|
Tags []string `json:"keywords"`
|
|
Title string `json:"headline"`
|
|
Text string `json:"articleBody"`
|
|
}
|
|
|
|
type GItem struct {
|
|
ArticleBody string `json:"articleBody"`
|
|
ArticleSection []string `json:"articleSection"`
|
|
}
|
|
|
|
var (
|
|
p = e.PanicErr
|
|
w = e.WarnErr
|
|
agent = "AhrefsBot/7.0"
|
|
)
|
|
|
|
func exists(filename string) bool {
|
|
_, err := os.Stat(filename)
|
|
return err == nil
|
|
}
|
|
|
|
func fileDate(filename string) string {
|
|
b, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
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)
|
|
}
|
|
}()
|
|
|
|
myLock := "/net/corpora/nlnieuws/RTVNoord/lock"
|
|
u.MkLock(myLock)
|
|
defer func() {
|
|
_ = os.Remove(myLock)
|
|
}()
|
|
|
|
resp, err := http.Get("https://www.rtvnoord.nl/rss/index.xml")
|
|
p(err)
|
|
body, err := io.ReadAll(resp.Body)
|
|
p(err)
|
|
p(resp.Body.Close())
|
|
|
|
var rss Rss
|
|
p(xml.Unmarshal(body, &rss))
|
|
|
|
if len(rss.Items) == 0 {
|
|
p(fmt.Errorf("len(rss.Items) == 0"))
|
|
}
|
|
|
|
for _, item := range rss.Items {
|
|
t, err := time.Parse(time.RFC1123Z, item.PubDate)
|
|
if err != nil {
|
|
t, err = time.Parse(time.RFC1123, item.PubDate)
|
|
}
|
|
p(err)
|
|
year, week := t.ISOWeek()
|
|
dirname := fmt.Sprintf("/net/corpora/nlnieuws/RTVNoord/%d/w%02d", year, week)
|
|
if exists(dirname + "/lock") {
|
|
continue
|
|
}
|
|
guid := strings.TrimPrefix(item.Guid, "https://www.rtvnoord.nl/")
|
|
guid = strings.TrimRight(guid, "/-")
|
|
guid = strings.ReplaceAll(guid, "/", "_")
|
|
filename := dirname + "/" + guid
|
|
|
|
ts := fmt.Sprintf("%d", t.Unix())
|
|
needUpdate := fileDate(filename+".xml") != ts
|
|
|
|
p(os.MkdirAll(dirname, 0777))
|
|
func() {
|
|
var ok bool
|
|
defer func() {
|
|
if e.Panicked {
|
|
fmt.Fprintln(os.Stderr, "----", filename)
|
|
fmt.Fprintln(os.Stderr, "----", item.Link)
|
|
}
|
|
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)
|
|
}()
|
|
}
|
|
}
|
|
|
|
func doArticle(filename string, url string, timestamp time.Time, needUpdate bool) bool {
|
|
if exists(filename + ".skip") {
|
|
return true
|
|
}
|
|
if needUpdate {
|
|
_ = os.Remove(filename + ".err")
|
|
_ = os.Remove(filename + ".html")
|
|
_ = os.Remove(filename + ".json")
|
|
_ = os.Remove(filename + ".txt")
|
|
} else {
|
|
// voor sommige berichten is geen .txt, alleen .json
|
|
if exists(filename + ".json") {
|
|
return true
|
|
}
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
p(err)
|
|
req.Header.Set("User-Agent", agent)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
p(err)
|
|
body, err := io.ReadAll(resp.Body)
|
|
p(err)
|
|
p(resp.Body.Close())
|
|
|
|
s := string(body)
|
|
ok := true
|
|
var doc Doc
|
|
skip := 0
|
|
for {
|
|
var i2 int
|
|
var s2 string
|
|
i1 := strings.Index(s[skip:], `type="application/ld+json"`) + skip
|
|
if i1 < 0 {
|
|
ok = false
|
|
break
|
|
} else {
|
|
i1 += strings.Index(s[i1:], `>`) + 1
|
|
i2 = i1 + strings.Index(s[i1:], `</script>`)
|
|
if i2 < i1 {
|
|
ok = false
|
|
break
|
|
} else {
|
|
s2 = html.UnescapeString(s[i1:i2])
|
|
}
|
|
}
|
|
if err = json.Unmarshal([]byte(s2), &doc); err != nil {
|
|
_ = w(err, url)
|
|
skip = i2
|
|
continue
|
|
}
|
|
if doc.Type == "Organization" {
|
|
skip = i2
|
|
continue
|
|
}
|
|
s = s2
|
|
break
|
|
}
|
|
if !ok {
|
|
_ = w(fmt.Errorf("script jsonld not found: %s", url))
|
|
|
|
fp, err := os.Create(filename + ".err")
|
|
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")
|
|
p(err)
|
|
p(fp.Write(body))
|
|
p(fp.Close())
|
|
p(os.Chtimes(filename+".html", timestamp, timestamp))
|
|
|
|
return false
|
|
}
|
|
fp, err := os.Create(filename + ".json")
|
|
p(err)
|
|
p(fp.WriteString(s))
|
|
p(fp.Close())
|
|
p(os.Chtimes(filename+".json", timestamp, timestamp))
|
|
|
|
// text bevat kopjes zonder punt aan het eind
|
|
lines := strings.Split(doc.Text, "\n")
|
|
for i, line := range lines {
|
|
lines[i] = u.AddEnd(u.FixSpace(line))
|
|
}
|
|
text := strings.Join(lines, "") + "\n"
|
|
|
|
fp, err = os.Create(filename + ".txt")
|
|
p(err)
|
|
if len(doc.Tags) == 0 {
|
|
p(fmt.Fprintln(fp, "##META text tag ="))
|
|
} else {
|
|
for _, tag := range doc.Tags {
|
|
t := strings.ToLower(u.FixSpace(tag))
|
|
if strings.HasPrefix(t, "br_") {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(t, "tr_") {
|
|
t = t[3:]
|
|
}
|
|
p(fmt.Fprintf(fp, "##META text tag = %s\n", t))
|
|
}
|
|
}
|
|
if doc.Cat == "" {
|
|
p(fmt.Fprintln(fp, "##META text cat ="))
|
|
} else {
|
|
p(fmt.Fprintf(fp, "##META text cat = %s\n", u.FixSpace(doc.Cat)))
|
|
}
|
|
|
|
p(fp.WriteString(u.AddEnd(doc.Title)))
|
|
|
|
p(fp.WriteString(text))
|
|
p(fp.Close())
|
|
p(os.Chtimes(filename+".txt", timestamp, timestamp))
|
|
|
|
return true
|
|
}
|