rss/rss_2.0.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package rss import ( "bytes" "encoding/xml" "fmt" "sort" "strings" "time" ) func parseRSS2(data []byte) (*Feed, error) { warnings := false feed := rss2_0Feed{} p := xml.NewDecoder(bytes.NewReader(data)) p.CharsetReader = charsetReader p.Strict = false err := p.Decode(&feed) if err != nil { return nil, err } if feed.Channel == nil { return nil, fmt.Errorf("no channel found in %q", string(data)) } channel := feed.Channel out := new(Feed) out.Title = channel.Title out.Language = channel.Language out.Author = channel.Author out.Description = channel.Description out.Categories = channel.Categories.toArray() for _, link := range channel.Link { if link.Rel == "" && link.Type == "" && link.Href == "" && link.Chardata != "" { out.Link = link.Chardata break } } out.Image = channel.Image.Image() if channel.MinsToLive != 0 { sort.Ints(channel.SkipHours) next := time.Now().Add(time.Duration(channel.MinsToLive) * time.Minute) for _, hour := range channel.SkipHours { if hour == next.Hour() { next = next.Add(time.Duration(60-next.Minute()) * time.Minute) } } trying := true for trying { trying = false for _, day := range channel.SkipDays { if strings.Title(day) == next.Weekday().String() { next = next.Add(time.Duration(24-next.Hour()) * time.Hour) trying = true break } } } out.Refresh = next } if out.Refresh.IsZero() { out.Refresh = time.Now().Add(DefaultRefreshInterval) } out.Items = make([]*Item, 0, len(channel.Items)) out.ItemMap = make(map[string]struct{}) // Process items. for _, item := range channel.Items { if item.ID == "" { if item.Link == "" { if debug { fmt.Printf("[w] Item %q has no ID or link and will be ignored.\n", item.Title) fmt.Printf("[w] %#v\n", item) } warnings = true continue } item.ID = item.Link } // Skip items already known. if _, ok := out.ItemMap[item.ID]; ok { continue } next := new(Item) next.Title = item.Title next.Summary = item.Description next.Categories = item.Categories next.Link = item.Link next.Image = item.Image.Image() if item.Date != "" { next.Date, err = parseTime(item.Date) if err == nil { next.DateValid = true } } else if item.PubDate != "" { next.Date, err = parseTime(item.PubDate) if err == nil { next.DateValid = true } } next.ID = item.ID if len(item.Enclosures) > 0 { next.Enclosures = make([]*Enclosure, len(item.Enclosures)) for i := range item.Enclosures { next.Enclosures[i] = item.Enclosures[i].Enclosure() } } next.Read = false out.Items = append(out.Items, next) out.ItemMap[next.ID] = struct{}{} out.Unread++ } if warnings && debug { fmt.Printf("[i] Encountered warnings:\n%s\n", data) } return out, nil } type rss2_0Feed struct { XMLName xml.Name `xml:"rss"` Channel *rss2_0Channel `xml:"channel"` } type rss2_0Category struct { XMLName xml.Name `xml:"category"` Name string `xml:"text,attr"` } type rss2_0CategorySlice []rss2_0Category func (r rss2_0CategorySlice) toArray() (result []string) { count := len(r) if count == 0 || r == nil { return } result = make([]string, count) for i, _ := range r { result[i] = r[i].Name } return } type rss2_0Channel struct { XMLName xml.Name `xml:"channel"` Title string `xml:"title"` Language string `xml:"language"` Author string `xml:"author"` Description string `xml:"description"` Link []rss2_0Link `xml:"link"` Image rss2_0Image `xml:"image"` Categories rss2_0CategorySlice `xml:"category"` Items []rss2_0Item `xml:"item"` MinsToLive int `xml:"ttl"` SkipHours []int `xml:"skipHours>hour"` SkipDays []string `xml:"skipDays>day"` } type rss2_0Link struct { Rel string `xml:"rel,attr"` Href string `xml:"href,attr"` Type string `xml:"type,attr"` Chardata string `xml:",chardata"` } type rss2_0Categories []string type rss2_0Item struct { XMLName xml.Name `xml:"item"` Title string `xml:"title"` Description string `xml:"description"` Categories rss2_0Categories `xml:"category"` Link string `xml:"link"` PubDate string `xml:"pubDate"` Date string `xml:"date"` Image rss2_0Image `xml:"image"` DateValid bool ID string `xml:"guid"` Enclosures []rss2_0Enclosure `xml:"enclosure"` } type rss2_0Enclosure struct { XMLName xml.Name `xml:"enclosure"` URL string `xml:"url,attr"` Type string `xml:"type,attr"` Length uint `xml:"length,attr"` } func (r *rss2_0Enclosure) Enclosure() *Enclosure { out := new(Enclosure) out.URL = r.URL out.Type = r.Type out.Length = r.Length return out } type rss2_0Image struct { XMLName xml.Name `xml:"image"` Href string `xml:"href,attr"` Title string `xml:"title"` URL string `xml:"url"` Height int `xml:"height"` Width int `xml:"width"` } func (i *rss2_0Image) Image() *Image { out := new(Image) out.Title = i.Title out.Href = i.Href out.URL = i.URL out.Height = uint32(i.Height) out.Width = uint32(i.Width) return out }