aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/rdf/rdf.go
blob: ad4d53a08fec4e41f1429edb2b1efa435c79f93f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package rdf

import (
	"encoding/xml"
	"strings"
	"time"

	"github.com/miniflux/miniflux/helper"
	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/reader/sanitizer"
)

type rdfFeed struct {
	XMLName xml.Name  `xml:"RDF"`
	Title   string    `xml:"channel>title"`
	Link    string    `xml:"channel>link"`
	Creator string    `xml:"channel>creator"`
	Items   []rdfItem `xml:"item"`
}

func (r *rdfFeed) Transform() *model.Feed {
	feed := new(model.Feed)
	feed.Title = sanitizer.StripTags(r.Title)
	feed.SiteURL = r.Link

	for _, item := range r.Items {
		entry := item.Transform()

		if entry.Author == "" && r.Creator != "" {
			entry.Author = sanitizer.StripTags(r.Creator)
		}

		if entry.URL == "" {
			entry.URL = feed.SiteURL
		}

		feed.Entries = append(feed.Entries, entry)
	}

	return feed
}

type rdfItem struct {
	Title       string `xml:"title"`
	Link        string `xml:"link"`
	Description string `xml:"description"`
	Creator     string `xml:"creator"`
}

func (r *rdfItem) Transform() *model.Entry {
	entry := new(model.Entry)
	entry.Title = strings.TrimSpace(r.Title)
	entry.Author = strings.TrimSpace(r.Creator)
	entry.URL = r.Link
	entry.Content = r.Description
	entry.Hash = getHash(r)
	entry.Date = time.Now()
	return entry
}

func getHash(r *rdfItem) string {
	value := r.Link
	if value == "" {
		value = r.Title + r.Description
	}

	return helper.Hash(value)
}