86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
|
||
|
"christine.website/jsonfeed"
|
||
|
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
|
||
|
"within.website/ln"
|
||
|
)
|
||
|
|
||
|
func (mi *Mi) RefreshBlog(w http.ResponseWriter, re *http.Request) {
|
||
|
jsonfeedURL, err := ioutil.ReadAll(re.Body)
|
||
|
if err != nil {
|
||
|
http.Error(w, "bad data", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
defer re.Body.Close()
|
||
|
|
||
|
resp, err := http.Get(string(jsonfeedURL))
|
||
|
if err != nil {
|
||
|
ln.Error(re.Context(), err)
|
||
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
feed, err := jsonfeed.Parse(resp.Body)
|
||
|
if err != nil {
|
||
|
ln.Error(re.Context(), err)
|
||
|
http.Error(w, "bad feed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = r.Table("blogposts").Insert(feed.Items).Exec(mi.session)
|
||
|
if err != nil {
|
||
|
ln.Error(re.Context(), err)
|
||
|
http.Error(w, "can't save", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
fmt.Fprintf(w, "wrote %d blogposts\n", len(feed.Items))
|
||
|
}
|
||
|
|
||
|
type NewFeedItem struct {
|
||
|
NewVal *jsonfeed.Item `rethinkdb:"new_val"`
|
||
|
OldVal *jsonfeed.Item `rethinkdb:"old_val"`
|
||
|
}
|
||
|
|
||
|
func (mi *Mi) BlogPOSSE(ctx context.Context) {
|
||
|
res, err := r.Table("blogposts").Changes().Run(mi.session)
|
||
|
if err != nil {
|
||
|
ln.FatalErr(ctx, err)
|
||
|
}
|
||
|
defer res.Close()
|
||
|
|
||
|
ln.Log(ctx, ln.Info("streaming blogposts to mastodon, reddit and twitter"))
|
||
|
|
||
|
var newFeedItem NewFeedItem
|
||
|
for res.Next(&newFeedItem) {
|
||
|
ln.Log(ctx, ln.Info("got here"))
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
break
|
||
|
default:
|
||
|
}
|
||
|
|
||
|
if newFeedItem.NewVal == nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
err := mi.Push(ctx, Post{
|
||
|
Title: newFeedItem.NewVal.Title,
|
||
|
URL: newFeedItem.NewVal.URL,
|
||
|
Tags: newFeedItem.NewVal.Tags,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
ln.Error(ctx, err)
|
||
|
}
|
||
|
}
|
||
|
}
|