├── README.md ├── _example └── bgm │ ├── main.go │ ├── play_any.go │ └── play_darwin.go └── api.go /README.md: -------------------------------------------------------------------------------- 1 | # iTunes Search API 2 | iTunes Search API for Go. 3 | 4 | # Installation 5 | ```golang 6 | go get github.com/mattn/itunes-search-api 7 | ``` 8 | 9 | # Examples 10 | ```golang 11 | import( 12 | "github.com/mattn/itunes-search-api" 13 | ) 14 | ``` 15 | # Search Example 16 | ```golang 17 | results, err := itunessearch.Search("shinedown", "US", "music") 18 | if err != nil { 19 | log.Fatal(err) 20 | } 21 | ``` 22 | # Lookup Example 23 | ```golang 24 | lookup, err := itunessearch.Lookup("id", "1202948413", "", "1", "") 25 | if err != nil { 26 | log.Fatal(err) 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /_example/bgm/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/mattn/itunes-search-api" 8 | ) 9 | 10 | func main() { 11 | results, err := itunessearch.Search("マツケンサンバ", "JP", "music") 12 | if err != nil { 13 | log.Fatal(err) 14 | } 15 | for _, result := range results.Results { 16 | fmt.Println(result.ArtistName, result.TrackName, result.CollectionViewUrl) 17 | playURL(result.PreviewUrl) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /_example/bgm/play_any.go: -------------------------------------------------------------------------------- 1 | // +build !darwin 2 | 3 | package main 4 | 5 | import ( 6 | "errors" 7 | "os" 8 | "os/exec" 9 | ) 10 | 11 | func playURL(url string) error { 12 | var cmd *exec.Cmd 13 | for _, player := range []string{"ffplay", "avplay"} { 14 | f, err := exec.LookPath(player) 15 | if err == nil { 16 | args := []string{"-autoexit", "-nodisp", url} 17 | cmd = exec.Command(f, args...) 18 | cmd.Stdin = os.Stdin 19 | return cmd.Run() 20 | } 21 | } 22 | 23 | f, err := exec.LookPath("mplayer") 24 | if err == nil { 25 | cmd = exec.Command(f, url) 26 | cmd.Stdin = os.Stdin 27 | return cmd.Run() 28 | } 29 | return errors.New("player not found") 30 | } 31 | -------------------------------------------------------------------------------- /_example/bgm/play_darwin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | #cgo CFLAGS: -x objective-c 5 | #cgo LDFLAGS: -framework Foundation -framework AVFoundation -framework CoreMedia 6 | 7 | #import 8 | 9 | static int _playURL(const char* url) { 10 | @autoreleasepool { 11 | NSURL* u = [NSURL URLWithString:[NSString stringWithUTF8String:url]]; 12 | 13 | AVPlayer* p = [AVPlayer playerWithURL:u]; 14 | [p play]; 15 | 16 | NSTimeInterval played = 0.; 17 | while (1) { 18 | NSTimeInterval t = CMTimeGetSeconds([p currentTime]); 19 | if (t > 0. && t == played) { 20 | break; 21 | } 22 | played = t; 23 | 24 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 25 | beforeDate:[[NSDate date] dateByAddingTimeInterval:.1]]; 26 | 27 | } 28 | 29 | return 0; 30 | } 31 | } 32 | 33 | */ 34 | import "C" 35 | import "unsafe" 36 | 37 | import ( 38 | "errors" 39 | ) 40 | 41 | func playURL(url string) error { 42 | c := C.CString(url) 43 | defer C.free(unsafe.Pointer(c)) 44 | 45 | if r := C._playURL(c); r != 0 { 46 | return errors.New("play error") 47 | } 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /api.go: -------------------------------------------------------------------------------- 1 | package itunessearch 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | "net/url" 7 | ) 8 | 9 | type SearchMap struct { 10 | ArtistId float64 `json:"artistId"` 11 | ArtistName string `json:"artistName"` 12 | ArtistViewUrl string `json:"artistViewUrl"` 13 | ArtworkUrl100 string `json:"artworkUrl100"` 14 | ArtworkUrl30 string `json:"artworkUrl30"` 15 | ArtworkUrl60 string `json:"artworkUrl60"` 16 | CollectionCensoredName string `json:"collectionCensoredName"` 17 | CollectionExplicitness string `json:"collectionExplicitness"` 18 | CollectionId float64 `json:"collectionId"` 19 | CollectionName string `json:"collectionName"` 20 | CollectionPrice float64 `json:"collectionPrice"` 21 | CollectionViewUrl string `json:"collectionViewUrl"` 22 | Country string `json:"country"` 23 | Currency string `json:"currency"` 24 | DiscCount float64 `json:"discCount"` 25 | DiscNumber float64 `json:"discNumber"` 26 | Kind string `json:"kind"` 27 | PreviewUrl string `json:"previewUrl"` 28 | PrimaryGenreName string `json:"primaryGenreName"` 29 | RadioStationUrl string `json:"radioStationUrl"` 30 | ReleaseDate string `json:"releaseDate"` 31 | TrackCensoredName string `json:"trackCensoredName"` 32 | TrackCount float64 `json:"trackCount"` 33 | TrackExplicitness string `json:"trackExplicitness"` 34 | TrackId float64 `json:"trackId"` 35 | TrackName string `json:"trackName"` 36 | TrackNumber float64 `json:"trackNumber"` 37 | TrackPrice float64 `json:"trackPrice"` 38 | TrackTimeMillis float64 `json:"trackTimeMillis"` 39 | TrackViewUrl string `json:"trackViewUrl"` 40 | WrapperType string `json:"wrapperType"` 41 | } 42 | 43 | type SearchResult struct { 44 | ResultCount int `json:"resultCount"` 45 | Results []SearchMap `json:"results"` 46 | } 47 | 48 | type LookupMap struct { 49 | Advisories []string `json:"advisories"` 50 | AppletvScreenshotUrls []string `json:"appletvScreenshotUrls"` 51 | ArtistId float64 `json:"artistId"` 52 | ArtistName string `json:"artistName"` 53 | ArtistViewUrl string `json:"artistViewUrl"` 54 | ArtworkUrl60 string `json:"artworkUrl60"` 55 | ArtworkUrl100 string `json:"artworkUrl100"` 56 | ArtworkUrl512 string `json:"artworkUrl512"` 57 | BundleId string `json:"bundleId"` 58 | ContentAdvisoryRating string `json:"contentAdvisoryRating"` 59 | Currency string `json:"currency"` 60 | CurrentVersionReleaseDate string `json:"currentVersionReleaseDate"` 61 | Description string `json:"description"` 62 | Features []string `json:"features"` 63 | FileSizeBytes string `json:"fileSizeBytes"` 64 | FormattedPrice string `json:"formattedPrice"` 65 | Genres []string `json:"genres"` 66 | GenreIds []string `json:"genreIds"` 67 | IpadScreenshotUrls []string `json:"ipadScreenshotUrls"` 68 | IsGameCenterEnabled bool `json:"isGameCenterEnabled"` 69 | IsVppDeviceBasedLicensingEnabled bool `json:"isVppDeviceBasedLicensingEnabled"` 70 | Kind string `json:"kind"` 71 | LanguageCodesISO2A []string `json:"languageCodesISO2A"` 72 | MinimumOsVersion string `json:"minimumOsVersion"` 73 | Price float64 `json:"price"` 74 | PrimaryGenreId float64 `json:"primaryGenreId"` 75 | PrimaryGenreName string `json:"primaryGenreName"` 76 | ReleaseDate string `json:"releaseDate"` 77 | ReleaseNotes string `json:"releaseNotes"` 78 | ScreenshotUrls []string `json:"screenshotUrls"` 79 | SellerName string `json:"sellerName"` 80 | SellerUrl string `json:"sellerUrl"` 81 | SupportedDevices []string `json:"supportedDevices"` 82 | TrackCensoredName string `json:"trackCensoredName"` 83 | TrackContentRating string `json:"trackContentRating"` 84 | TrackId float64 `json:"trackId"` 85 | TrackName string `json:"trackName"` 86 | TrackViewUrl string `json:"trackViewUrl"` 87 | Version string `json:"version"` 88 | WrapperType string `json:"wrapperType"` 89 | } 90 | 91 | type LookupResult struct { 92 | ResultCount int `json:"resultCount"` 93 | Results []LookupMap `json:"results"` 94 | } 95 | 96 | func Search(query, country, media string) (*SearchResult, error) { 97 | u := url.Values{} 98 | u["term"] = []string{query} 99 | u["country"] = []string{country} 100 | u["media"] = []string{media} 101 | res, err := http.Get("https://itunes.apple.com/search?" + u.Encode()) 102 | if err != nil { 103 | return nil, err 104 | } 105 | defer res.Body.Close() 106 | ret := SearchResult{} 107 | err = json.NewDecoder(res.Body).Decode(&ret) 108 | if err != nil { 109 | return nil, err 110 | } 111 | return &ret, nil 112 | } 113 | 114 | // New Lookup API 115 | // search_term will be id, amgArtistId, upc, isbn 116 | func Lookup(search_term string, search_term_value string, entity string, limit string, sort string) (*LookupResult, error) { 117 | u := url.Values{} 118 | u[search_term] = []string{search_term_value} 119 | u["entity"] = []string{entity} 120 | u["limit"] = []string{limit} 121 | u["sort"] = []string{sort} 122 | res, err := http.Get("https://itunes.apple.com/lookup?" + u.Encode()) 123 | if err != nil { 124 | return nil, err 125 | } 126 | defer res.Body.Close() 127 | ret := LookupResult{} 128 | err = json.NewDecoder(res.Body).Decode(&ret) 129 | if err != nil { 130 | return nil, err 131 | } 132 | return &ret, nil 133 | } 134 | --------------------------------------------------------------------------------