-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21b3a56
commit 20a5fbd
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package radiko | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
func TestAreaID(t *testing.T) { | ||
areaID, err := AreaID() | ||
if err != nil { | ||
t.Errorf("Failed to download player.swf: %s", err) | ||
} | ||
if areaID == "" { | ||
t.Errorf("Invalid area id: %s", areaID) | ||
} | ||
} | ||
|
||
func TestProcessSpanNode(t *testing.T) { | ||
s := `document.write('<span class="JP13">TOKYO JAPAN</span>');` | ||
doc, err := html.Parse(strings.NewReader(s)) | ||
if err != nil { | ||
t.Errorf("Parse HTML: %s", err) | ||
} | ||
|
||
areaID := processSpanNode(doc) | ||
if areaID != "JP13" { | ||
t.Errorf("Failed to process span node.\nAreaID: %s", | ||
areaID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters