Cleanup (parse category better)
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2018-12-18 19:40:56 +01:00
parent c9f06518c1
commit 34133191fc
Signed by: peter
GPG Key ID: 374322D56E5209E8
4 changed files with 46 additions and 28 deletions

View File

@ -22,7 +22,6 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -51,6 +50,17 @@ type hubIncomingBackend struct {
baseURL string baseURL string
} }
type Feed struct {
ID int64 `redis:"id"`
Channel string `redis:"channel"`
URL string `redis:"url"`
Callback string `redis:"callback"`
Hub string `redis:"hub"`
Secret string `redis:"secret"`
LeaseSeconds int64 `redis:"lease_seconds"`
ResubscribeAt int64 `redis:"resubscribe_at"`
}
func (h *hubIncomingBackend) GetSecret(id int64) string { func (h *hubIncomingBackend) GetSecret(id int64) string {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -66,7 +76,6 @@ func (h *hubIncomingBackend) CreateFeed(topic string, channel string) (int64, er
defer conn.Close() defer conn.Close()
// TODO(peter): check if topic already is registered // TODO(peter): check if topic already is registered
id, err := redis.Int64(conn.Do("INCR", "feed:next_id")) id, err := redis.Int64(conn.Do("INCR", "feed:next_id"))
if err != nil { if err != nil {
return 0, err return 0, err
@ -80,12 +89,13 @@ func (h *hubIncomingBackend) CreateFeed(topic string, channel string) (int64, er
client := &http.Client{} client := &http.Client{}
hubURL, err := websub.GetHubURL(client, topic) hubURL, err := websub.GetHubURL(client, topic)
if hubURL == "" { if err != nil {
log.Printf("WebSub Hub URL not found for topic=%s\n", topic) log.Printf("WebSub Hub URL not found for topic=%s\n", topic)
} else { return 0, err
log.Printf("WebSub Hub URL found for topic=%s hub=%s\n", topic, hubURL)
} }
log.Printf("WebSub Hub URL found for topic=%s hub=%s\n", topic, hubURL)
callbackURL := fmt.Sprintf("%s/incoming/%d", h.baseURL, id) callbackURL := fmt.Sprintf("%s/incoming/%d", h.baseURL, id)
if err == nil && hubURL != "" { if err == nil && hubURL != "" {
@ -137,17 +147,6 @@ func (h *hubIncomingBackend) FeedSetLeaseSeconds(feedID int64, leaseSeconds int6
return nil return nil
} }
type Feed struct {
ID int64 `redis:"id"`
Channel string `redis:"channel"`
URL string `redis:"url"`
Callback string `redis:"callback"`
Hub string `redis:"hub"`
Secret string `redis:"secret"`
LeaseSeconds int64 `redis:"lease_seconds"`
ResubscribeAt int64 `redis:"resubscribe_at"`
}
func (h *hubIncomingBackend) GetFeeds() []Feed { func (h *hubIncomingBackend) GetFeeds() []Feed {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -209,10 +208,13 @@ func (h *hubIncomingBackend) run() error {
log.Printf("Looking at %s\n", feed.URL) log.Printf("Looking at %s\n", feed.URL)
if feed.ResubscribeAt == 0 || time.Now().After(time.Unix(feed.ResubscribeAt, 0)) { if feed.ResubscribeAt == 0 || time.Now().After(time.Unix(feed.ResubscribeAt, 0)) {
if feed.Callback == "" { if feed.Callback == "" {
feed.Callback = fmt.Sprintf("%s/incoming/%d", os.Getenv("EKSTER_BASEURL"), feed.ID) feed.Callback = fmt.Sprintf("%s/incoming/%d", h.baseURL, feed.ID)
} }
log.Printf("Send resubscribe for %s\n", feed.URL) log.Printf("Send resubscribe for %s\n", feed.URL)
h.Subscribe(&feed) err := h.Subscribe(&feed)
if err != nil {
log.Printf("Error while subscribing: %s", err)
}
} }
} }
case <-quit: case <-quit:

View File

@ -72,9 +72,7 @@ func FeedHeader(fetcher Fetcher, fetchURL, contentType string, body io.Reader) (
feed.Name = author.Name feed.Name = author.Name
feed.Photo = author.Photo feed.Photo = author.Photo
} else if strings.HasPrefix(contentType, "application/json") { // json feed? } else if strings.HasPrefix(contentType, "application/json") { // json feed?
var jfeed jsonfeed.Feed jfeed, err := jsonfeed.Parse(body)
dec := json.NewDecoder(body)
err := dec.Decode(&jfeed)
if err != nil { if err != nil {
log.Printf("Error while parsing json feed: %s\n", err) log.Printf("Error while parsing json feed: %s\n", err)
return feed, err return feed, err

View File

@ -96,8 +96,7 @@ func simplifyToItem(itemType string, item map[string][]interface{}) microsub.Ite
case "bookmark-of", "like-of", "repost-of", "in-reply-to": case "bookmark-of", "like-of", "repost-of", "in-reply-to":
u, withItem, refItem := simplifyRefItem(k, v) u, withItem, refItem := simplifyRefItem(k, v)
resultPtr := itemPtr(&feedItem, k) if resultPtr := itemPtr(&feedItem, k); resultPtr != nil {
if resultPtr != nil {
*resultPtr = append(*resultPtr, u) *resultPtr = append(*resultPtr, u)
if withItem { if withItem {
feedItem.Refs[u] = refItem feedItem.Refs[u] = refItem
@ -113,17 +112,23 @@ func simplifyToItem(itemType string, item map[string][]interface{}) microsub.Ite
author, _ := simplifyCard(v[0]) author, _ := simplifyCard(v[0])
feedItem.Checkin = &author feedItem.Checkin = &author
case "name", "published", "updated", "url", "uid", "latitude", "longitude": case "name", "published", "updated", "url", "uid", "latitude", "longitude":
resultPtr := getScalarPtr(&feedItem, k) if resultPtr := getScalarPtr(&feedItem, k); resultPtr != nil {
if resultPtr != nil {
if len(v) >= 1 { if len(v) >= 1 {
*resultPtr = v[0].(string) *resultPtr = v[0].(string)
} }
} }
case "category": case "category":
resultPtr := itemPtr(&feedItem, k) if resultPtr := itemPtr(&feedItem, k); resultPtr != nil {
if resultPtr != nil {
for _, c := range v { for _, c := range v {
*resultPtr = append(*resultPtr, c.(string)) switch t := c.(type) {
case microformats.Microformat:
// TODO: perhaps use name
if t.Value != "" {
*resultPtr = append(*resultPtr, t.Value)
}
case string:
*resultPtr = append(*resultPtr, t)
}
} }
} }
default: default:

View File

@ -17,6 +17,11 @@
*/ */
package jsonfeed package jsonfeed
import (
"encoding/json"
"io"
)
type Attachment struct { type Attachment struct {
URL string `json:"url"` URL string `json:"url"`
MimeType string `json:"mime_type"` MimeType string `json:"mime_type"`
@ -63,3 +68,11 @@ type Feed struct {
Items []Item `json:"items"` Items []Item `json:"items"`
Hubs []Hub `json:"hubs"` Hubs []Hub `json:"hubs"`
} }
// Parse parses a jsonfeed
func Parse(body io.Reader) (Feed, error) {
var feed Feed
dec := json.NewDecoder(body)
err := dec.Decode(&feed)
return feed, err
}