mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
Export only necessary structs in JsonFeed package
This commit is contained in:
parent
6618caca81
commit
e91a9b4f13
2 changed files with 30 additions and 29 deletions
|
@ -5,44 +5,45 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/miniflux/miniflux2/helper"
|
"github.com/miniflux/miniflux2/helper"
|
||||||
"github.com/miniflux/miniflux2/model"
|
"github.com/miniflux/miniflux2/model"
|
||||||
"github.com/miniflux/miniflux2/reader/feed/date"
|
"github.com/miniflux/miniflux2/reader/feed/date"
|
||||||
"github.com/miniflux/miniflux2/reader/processor"
|
"github.com/miniflux/miniflux2/reader/processor"
|
||||||
"github.com/miniflux/miniflux2/reader/sanitizer"
|
"github.com/miniflux/miniflux2/reader/sanitizer"
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type JsonFeed struct {
|
type jsonFeed struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
SiteURL string `json:"home_page_url"`
|
SiteURL string `json:"home_page_url"`
|
||||||
FeedURL string `json:"feed_url"`
|
FeedURL string `json:"feed_url"`
|
||||||
Author JsonAuthor `json:"author"`
|
Author jsonAuthor `json:"author"`
|
||||||
Items []JsonItem `json:"items"`
|
Items []jsonItem `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonAuthor struct {
|
type jsonAuthor struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonItem struct {
|
type jsonItem struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
Text string `json:"content_text"`
|
Text string `json:"content_text"`
|
||||||
Html string `json:"content_html"`
|
HTML string `json:"content_html"`
|
||||||
DatePublished string `json:"date_published"`
|
DatePublished string `json:"date_published"`
|
||||||
DateModified string `json:"date_modified"`
|
DateModified string `json:"date_modified"`
|
||||||
Author JsonAuthor `json:"author"`
|
Author jsonAuthor `json:"author"`
|
||||||
Attachments []JsonAttachment `json:"attachments"`
|
Attachments []jsonAttachment `json:"attachments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonAttachment struct {
|
type jsonAttachment struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
MimeType string `json:"mime_type"`
|
MimeType string `json:"mime_type"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
@ -50,11 +51,11 @@ type JsonAttachment struct {
|
||||||
Duration int `json:"duration_in_seconds"`
|
Duration int `json:"duration_in_seconds"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonFeed) GetAuthor() string {
|
func (j *jsonFeed) GetAuthor() string {
|
||||||
return getAuthor(j.Author)
|
return getAuthor(j.Author)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonFeed) Transform() *model.Feed {
|
func (j *jsonFeed) Transform() *model.Feed {
|
||||||
feed := new(model.Feed)
|
feed := new(model.Feed)
|
||||||
feed.FeedURL = j.FeedURL
|
feed.FeedURL = j.FeedURL
|
||||||
feed.SiteURL = j.SiteURL
|
feed.SiteURL = j.SiteURL
|
||||||
|
@ -76,7 +77,7 @@ func (j *JsonFeed) Transform() *model.Feed {
|
||||||
return feed
|
return feed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetDate() time.Time {
|
func (j *jsonItem) GetDate() time.Time {
|
||||||
for _, value := range []string{j.DatePublished, j.DateModified} {
|
for _, value := range []string{j.DatePublished, j.DateModified} {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
d, err := date.Parse(value)
|
d, err := date.Parse(value)
|
||||||
|
@ -92,12 +93,12 @@ func (j *JsonItem) GetDate() time.Time {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetAuthor() string {
|
func (j *jsonItem) GetAuthor() string {
|
||||||
return getAuthor(j.Author)
|
return getAuthor(j.Author)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetHash() string {
|
func (j *jsonItem) GetHash() string {
|
||||||
for _, value := range []string{j.ID, j.URL, j.Text + j.Html + j.Summary} {
|
for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return helper.Hash(value)
|
return helper.Hash(value)
|
||||||
}
|
}
|
||||||
|
@ -106,8 +107,8 @@ func (j *JsonItem) GetHash() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetTitle() string {
|
func (j *jsonItem) GetTitle() string {
|
||||||
for _, value := range []string{j.Title, j.Summary, j.Text, j.Html} {
|
for _, value := range []string{j.Title, j.Summary, j.Text, j.HTML} {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return truncate(value)
|
return truncate(value)
|
||||||
}
|
}
|
||||||
|
@ -116,8 +117,8 @@ func (j *JsonItem) GetTitle() string {
|
||||||
return j.URL
|
return j.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetContent() string {
|
func (j *jsonItem) GetContent() string {
|
||||||
for _, value := range []string{j.Html, j.Text, j.Summary} {
|
for _, value := range []string{j.HTML, j.Text, j.Summary} {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
@ -126,7 +127,7 @@ func (j *JsonItem) GetContent() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) GetEnclosures() model.EnclosureList {
|
func (j *jsonItem) GetEnclosures() model.EnclosureList {
|
||||||
enclosures := make(model.EnclosureList, 0)
|
enclosures := make(model.EnclosureList, 0)
|
||||||
|
|
||||||
for _, attachment := range j.Attachments {
|
for _, attachment := range j.Attachments {
|
||||||
|
@ -140,7 +141,7 @@ func (j *JsonItem) GetEnclosures() model.EnclosureList {
|
||||||
return enclosures
|
return enclosures
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JsonItem) Transform() *model.Entry {
|
func (j *jsonItem) Transform() *model.Entry {
|
||||||
entry := new(model.Entry)
|
entry := new(model.Entry)
|
||||||
entry.URL = j.URL
|
entry.URL = j.URL
|
||||||
entry.Date = j.GetDate()
|
entry.Date = j.GetDate()
|
||||||
|
@ -152,7 +153,7 @@ func (j *JsonItem) Transform() *model.Entry {
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAuthor(author JsonAuthor) string {
|
func getAuthor(author jsonAuthor) string {
|
||||||
if author.Name != "" {
|
if author.Name != "" {
|
||||||
return author.Name
|
return author.Name
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,13 @@ import (
|
||||||
"github.com/miniflux/miniflux2/model"
|
"github.com/miniflux/miniflux2/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse returns a normalized feed struct.
|
// Parse returns a normalized feed struct from a JON feed.
|
||||||
func Parse(data io.Reader) (*model.Feed, error) {
|
func Parse(data io.Reader) (*model.Feed, error) {
|
||||||
jsonFeed := new(JsonFeed)
|
feed := new(jsonFeed)
|
||||||
decoder := json.NewDecoder(data)
|
decoder := json.NewDecoder(data)
|
||||||
if err := decoder.Decode(&jsonFeed); err != nil {
|
if err := decoder.Decode(&feed); err != nil {
|
||||||
return nil, errors.NewLocalizedError("Unable to parse JSON Feed: %v", err)
|
return nil, errors.NewLocalizedError("Unable to parse JSON Feed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonFeed.Transform(), nil
|
return feed.Transform(), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue