aboutsummaryrefslogtreecommitdiffstats
path: root/internal/importer/importer.go
blob: de5e66c5584487b093c93ec4bf14cf11800aa580 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package importer

import (
	"bufio"
	"encoding/json"
	"encoding/xml"
	"errors"
	"io"
	"log"
	"os"
	"strings"

	"adammathes.com/neko/models/feed"
	"adammathes.com/neko/models/item"
)

type IItem struct {
	Title       string `json:"title"`
	Url         string `json:"url"`
	Description string `json:"description"`
	ReadState   bool   `json:"read"`
	Starred     bool   `json:"starred"`
	Date        *IDate `json:"date"`
	Feed        *IFeed `json:"feed"`
}

type IFeed struct {
	Url    string `json:"url"`
	Title  string `json:"title"`
	WebUrl string `json:"web_url"`
}

type IDate struct {
	Date string `json:"$date"`
}

type OPML struct {
	XMLName xml.Name `xml:"opml"`
	Version string   `xml:"version,attr"`
	Head    struct {
		Title string `xml:"title"`
	} `xml:"head"`
	Body struct {
		Outlines []Outline `xml:"outline"`
	} `xml:"body"`
}

type Outline struct {
	Text     string    `xml:"text,attr"`
	Title    string    `xml:"title,attr"`
	Type     string    `xml:"type,attr"`
	XMLURL   string    `xml:"xmlUrl,attr"`
	HTMLURL  string    `xml:"htmlUrl,attr"`
	Category string    `xml:"category,attr"`
	Outlines []Outline `xml:"outline"`
}

func ImportFeeds(format string, r io.Reader) error {
	switch format {
	case "opml":
		return ImportOPML(r)
	case "text":
		return ImportText(r)
	case "json":
		return ImportJSONReader(r)
	default:
		return errors.New("unsupported import format")
	}
}

func ImportOPML(r io.Reader) error {
	var o OPML
	if err := xml.NewDecoder(r).Decode(&o); err != nil {
		return err
	}

	var walk func([]Outline, string)
	walk = func(outlines []Outline, cat string) {
		for _, out := range outlines {
			if out.Type == "rss" || out.XMLURL != "" {
				f := &feed.Feed{
					Url:      out.XMLURL,
					Title:    out.Title,
					WebUrl:   out.HTMLURL,
					Category: cat,
				}
				if f.Title == "" {
					f.Title = out.Text
				}
				if f.Category == "" {
					f.Category = out.Category
				}
				err := f.Create()
				if err != nil {
					log.Printf("error importing %s: %v", f.Url, err)
				} else {
					log.Printf("imported %s", f.Url)
				}
			}
			if len(out.Outlines) > 0 {
				newCat := cat
				if out.XMLURL == "" && out.Text != "" {
					newCat = out.Text
				}
				walk(out.Outlines, newCat)
			}
		}
	}
	walk(o.Body.Outlines, "")
	return nil
}

func ImportText(r io.Reader) error {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if line == "" || strings.HasPrefix(line, "#") {
			continue
		}
		err := feed.NewFeed(line)
		if err != nil {
			log.Printf("error importing %s: %v", line, err)
		} else {
			log.Printf("imported %s", line)
		}
	}
	return scanner.Err()
}

func ImportJSONReader(r io.Reader) error {
	dec := json.NewDecoder(r)
	for {
		var ii IItem
		if err := dec.Decode(&ii); err == io.EOF {
			break
		} else if err != nil {
			return err
		} else {
			err := InsertIItem(&ii)
			if err != nil {
				log.Println(err)
			}
		}
	}
	return nil
}

func ImportJSON(filename string) error {
	f, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer func() { _ = f.Close() }()
	return ImportJSONReader(f)
}

func InsertIItem(ii *IItem) error {
	var f feed.Feed

	if ii.Feed == nil {
		return nil
	}
	err := f.ByUrl(ii.Feed.Url)
	if err != nil {
		f.Url = ii.Feed.Url
		f.Title = ii.Feed.Title
		f.WebUrl = ii.Feed.WebUrl
		err = f.Create()
		if err != nil {
			return err
		}
	}

	var i item.Item
	i.FeedId = f.Id
	i.Title = ii.Title
	i.Url = ii.Url
	i.Description = ii.Description

	if ii.Date != nil {
		i.PublishDate = ii.Date.Date
	}

	err = i.Create()
	return err
}