-
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
Showing
6 changed files
with
93 additions
and
91 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,31 +1,92 @@ | ||
package radiko | ||
|
||
import ( | ||
"compress/zlib" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
const ( | ||
playerURL = "http://radiko.jp/apps/js/flash/myplayer-release.swf" | ||
) | ||
|
||
// DownloadPlayer downloads a swf player file. | ||
func DownloadPlayer(path string) error { | ||
func downloadBinary() ([]byte, error) { | ||
resp, err := http.Get(playerURL) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
f, err := os.Create(path) | ||
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) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
buf, err := ioutil.ReadAll(zf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
offset := 0 | ||
|
||
// Skip Rect | ||
rectSize := int(buf[offset] >> 3) | ||
rectOffset := (headerRect + rectNum*rectSize + 7) / 8 | ||
|
||
offset += rectOffset | ||
|
||
// Skip the rest header | ||
offset += headerRest | ||
|
||
// Read tags | ||
for i := 0; ; i++ { | ||
// tag code | ||
code := int(buf[offset+1])<<2 + int(buf[offset])>>6 | ||
|
||
// tag length | ||
len := int(buf[offset] & 0x3f) | ||
|
||
// Skip tag header | ||
offset += 2 | ||
|
||
// tag length (if long version) | ||
if len == 0x3f { | ||
len = int(buf[offset]) | ||
len += int(buf[offset+1]) << 8 | ||
len += int(buf[offset+2]) << 16 | ||
len += int(buf[offset+3]) << 24 | ||
|
||
// skip tag lentgh header | ||
offset += 4 | ||
} | ||
|
||
// Not found... | ||
if code == 0 { | ||
return nil, errors.New("swf extract failed") | ||
} | ||
// tag ID | ||
id := int(buf[offset]) + int(buf[offset+1])<<8 | ||
|
||
// Found? | ||
if code == targetCode && id == targetID { | ||
return buf[offset+binaryOffset : offset+len], nil | ||
} | ||
|
||
_, err = io.Copy(f, resp.Body) | ||
if closeErr := f.Close(); err == nil { | ||
err = closeErr | ||
offset += len | ||
} | ||
return err | ||
} |
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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
package radiko | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
) | ||
import "testing" | ||
|
||
func TestDownloadPlayer(t *testing.T) { | ||
dir, removeDir := createTestTempDir(t) | ||
defer removeDir() // clean up | ||
|
||
playerPath := path.Join(dir, "myplayer.swf") | ||
err := DownloadPlayer(playerPath) | ||
func TestDownloadBinary(t *testing.T) { | ||
_, err := downloadBinary() | ||
if err != nil { | ||
t.Errorf("Failed to download player.swf: %s", err) | ||
t.Errorf("Failed to download binary: %s", err) | ||
} | ||
} |