Move jsonfeed to it's own package
All checks were successful
the build was successful

- Also implement hubs for jsonfeed
This commit is contained in:
Peter Stuifzand 2018-08-04 22:43:57 +02:00
parent c1f2b7a5a7
commit 66489b6de7
5 changed files with 92 additions and 73 deletions

View File

@ -114,7 +114,6 @@ func (h *microsubHandler) checkAuthToken(header string, token *TokenResponse) bo
dec := json.NewDecoder(res.Body)
err = dec.Decode(&token)
if err != nil {
log.Printf("Error in json object: %v", err)
return false

View File

@ -37,6 +37,7 @@ import (
"p83.nl/go/ekster/pkg/microsub"
"github.com/garyburd/redigo/redis"
"p83.nl/go/ekster/pkg/jsonfeed"
"willnorris.com/go/microformats"
)
@ -100,7 +101,7 @@ func (b *memoryBackend) feedHeader(fetchURL, contentType string, body io.Reader)
}
}
} else if strings.HasPrefix(contentType, "application/json") { // json feed?
var jfeed JSONFeed
var jfeed jsonfeed.Feed
dec := json.NewDecoder(body)
err := dec.Decode(&jfeed)
if err != nil {
@ -224,7 +225,7 @@ func (b *memoryBackend) feedItems(fetchURL, contentType string, body io.Reader)
items = append(items, item)
}
} else if strings.HasPrefix(contentType, "application/json") { // json feed?
var feed JSONFeed
var feed jsonfeed.Feed
dec := json.NewDecoder(body)
err := dec.Decode(&feed)
if err != nil {

View File

@ -1,65 +0,0 @@
/*
Microsub server
Copyright (C) 2018 Peter Stuifzand
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
type JSONFeedAttachment struct {
URL string `json:"url"`
MimeType string `json:"mime_type"`
Title string `json:"title,omitempty"`
SizeInBytes int `json:"size_in_bytes,omitempty"`
DurationInSeconds int `json:"duration_in_seconds,omitempty"`
}
type JSONFeedItem struct {
ID string `json:"id"`
ContentText string `json:"content_text,omitempty"`
ContentHTML string `json:"content_html,omitempty"`
Summary string `json:"summary,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Image string `json:"image,omitempty"`
ExternalURL string `json:"external_url,omitempty"`
DatePublished string `json:"date_published,omitempty"`
Author JSONFeedAuthor `json:"author,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []JSONFeedAttachment `json:"attachments,omitempty"`
}
type JSONFeedAuthor struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Avatar string `json:"avatar,omitempty"`
}
type JSONFeedHub struct {
Type string `json:"type"`
URL string `json:"url"`
}
type JSONFeed struct {
Version string `json:"version"`
Title string `json:"title"`
HomePageURL string `json:"home_page_url"`
FeedURL string `json:"feed_url"`
NextUrl string `json:"next_url"`
Icon string `json:"icon"`
Favicon string `json:"favicon"`
Author JSONFeedAuthor `json:"author,omitempty"`
Items []JSONFeedItem `json:"items"`
Hubs []JSONFeedHub `json:"hubs"`
}

65
pkg/jsonfeed/jsonfeed.go Normal file
View File

@ -0,0 +1,65 @@
/*
Microsub server
Copyright (C) 2018 Peter Stuifzand
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jsonfeed
type Attachment struct {
URL string `json:"url"`
MimeType string `json:"mime_type"`
Title string `json:"title,omitempty"`
SizeInBytes int `json:"size_in_bytes,omitempty"`
DurationInSeconds int `json:"duration_in_seconds,omitempty"`
}
type Item struct {
ID string `json:"id"`
ContentText string `json:"content_text,omitempty"`
ContentHTML string `json:"content_html,omitempty"`
Summary string `json:"summary,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Image string `json:"image,omitempty"`
ExternalURL string `json:"external_url,omitempty"`
DatePublished string `json:"date_published,omitempty"`
Author Author `json:"author,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
type Author struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Avatar string `json:"avatar,omitempty"`
}
type Hub struct {
Type string `json:"type"`
URL string `json:"url"`
}
type Feed struct {
Version string `json:"version"`
Title string `json:"title"`
HomePageURL string `json:"home_page_url"`
FeedURL string `json:"feed_url"`
NextUrl string `json:"next_url"`
Icon string `json:"icon"`
Favicon string `json:"favicon"`
Author Author `json:"author,omitempty"`
Items []Item `json:"items"`
Hubs []Hub `json:"hubs"`
}

View File

@ -1,8 +1,10 @@
package websub
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
@ -10,6 +12,7 @@ import (
"linkheader"
"rss"
"p83.nl/go/ekster/pkg/jsonfeed"
"willnorris.com/go/microformats"
)
@ -25,7 +28,7 @@ func GetHubURL(client *http.Client, topic string) (string, error) {
return hubURL, err
}
return "", fmt.Errorf("No hub url found for topic %s", topic)
return "", fmt.Errorf("no hub url found for topic %s", topic)
}
func isFeedContentType(contentType string) bool {
@ -67,7 +70,7 @@ func parseBodyLinks(client *http.Client, topic string) (string, error) {
if feed.HubURL != "" {
return feed.HubURL, nil
}
return "", fmt.Errorf("No WebSub hub url found in the RSS feed")
return "", fmt.Errorf("no WebSub hub url found in the RSS feed")
} else if strings.HasPrefix(contentType, "text/html") {
topicURL, _ := url.Parse(topic)
md := microformats.Parse(resp.Body, topicURL)
@ -76,10 +79,26 @@ func parseBodyLinks(client *http.Client, topic string) (string, error) {
return hubs[0], nil
}
}
return "", fmt.Errorf("No WebSub hub url found in HTML <link> elements")
return "", fmt.Errorf("no WebSub hub url found in HTML <link> elements")
} else if strings.HasPrefix(contentType, "application/json") {
var feed jsonfeed.Feed
dec := json.NewDecoder(resp.Body)
err := dec.Decode(&feed)
if err != nil {
log.Printf("error while parsing json feed: %s\n", err)
return "", err
}
for _, v := range feed.Hubs {
if v.Type == "WebSub" {
return v.URL, nil
}
}
return "", fmt.Errorf("no WebSub hub url found in jsonfeed")
}
return "", fmt.Errorf("Unknown content type of response: %s", resp.Header.Get("Content-Type"))
return "", fmt.Errorf("unknown content type of response: %s", resp.Header.Get("Content-Type"))
}
func parseLinkHeaders(client *http.Client, topic string) (string, error) {
@ -100,7 +119,7 @@ func parseLinkHeaders(client *http.Client, topic string) (string, error) {
}
}
return "", fmt.Errorf("No hub url found in HTTP Link headers")
return "", fmt.Errorf("no hub url found in HTTP Link headers")
}
// Subscribe subscribes topicURL on hubURL