-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitunes.go
205 lines (158 loc) · 4.18 KB
/
itunes.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
// Package itunes extracts the underlying RSS feed from an iTunes page.
package itunes
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"regexp"
"golang.org/x/net/html"
)
const iTunesUA = "iTunes/10.1"
const maxRedirects = 3
// ErrNoFeed is returned by the ToRSS functions when they fail
// to find an RSS feed in the given iTunes page. This usually
// indicates an unsupported page type, such as a non-podcast
// iTunes page or an iTunesU page.
var ErrNoFeed = errors.New("no feed found")
// A Client is responsible for executing HTTP requests. Its
// interface is satisfied by http.Client. Provide your own
// implementation to intercept requests and responses.
type Client interface {
Do(req *http.Request) (*http.Response, error)
}
// ToRSS returns the underlying RSS feed from an iTunes URL
// using the default HTTP client.
func ToRSS(url string) (string, error) {
return ToRSSClient(url, nil)
}
// ToRSSClient returns the underlying RSS feed from an iTunes
// URL using the provided Client.
func ToRSSClient(url string, client Client) (string, error) {
if client == nil {
client = http.DefaultClient
}
feed, err := processURL(url, client, 0)
if err == io.EOF {
err = ErrNoFeed
}
return feed, err
}
func processURL(url string, client Client, redirects int) (string, error) {
resp, err := fetch(client, url)
if err != nil {
return "", fmt.Errorf("fetch error: %s", err)
}
defer resp.Body.Close()
ctype := resp.Header.Get("Content-Type")
media, _, err := mime.ParseMediaType(ctype)
if err != nil {
return "", fmt.Errorf("bad Content Type %q: %s", ctype, err)
}
switch media {
case "text/html":
return processHTML(resp.Body)
case "text/xml", "application/xml":
next, err := processXML(resp.Body)
if err != nil {
return "", err
}
redirects++
if redirects > maxRedirects {
return "", errors.New("too many redirects")
}
return processURL(next, client, redirects)
default:
return "", fmt.Errorf("unsupported Content Type %q", ctype)
}
}
func processHTML(r io.Reader) (string, error) {
var attr, val []byte
tagButton := []byte("button")
attrFeed := []byte("feed-url")
z := html.NewTokenizer(r)
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
if tt != html.StartTagToken {
continue
}
tag, hasAttrs := z.TagName()
if !bytes.Equal(tag, tagButton) {
continue
}
for hasAttrs {
attr, val, hasAttrs = z.TagAttr()
if bytes.Equal(attr, attrFeed) && len(val) > 0 {
return string(val), nil
}
}
}
return "", z.Err()
}
var (
// This line comes directly before the URL line in a Goto file.
prevLine = []byte("<key>kind</key><string>Goto</string>")
// This regex extracts the URL from a Goto file.
// Matches: <key>url</key><string>path/to/itunes-page</string>
reGoto = regexp.MustCompile(`^<key>url</key><string>(\S+)</string>$`)
)
func processXML(r io.Reader) (string, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if !bytes.Equal(scanner.Bytes(), prevLine) {
continue
}
if !scanner.Scan() {
break
}
matches := reGoto.FindSubmatch(scanner.Bytes())
if len(matches) != 2 {
continue
}
// Unescape URL.
// e.g. https://itunes.apple.com/WebObjects/DZR.woa/wa/viewPodcast?urlDesc=&id=1234567890
// becomes https://itunes.apple.com/WebObjects/DZR.woa/wa/viewPodcast?urlDesc=&id=1234567890
return html.UnescapeString(string(matches[1])), nil
}
err := scanner.Err()
if err == nil {
// If Scan() returns false but Err() is nil,
// we've reached the end of the input.
err = io.EOF
}
return "", err
}
func newRequest(u string) (*http.Request, error) {
req, err := http.NewRequest("GET", u, nil)
if err != nil {
if e, ok := err.(*url.Error); ok {
err = e.Err
}
return nil, err
}
// Make requests look like they come from iTunes.
req.Header.Set("User-Agent", iTunesUA)
return req, nil
}
func fetch(client Client, url string) (*http.Response, error) {
req, err := newRequest(url)
if err != nil {
return nil, fmt.Errorf("bad URL: %s", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, errors.New(resp.Status)
}
return resp, nil
}