indebuurt:amsterdam
This commit is contained in:
250
BuurtAdam/cmd/buurtadam/buurtadam.go
Normal file
250
BuurtAdam/cmd/buurtadam/buurtadam.go
Normal file
@@ -0,0 +1,250 @@
|
||||
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 {
|
||||
Title string `xml:"title"`
|
||||
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"
|
||||
// agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36"
|
||||
)
|
||||
|
||||
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/BuurtAdam/lock"
|
||||
mkLock(myLock)
|
||||
defer func() {
|
||||
_ = os.Remove(myLock)
|
||||
}()
|
||||
|
||||
req, err := http.NewRequest("GET", "https://indebuurt.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/BuurtAdam/%d/%02d", year, week)
|
||||
if exists(dirname + "/lock") {
|
||||
continue
|
||||
}
|
||||
guid := strings.TrimPrefix(item.Guid, "https://indebuurt.nl/")
|
||||
guid = strings.TrimPrefix(guid, "amsterdam/")
|
||||
guid = strings.TrimSuffix(guid, "/")
|
||||
guid = strings.ReplaceAll(guid, "/", "_")
|
||||
filename := dirname + "/" + url.PathEscape(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, item.Title, t, needUpdate)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func doArticle(filename string, url string, title 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()
|
||||
|
||||
divs, err := root.Search(`//div[@data-content-type="LABEL"]`)
|
||||
p(err)
|
||||
labels := make([]string, 0)
|
||||
for _, div := range divs {
|
||||
labels = append(labels, strings.Join(strings.Fields(div.Content()), " "))
|
||||
}
|
||||
|
||||
divs, err = root.Search(`//div[@data-content-type="TITLE" or @data-content-type="INTRO" or @data-content-type="PARAGRAPH" or @data-content-type="SUBHEADER"]`)
|
||||
p(err)
|
||||
if len(divs) == 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
|
||||
}
|
||||
|
||||
fp, err := os.Create(filename + ".txt")
|
||||
p(err)
|
||||
|
||||
if len(labels) == 0 {
|
||||
p(fp.WriteString("##META text tag =\n"))
|
||||
} else {
|
||||
for _, label := range labels {
|
||||
p(fp.WriteString("##META text tag = " + label + "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
for _, div := range divs {
|
||||
p(fp.WriteString(addEnd(fixSpace(div.Content()))))
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user