-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcastfeed.go
87 lines (71 loc) · 1.75 KB
/
podcastfeed.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
package main
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"strings"
)
type Rss struct {
XMLName xml.Name `xml:"rss"`
Channel Channel `xml:"channel"`
}
type Channel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Description string `xml:"description"`
LastBuildDate string `xml:"lastBuildDate"`
Items []Item `xml:"item"`
}
type Item struct {
Id string `xml:"guid"`
Title string `xml:"title"`
Published string `xml:"pubDate"`
Enclosure Enclosure `xml:"enclosure"`
}
type Enclosure struct {
Url string `xml:"url,attr"`
Length int `xml:"length,attr"`
}
func (i Item) String() string {
return fmt.Sprintf("Id: %v, Title: %v, Url: %v", i.Id, i.Title, i.Enclosure.Url)
}
func LoadFeed(name string) (*Channel, error) {
data, err := ioutil.ReadFile(name)
if err != nil {
return nil, err
}
return toFeed(data)
}
func toFeed(data []byte) (*Channel, error) {
var r Rss
err := xml.Unmarshal(data, &r)
if err != nil {
return nil, err
}
c := r.Channel
fmt.Printf("Title: %v, LastBuildDate: %v\n", c.Title, c.LastBuildDate)
fmt.Printf("Items: %v\n", len(c.Items))
c.PrintLatestItems()
return &c, nil
}
func (feed *Channel) GetItemAtIndex(index int) (*Item, error) {
if len(feed.Items) <= index {
return nil, errors.New("index out of bounds")
}
item := feed.Items[index]
return &item, nil
}
func (feed *Channel) PrintLatestItems() {
latestItems := feed.Items[0:5]
fmt.Println("Latest Items...")
for index, item := range latestItems {
fmt.Printf("%v - %v\n", index, item)
}
}
func (item *Item) GetFileName(channel *Channel) string {
name := strings.TrimPrefix(item.Title, channel.Title)
name = strings.TrimSpace(name) + ".mp3"
fmt.Println(name)
return name
}