2019-12-22 09:52:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-22 11:57:14 +00:00
|
|
|
"bytes"
|
2019-12-22 10:32:31 +00:00
|
|
|
"fmt"
|
2019-12-22 09:52:43 +00:00
|
|
|
"html/template"
|
2019-12-22 11:57:14 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-12-22 09:52:43 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2019-12-22 11:57:14 +00:00
|
|
|
"regexp"
|
2019-12-22 10:10:11 +00:00
|
|
|
"time"
|
2019-12-22 09:52:43 +00:00
|
|
|
|
2019-12-22 10:10:11 +00:00
|
|
|
"github.com/jinzhu/now"
|
|
|
|
"p83.nl/go/ekster/pkg/microsub"
|
2019-12-22 09:52:43 +00:00
|
|
|
"willnorris.com/go/microformats"
|
|
|
|
|
|
|
|
"p83.nl/go/ekster/pkg/jf2"
|
|
|
|
)
|
|
|
|
|
2019-12-22 11:57:14 +00:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
}
|
2019-12-22 09:52:43 +00:00
|
|
|
func main() {
|
|
|
|
u, err := url.Parse(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-12-22 11:57:14 +00:00
|
|
|
filename := os.Args[2]
|
|
|
|
|
|
|
|
re, err := regexp.Compile(`<span class="next-entry"></span>`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := openAndCreate(filename)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-12-22 09:52:43 +00:00
|
|
|
|
2019-12-22 11:57:14 +00:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2019-12-22 10:32:31 +00:00
|
|
|
items, err := getEntriesForFeed(u)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-12-22 09:52:43 +00:00
|
|
|
|
2019-12-22 10:10:11 +00:00
|
|
|
items = filterEntriesAfter(items, now.With(time.Now()).Monday())
|
|
|
|
|
2019-12-22 11:57:14 +00:00
|
|
|
err = outputEntries(items, &buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := re.ReplaceAll(data, buf.Bytes())
|
|
|
|
err = ioutil.WriteFile(filename, output, 0666)
|
2019-12-22 10:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-12-22 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 11:57:14 +00:00
|
|
|
func openAndCreate(filename string) (io.ReadCloser, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
t, err := template.ParseFiles("templates/full.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err = os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = t.Execute(f, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err = os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputEntries(items []microsub.Item, w io.Writer) error {
|
2019-12-22 09:52:43 +00:00
|
|
|
t, err := template.ParseFiles("templates/weekly.html")
|
|
|
|
if err != nil {
|
2019-12-22 11:57:14 +00:00
|
|
|
log.Fatal(err)
|
2019-12-22 09:52:43 +00:00
|
|
|
}
|
2019-12-22 11:57:14 +00:00
|
|
|
err = t.Execute(w, items)
|
2019-12-22 09:52:43 +00:00
|
|
|
if err != nil {
|
2019-12-22 10:34:08 +00:00
|
|
|
return err
|
2019-12-22 09:52:43 +00:00
|
|
|
}
|
2019-12-22 10:34:08 +00:00
|
|
|
return nil
|
2019-12-22 09:52:43 +00:00
|
|
|
}
|
2019-12-22 10:10:11 +00:00
|
|
|
|
2019-12-22 10:32:31 +00:00
|
|
|
func getEntriesForFeed(u *url.URL) ([]microsub.Item, error) {
|
2019-12-22 10:25:08 +00:00
|
|
|
resp, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
2019-12-22 10:32:31 +00:00
|
|
|
return nil, err
|
2019-12-22 10:25:08 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
2019-12-22 10:32:31 +00:00
|
|
|
return nil, fmt.Errorf("status is not 200, but %d", resp.StatusCode)
|
2019-12-22 10:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
md := microformats.Parse(resp.Body, u)
|
|
|
|
items := jf2.SimplifyMicroformatDataItems(md)
|
2019-12-22 10:32:31 +00:00
|
|
|
return items, nil
|
2019-12-22 10:25:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 10:10:11 +00:00
|
|
|
func filterEntriesAfter(items []microsub.Item, from time.Time) []microsub.Item {
|
|
|
|
var result []microsub.Item
|
|
|
|
for _, item := range items {
|
|
|
|
published, err := time.Parse(time.RFC3339, item.Published)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if published.After(from) {
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|