forked from yyoshiki41/go-radiko
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea.go
More file actions
44 lines (35 loc) · 686 Bytes
/
area.go
File metadata and controls
44 lines (35 loc) · 686 Bytes
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
package radiko
import (
"net/http"
"golang.org/x/net/html"
)
const (
areaURL = "http://radiko.jp/area"
)
// AreaID returns areaID.
func AreaID() (string, error) {
resp, err := http.Get(areaURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
return "", err
}
return processSpanNode(doc), nil
}
func processSpanNode(n *html.Node) string {
var areaID string
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "span" && len(n.Attr) > 0 {
areaID = n.Attr[0].Val
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(n)
return areaID
}