-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacastClient.go
187 lines (147 loc) · 3.75 KB
/
acastClient.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
package gocast
import (
"github.com/beevik/etree"
"net/http"
"fmt"
"io/ioutil"
"os"
"io"
"gopkg.in/cheggaaa/pb.v1"
"path/filepath"
"regexp"
"sort"
)
var invalidFileNameRegex = regexp.MustCompile(regexp.QuoteMeta(`[\\/:"*?<>|]+`))
type AcastClient struct {
url string
document *etree.Document
channel *etree.Element
episodes map[int]*Episode
}
type Episode struct {
title string
media string
}
func NewAcastClient(url string) (*AcastClient, error) {
client := &AcastClient{}
client.url = url
document, err := client.reload()
if err != nil {
return nil, err
}
client.document = document
root := client.document.SelectElement("rss")
client.channel = root.SelectElement("channel")
fmt.Println(client.channel.SelectElement("title").Text())
client.episodes = *client.GetAllEpisodes()
return client, nil
}
func (c AcastClient) reload() (*etree.Document, error) {
response, err := http.Get(c.url)
if err != nil {
return nil, err
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
doc := etree.NewDocument()
if err := doc.ReadFromString(string(content)); err != nil {
panic(err)
}
return doc, nil
}
func (c AcastClient) GetAllEpisodes() *map[int]*Episode {
if c.channel == nil {
return nil
}
episodes := make(map[int]*Episode)
for index, episode := range c.channel.SelectElements("item") {
title := episode.SelectElement("title")
enclosure := episode.SelectElement("enclosure")
media := enclosure.SelectAttrValue("url", "")
if len(media) == 0 {
fmt.Printf("\nCould not find media link for %s", title)
} else {
episodes[index] = &Episode{title: title.Text(), media: media}
}
}
return &episodes
}
func (c AcastClient) ListAllEpisodes() {
if c.episodes == nil {
return
}
var keys []int
for k := range c.episodes {
keys = append(keys, k)
}
sort.Ints(keys)
for _, key := range keys {
fmt.Printf("%d) %s\n", key, c.episodes[key].title)
}
}
func (c AcastClient) DownloadAllEpisodes(outputPath string) error {
if c.channel == nil {
return nil
}
fmt.Printf("Downloading all episodes")
episodeCount := len(c.episodes)
if c.episodes == nil || episodeCount == 0 {
fmt.Println("Could not find episodes")
}
for index, episode := range c.episodes {
filename := invalidFileNameRegex.ReplaceAllString(episode.title, "")
fmt.Printf("Downloading [%d/%d]: %s\n", index+1, episodeCount, episode.title)
err := c.download(episode.media, filepath.Join(outputPath, fmt.Sprintf("%s.mp3", filename)))
if err != nil {
return err
}
}
return nil
}
func (c AcastClient) DownloadEpisode(index int, outputPath string) error {
if c.channel == nil {
return nil
}
episode := c.episodes[index]
if len(episode.media) == 0 {
fmt.Printf("\nError downloading %s. Could not find media link.", episode.title)
return nil
}
filename := invalidFileNameRegex.ReplaceAllString(episode.title, "")
fmt.Printf("Downloading episode: %s\n", episode.title)
outputPath = filepath.Join(outputPath, fmt.Sprintf("%s.mp3", filename))
c.download(episode.media, outputPath)
return nil
}
func (c AcastClient) download(url string, output string) (err error) {
// create the file
out, err := os.Create(output)
if err != nil {
return err
}
defer out.Close()
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
// create and start bar
bar := pb.New(int(response.ContentLength)).SetUnits(pb.U_BYTES)
bar.Start()
// create proxy reader
reader := bar.NewProxyReader(response.Body)
// check server response
if response.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", response.Status)
}
// Writer the body to file
_, err = io.Copy(out, reader)
if err != nil {
return err
}
bar.Finish()
return nil
}