344 lines
7.0 KiB
Go
344 lines
7.0 KiB
Go
package main
|
|
|
|
import (
|
|
e "codeberg.org/pebbe/errors"
|
|
"github.com/jbowtie/gokogiri"
|
|
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"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"`
|
|
}
|
|
|
|
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/Parool/lock"
|
|
mkLock(myLock)
|
|
defer func() {
|
|
_ = os.Remove(myLock)
|
|
}()
|
|
|
|
req, err := http.NewRequest("GET", "https://www.parool.nl/amsterdam/rss.xml", 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())
|
|
|
|
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/Parool/%d/%02d", year, week)
|
|
if exists(dirname + "/lock") {
|
|
continue
|
|
}
|
|
basename := item.Guid
|
|
filename := dirname + "/" + url.PathEscape(basename)
|
|
|
|
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) (ok bool) {
|
|
if exists(filename + ".skip") {
|
|
return true
|
|
}
|
|
if needUpdate {
|
|
_ = os.Remove(filename + ".err")
|
|
_ = os.Remove(filename + ".html")
|
|
_ = os.Remove(filename + ".txt")
|
|
} else {
|
|
if exists(filename + ".txt") {
|
|
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())
|
|
|
|
doc, err := gokogiri.ParseHtml(body)
|
|
p(err)
|
|
|
|
root := doc.Root()
|
|
|
|
articles, err := root.Search(`//article[@id="article-content"]`)
|
|
p(err)
|
|
if len(articles) == 0 {
|
|
_ = w(fmt.Errorf("empty: %s", url))
|
|
|
|
fp, err := os.Create(filename + ".err")
|
|
p(err)
|
|
p(fmt.Fprintf(fp, "empty: %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
|
|
}
|
|
article := articles[0]
|
|
|
|
live, err := article.Search(`//*[@data-test-id="live-blog-label"]`)
|
|
p(err)
|
|
if len(live) > 0 {
|
|
fp, err := os.Create(filename + ".skip")
|
|
p(fp.WriteString("liveblog\n"))
|
|
p(err)
|
|
p(os.Chtimes(filename+".skip", timestamp, timestamp))
|
|
return true
|
|
}
|
|
|
|
tags := make([]string, 0)
|
|
ell, err := article.Search(`//header//*[@data-test-id="article-label"]`)
|
|
p(err)
|
|
if len(ell) == 0 {
|
|
_ = w(fmt.Errorf("no labels: %s", url))
|
|
}
|
|
for _, el := range ell {
|
|
s := strings.TrimSpace(el.Content())
|
|
if s != "" {
|
|
tags = append(tags, s)
|
|
}
|
|
}
|
|
|
|
pars := make([]string, 0)
|
|
|
|
ell, err = article.Search(`//header//*[@data-test-id="article-title"]`)
|
|
p(err)
|
|
if len(ell) != 1 {
|
|
_ = w(fmt.Errorf("found %d titles: %s", len(ell), url))
|
|
}
|
|
for _, el := range ell {
|
|
s := strings.TrimSpace(el.Content())
|
|
if s != "" {
|
|
pars = append(pars, s)
|
|
}
|
|
}
|
|
|
|
found := false
|
|
ell, err = article.Search(`//header//*[@data-test-id="header-intro"]`)
|
|
p(err)
|
|
for _, el := range ell {
|
|
s := strings.TrimSpace(el.Content())
|
|
if s != "" {
|
|
pars = append(pars, s)
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
_ = w(fmt.Errorf("no intro: %s", url))
|
|
}
|
|
|
|
specials, err := article.Search(`//section//aside | //section//figure | //section//b`)
|
|
p(err)
|
|
for _, special := range specials {
|
|
special.Remove()
|
|
}
|
|
|
|
ell, err = article.Search(`//section//*[@data-article-element-index]`)
|
|
p(err)
|
|
if len(ell) == 0 {
|
|
_ = w(fmt.Errorf("no elements: %s", url))
|
|
|
|
fp, err := os.Create(filename + ".err")
|
|
p(err)
|
|
p(fmt.Fprintf(fp, "no elements: %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
|
|
}
|
|
|
|
found = false
|
|
for _, el := range ell {
|
|
s := strings.TrimSpace(el.Content())
|
|
if s != "" {
|
|
pars = append(pars, s)
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
_ = w(fmt.Errorf("no text, skipping: %s", url))
|
|
fp, err := os.Create(filename + ".skip")
|
|
p(fp.WriteString(url + "\n"))
|
|
p(err)
|
|
p(os.Chtimes(filename+".skip", 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 true
|
|
}
|
|
|
|
fp, err := os.Create(filename + ".txt")
|
|
p(err)
|
|
|
|
if len(tags) == 0 {
|
|
p(fmt.Fprintln(fp, "##META text tag ="))
|
|
} else {
|
|
for _, tag := range tags {
|
|
p(fmt.Fprintf(fp, "##META text tag = %s\n", fixSpace(tag)))
|
|
}
|
|
}
|
|
|
|
for _, par := range pars {
|
|
p(fp.WriteString(addEnd(fixSpace(par))))
|
|
}
|
|
|
|
p(fp.Close())
|
|
|
|
p(os.Chtimes(filename+".txt", timestamp, timestamp))
|
|
|
|
return true
|
|
}
|
|
|
|
func addEnd(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
n := len(s)
|
|
if n == 0 {
|
|
return ""
|
|
}
|
|
if n > 0 {
|
|
if strings.ContainsAny(s[n-1:], ".!?") {
|
|
return s + "\n"
|
|
}
|
|
}
|
|
if n > 1 {
|
|
s2 := s[n-2:]
|
|
if s2 == `."` || s2 == `!"` || s2 == `?"` || s2 == `.'` || s2 == `!'` || s2 == `?'` {
|
|
return s + "\n"
|
|
}
|
|
}
|
|
if strings.HasSuffix(s, `.”`) || strings.HasSuffix(s, `!”`) || strings.HasSuffix(s, `?”`) {
|
|
return s + "\n"
|
|
}
|
|
return s + ".\n"
|
|
}
|
|
|
|
func fixSpace(s string) string {
|
|
return strings.Join(strings.Fields(s), " ")
|
|
}
|
|
|
|
func mkLock(filename string) {
|
|
pid := os.Getpid()
|
|
link := fmt.Sprintf("%s.%d", filepath.Base(filename), pid)
|
|
p(os.Symlink(link, filename))
|
|
|
|
name, err := os.Readlink(filename)
|
|
p(err)
|
|
|
|
if name != link {
|
|
p(fmt.Errorf("wrong lock name %q, should be %q", name, link))
|
|
}
|
|
}
|