Support playback from http:// and ipfs:// URLs

This commit is contained in:
Marcin Kulik
2015-09-18 19:55:02 +02:00
parent c3ba15ba56
commit d57519bfba
2 changed files with 41 additions and 12 deletions

View File

@@ -2,7 +2,8 @@
## master
* Added ability to replay asciicast from stdin by passing "-" as filename
* Playing asciicast from stdin: `cat demo.json | asciinema play -`
* Playing asciicast from IPFS: `asciinema play ipfs://QmcdXYJp6e4zNuimuGeWPwNMHQdxuqWmKx7NhZofQ1nw2V`
* `-q/--quiet` option added to `rec` command
## 1.1.1 (2015-06-21)

View File

@@ -3,8 +3,11 @@ package asciicast
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Env struct {
@@ -56,25 +59,50 @@ func Save(asciicast *Asciicast, path string) error {
return nil
}
func Load(path string) (*Asciicast, error) {
var file *os.File
// asciinema play file.json
// asciinema play https://asciinema.org/a/123.json
// asciinema play ipfs://QmbdpNCwqeZgnmAWBCQcs8u6Ts6P2ku97tfKAycE1XY88p
// asciinema play -
func getSource(url string) (io.ReadCloser, error) {
if strings.HasPrefix(url, "ipfs://") {
hash := url[7:len(url)]
url = fmt.Sprintf("https://ipfs.io/ipfs/%v", hash)
}
if url == "-" {
return os.Stdin, nil
}
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
resp, err := http.Get(url)
if path == "-" {
file = os.Stdin
} else {
var err error
file, err = os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, fmt.Errorf("got status %v when requesting %v", resp.StatusCode, url)
}
return resp.Body, nil
}
dec := json.NewDecoder(file)
return os.Open(url)
}
func Load(url string) (*Asciicast, error) {
source, err := getSource(url)
if err != nil {
return nil, err
}
defer source.Close()
dec := json.NewDecoder(source)
asciicast := &Asciicast{}
err := dec.Decode(asciicast)
if err != nil {
if err = dec.Decode(asciicast); err != nil {
return nil, err
}