Skip to content

Instantly share code, notes, and snippets.

@ToQoz
Last active December 20, 2015 17:49
Show Gist options
  • Save ToQoz/6171370 to your computer and use it in GitHub Desktop.
Save ToQoz/6171370 to your computer and use it in GitHub Desktop.
URLからsoundcloudのリソースのタイプとIDとストリーミング再生できるかをとってくるやつ
package main
// CLIENT_ID=<your client id> go run resolve_soundcloud.go <soundcloud url>
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
)
const (
ApiEndPoint = "http://api.soundcloud.com"
)
var (
ClientId = os.Getenv("CLIENT_ID")
url = os.Args[1]
)
func main() {
fmt.Printf("Resolving %s\n", url)
item, err := ResolveSoundCloudURL(url)
if err != nil {
log.Println(err.Error())
return
}
fmt.Printf("ID: %d\n", item.Id)
fmt.Printf("Kind: %s\n", item.Kind)
fmt.Printf("Streamable: %t\n", item.Streamable)
}
type Item struct {
Id int
Kind string
Streamable bool
}
func ResolveSoundCloudURL(url string) (*Item, error) {
item := new(Item)
resp, apiErr := http.Get(ApiEndPoint + "/resolve.json?url=" + url + "&client_id=" + ClientId)
if apiErr != nil {
return item, apiErr
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return item, errors.New(resp.Status)
}
decodeErr := json.NewDecoder(resp.Body).Decode(item)
if decodeErr != nil {
return item, decodeErr
}
return item, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment