fix title voor HLN en Parool; helperfuncties naar internal/util

This commit is contained in:
Peter Kleiweg
2026-05-24 16:16:21 +02:00
parent 4b56c0cd70
commit 75832c3132
36 changed files with 301 additions and 648 deletions

View File

@@ -5,5 +5,5 @@ all: \
metadata: cmd/metadata/*.go
go build -o $@ $^
rtvnoord: cmd/rtvnoord/*.go
go build -o $@ $^
rtvnoord: cmd/rtvnoord/*.go ../internal/util/*.go
go build -o $@ $<

View File

@@ -3,6 +3,8 @@ package main
import (
e "codeberg.org/pebbe/errors"
u "git.web.rug.nl/p209327/nlnieuws/internal/util"
"encoding/json"
"encoding/xml"
"fmt"
@@ -10,7 +12,6 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
@@ -75,7 +76,7 @@ func main() {
}()
myLock := "/net/corpora/nlnieuws/RTVNoord/lock"
mkLock(myLock)
u.MkLock(myLock)
defer func() {
_ = os.Remove(myLock)
}()
@@ -225,7 +226,7 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
// text bevat kopjes zonder punt aan het eind
lines := strings.Split(doc.Text, "\n")
for i, line := range lines {
lines[i] = addEnd(fixSpace(line))
lines[i] = u.AddEnd(u.FixSpace(line))
}
text := strings.Join(lines, "") + "\n"
@@ -235,16 +236,16 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
p(fmt.Fprintln(fp, "##META text tag ="))
} else {
for _, tag := range doc.Tags {
p(fmt.Fprintf(fp, "##META text tag = %s\n", strings.ToLower(fixSpace(tag))))
p(fmt.Fprintf(fp, "##META text tag = %s\n", strings.ToLower(u.FixSpace(tag))))
}
}
if doc.Cat == "" {
p(fmt.Fprintln(fp, "##META text cat ="))
} else {
p(fmt.Fprintf(fp, "##META text cat = %s\n", fixSpace(doc.Cat)))
p(fmt.Fprintf(fp, "##META text cat = %s\n", u.FixSpace(doc.Cat)))
}
p(fp.WriteString(addEnd(doc.Title)))
p(fp.WriteString(u.AddEnd(doc.Title)))
p(fp.WriteString(text))
p(fp.Close())
@@ -252,40 +253,3 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
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))
}
}