-
Notifications
You must be signed in to change notification settings - Fork 4
/
feed.go
71 lines (62 loc) · 2.02 KB
/
feed.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
package main
import (
"encoding/xml"
"errors"
"log"
"time"
)
func FeedEntryToRss2Item(entry *FeedEntry) (item Rss2Item) {
if nil == entry || nil == entry.Link || nil == entry.Cache {
log.Println("[ERROR] got invalid entry: entry is nil or entry.Link is nil or entry.Cache is nil")
return
}
item.Title = entry.Title
item.Link = entry.Link.String()
item.Description = string(entry.Content)
if nil != entry.PubDate {
item.PubDate = entry.PubDate.Format(time.RFC1123Z)
} else if nil != entry.Cache.LastModified {
item.PubDate = entry.Cache.LastModified.Format(time.RFC1123Z)
} else if nil != entry.Cache.Date {
item.PubDate = entry.Cache.Date.Format(time.RFC1123Z)
} else {
log.Printf("[ERROR] entry's cache date is nil %s", entry.Link.String())
item.PubDate = time.Now().Format(time.RFC1123Z)
}
item.Guid = entry.Link.String()
return
}
func GenerateRss2Feed(feed *Feed) (rss2FeedStr []byte, err error) {
if nil == feed || nil == feed.URL {
log.Println("[ERROR] Got empty feed, wll ignore it")
err = errors.New("Empty feed")
return
}
rss2Feed := &Rss2Feed{Version: FEED_VERSION}
rss2Feed.Channel = Rss2Channel{
Title: feed.Title,
Link: feed.URL.String(),
Description: "",
PubDate: feed.LastModified.Format(time.RFC1123Z),
Generator: GOFEED_NAME + " " + GOFEED_VERSION,
}
rss2Feed.Channel.Items = make([]Rss2Item, len(feed.Entries))
itemInd := 0
for entryInd, entry := range feed.Entries {
if nil == entry {
log.Printf("[ERROR] got nil entry at index %d", entryInd)
} else if nil == entry.Link || nil == entry.Cache {
log.Println("[WARN] Ignore invalid feed entry: link or cache is nil")
} else if 0 == len(entry.Content) {
log.Printf("[WARN] Ignore empty feed entry %s", entry.Link.String())
} else {
rss2Feed.Channel.Items = append(rss2Feed.Channel.Items[:itemInd], FeedEntryToRss2Item(entry))
itemInd += 1
}
}
rss2FeedStr, err = xml.MarshalIndent(rss2Feed, " ", " ")
if nil != err {
log.Printf("[ERROR] failed to marshal rss2 feed: %s", err)
}
return
}