Skip to content

Commit

Permalink
feat(player): Add DownloadPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
yyoshiki41 committed Jul 22, 2017
1 parent 5841c18 commit c6c1539
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
39 changes: 30 additions & 9 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,42 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
)

const (
playerURL = "http://radiko.jp/apps/js/flash/myplayer-release.swf"

// swfextract
targetID = 12 // swfextract -b "12"
targetCode = 87 // swfextract "-b" 12
headerCWS = 8
headerRect = 5
rectNum = 4
headerRest = 2 + 2
binaryOffset = 6
)

// DownloadPlayer downloads a swf player file.
func DownloadPlayer(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}

resp, err := http.Get(playerURL)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = io.Copy(f, resp.Body)
if closeErr := f.Close(); err == nil {
err = closeErr
}
return err
}

func downloadBinary() ([]byte, error) {
resp, err := http.Get(playerURL)
if err != nil {
Expand All @@ -22,15 +52,6 @@ func downloadBinary() ([]byte, error) {
return swfExtract(resp.Body)
}

const targetID = 12 // swfextract -b "12"
const targetCode = 87 // swfextract "-b" 12

const headerCWS = 8
const headerRect = 5
const rectNum = 4
const headerRest = 2 + 2
const binaryOffset = 6

func swfExtract(body io.Reader) ([]byte, error) {
io.CopyN(ioutil.Discard, body, headerCWS)
zf, err := zlib.NewReader(body)
Expand Down
16 changes: 15 additions & 1 deletion player_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package radiko

import "testing"
import (
"path"
"testing"
)

func TestDownloadPlayer(t *testing.T) {
dir, removeDir := createTestTempDir(t)
defer removeDir() // clean up

playerPath := path.Join(dir, "myplayer.swf")
err := DownloadPlayer(playerPath)
if err != nil {
t.Errorf("Failed to download player.swf: %s", err)
}
}

func TestDownloadBinary(t *testing.T) {
_, err := downloadBinary()
Expand Down

0 comments on commit c6c1539

Please sign in to comment.