helperfuncties naar internal/util (2)

This commit is contained in:
Peter Kleiweg
2026-05-24 16:29:56 +02:00
parent 75832c3132
commit fcad105a75
14 changed files with 37 additions and 125 deletions

View File

@@ -3,6 +3,8 @@ package main
import (
e "codeberg.org/pebbe/errors"
u "git.web.rug.nl/p209327/nlnieuws/internal/util"
"bytes"
"encoding/json"
"encoding/xml"
@@ -12,7 +14,6 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
@@ -73,7 +74,7 @@ func main() {
}()
myLock := "/net/corpora/nlnieuws/NU/lock"
mkLock(myLock)
u.MkLock(myLock)
defer func() {
_ = os.Remove(myLock)
}()
@@ -226,7 +227,7 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
// text bevat kopjes zonder punt aan het eind
lines := strings.Split(text, "\n")
for i, line := range lines {
line = fixSpace(line)
line = u.FixSpace(line)
n := len(line)
if n > 0 {
if strings.ContainsAny(line[n-1:], ".!?") {
@@ -249,7 +250,7 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
p(fmt.Fprintln(fp, "##META text tag ="))
} else {
for _, tag := range tags {
p(fmt.Fprintf(fp, "##META text tag = %s\n", fixSpace(tag)))
p(fmt.Fprintf(fp, "##META text tag = %s\n", u.FixSpace(tag)))
}
}
p(fp.WriteString(text))
@@ -259,20 +260,3 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
return true
}
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))
}
}