fix title voor HLN en Parool; helperfuncties naar internal/util
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
e "codeberg.org/pebbe/errors"
|
||||
"github.com/jbowtie/gokogiri"
|
||||
|
||||
u "git.web.rug.nl/p209327/nlnieuws/internal/util"
|
||||
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"html"
|
||||
@@ -11,7 +13,6 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -64,7 +65,7 @@ func main() {
|
||||
}()
|
||||
|
||||
myLock := "/net/corpora/nlnieuws/Sikkom/lock"
|
||||
mkLock(myLock)
|
||||
u.MkLock(myLock)
|
||||
defer func() {
|
||||
_ = os.Remove(myLock)
|
||||
}()
|
||||
@@ -220,49 +221,12 @@ func doArticle(filename string, url string, title string, timestamp time.Time, n
|
||||
fp, err = os.Create(filename + ".txt")
|
||||
p(err)
|
||||
|
||||
p(fp.WriteString(addEnd(fixSpace(title))))
|
||||
p(fp.WriteString(u.AddEnd(u.FixSpace(title))))
|
||||
|
||||
for _, p1 := range pp {
|
||||
p(fp.WriteString(addEnd(fixSpace(p1.Content()))))
|
||||
p(fp.WriteString(u.AddEnd(u.FixSpace(p1.Content()))))
|
||||
}
|
||||
|
||||
p(fp.Close())
|
||||
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