Last active
December 20, 2015 17:49
-
-
Save ToQoz/6171370 to your computer and use it in GitHub Desktop.
URLからsoundcloudのリソースのタイプとIDとストリーミング再生できるかをとってくるやつ
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
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