-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.go
63 lines (54 loc) · 1.15 KB
/
rss.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
package main
import (
"fmt"
"strconv"
"strings"
"unicode/utf8"
"github.com/gorilla/feeds"
)
type RSSProvider struct {
}
func NewRSSProvider() *RSSProvider {
return &RSSProvider{}
}
func (p *RSSProvider) Feed(topicID int) (*feeds.Feed, error) {
downloader := NewPostDownloader()
title, posts, err := downloader.Download(topicID)
if err != nil {
return nil, err
}
feed := &feeds.Feed{
Title: title,
Link: &feeds.Link{},
Id: fmt.Sprintf("4pda-%d", topicID),
}
for _, post := range posts {
mainText := post.MainText
if utf8.RuneCountInString(mainText) > 50 {
words := strings.Fields(mainText)
if len(words) > 15 {
words = words[:15]
}
mainText = strings.Join(words, " ")
}
postID := post.Link
if postID == "" {
postID = strconv.FormatInt(post.ID, 10)
}
if !post.Updated.IsZero() {
postID += "#" + strconv.FormatInt(post.Updated.Unix(), 10)
}
item := &feeds.Item{
Title: mainText,
Link: &feeds.Link{
Href: post.Link,
},
Id: postID,
Created: post.Created,
Updated: post.Updated,
Description: post.HTML,
}
feed.Items = append(feed.Items, item)
}
return feed, nil
}