pepabo-mobile-app-training / spotter

こうめい、えんどぅーチームです。
0 stars 0 forks source link

SpotifyのAPIを叩いた際の取得データをテーブルビューで表示する。 #40

Open Fendo181 opened 7 years ago

Fendo181 commented 7 years ago

困っていること

サンプル

``` { "href" : "https://api.spotify.com/v1/users/spotify_espa%C3%B1a/playlists/21THa8j9TaSGuXYNBU5tsC/tracks", "items" : [ { "added_at" : "2016-10-11T13:44:40Z", "added_by" : { "external_urls" : { "spotify" : "http://open.spotify.com/user/spotify_espa%C3%B1a" }, "href" : "https://api.spotify.com/v1/users/spotify_espa%C3%B1a", "id" : "spotify_españa", "type" : "user", "uri" : "spotify:user:spotify_espa%C3%B1a" }, "is_local" : false, "track" : { "album" : { "album_type" : "single", "artists" : [ { "external_urls" : { "spotify" : "https://open.spotify.com/artist/21451j1KhjAiaYKflxBjr1" }, "href" : "https://api.spotify.com/v1/artists/21451j1KhjAiaYKflxBjr1", "id" : "21451j1KhjAiaYKflxBjr1", "name" : "Zion & Lennox", "type" : "artist", "uri" : "spotify:artist:21451j1KhjAiaYKflxBjr1" } ], "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "UY" ], "external_urls" : { "spotify" : "https://open.spotify.com/album/5GjKG3Y8OvSVJO55dQTFyD" }, "href" : "https://api.spotify.com/v1/albums/5GjKG3Y8OvSVJO55dQTFyD", "id" : "5GjKG3Y8OvSVJO55dQTFyD", "images" : [ { "height" : 640, "url" : "https://i.scdn.co/image/b16064142fcd2bd318b08aab0b93b46e87b1ebf5", "width" : 640 }, { "height" : 300, "url" : "https://i.scdn.co/image/9f05124de35d807b78563ea2ca69550325081747", "width" : 300 }, { "height" : 64, "url" : "https://i.scdn.co/image/863c805b580a29c184fc447327e28af5dac9490b", "width" : 64 } ], "name" : "Otra Vez (feat. J Balvin)", "type" : "album", "uri" : "spotify:album:5GjKG3Y8OvSVJO55dQTFyD" }, "artists" : [ { "external_urls" : { "spotify" : "https://open.spotify.com/artist/21451j1KhjAiaYKflxBjr1" }, "href" : "https://api.spotify.com/v1/artists/21451j1KhjAiaYKflxBjr1", "id" : "21451j1KhjAiaYKflxBjr1", "name" : "Zion & Lennox", "type" : "artist", "uri" : "spotify:artist:21451j1KhjAiaYKflxBjr1" }, { "external_urls" : { "spotify" : "https://open.spotify.com/artist/1vyhD5VmyZ7KMfW5gqLgo5" }, "href" : "https://api.spotify.com/v1/artists/1vyhD5VmyZ7KMfW5gqLgo5", "id" : "1vyhD5VmyZ7KMfW5gqLgo5", "name" : "J Balvin", "type" : "artist", "uri" : "spotify:artist:1vyhD5VmyZ7KMfW5gqLgo5" } ], "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "UY" ], "disc_number" : 1, "duration_ms" : 209453, "explicit" : false, "external_ids" : { "isrc" : "USWL11600423" }, "external_urls" : { "spotify" : "https://open.spotify.com/track/7pk3EpFtmsOdj8iUhjmeCM" }, "href" : "https://api.spotify.com/v1/tracks/7pk3EpFtmsOdj8iUhjmeCM", "id" : "7pk3EpFtmsOdj8iUhjmeCM", "name" : "Otra Vez (feat. J Balvin)", "popularity" : 85, "preview_url" : "https://p.scdn.co/mp3-preview/79c8c9edc4f1ced9dbc368f24374421ed0a33005", "track_number" : 1, "type" : "track", "uri" : "spotify:track:7pk3EpFtmsOdj8iUhjmeCM" } }, { "added_at" : "2016-10-11T13:44:40Z", "added_by" : { "external_urls" : { "spotify" : "http://open.spotify.com/user/spotify_espa%C3%B1a" }, "href" : "https://api.spotify.com/v1/users/spotify_espa%C3%B1a", "id" : "spotify_españa", "type" : "user", "uri" : "spotify:user:spotify_espa%C3%B1a" }, "is_local" : false, ... } ], "limit" : 100, "next" : null, "offset" : 0, "previous" : null, "total" : 58 } ```

ゴール

相談したいこと

(ゴールにいたるために、悩んでいること、決めて欲しいこと、助言してもらいたいことを書きます。)

関係者

@Komei22

Fendo181 commented 7 years ago

SwiftJSONを使って以下のように取得する事ができる事がわかった。

let track_name = json["items"][0]["track"]["name"]
let album_name = json["items"][0]["track"]["album"]["name"]
let imgURL     = json["items"][0]["track"]["album"]["images"][1]["url"]
let track_url  = json["items"][0]["track"]["preview_url"]
Fendo181 commented 6 years ago

SpotifyPlayモデルを作って最終的にはこんな感じに取得するようにした。

 static func fetchPlaylist(handler: @escaping ((spotifyPlaylist) -> Void)) {
    spotifyAPIRequest(OathToken: OathToken!, playlistID: playlistID!){ json in
        let tracks =  spotifyPlaylist(
            track_name: json["items"][0]["track"]["name"].stringValue,
            album_name: json["items"][0]["track"]["album"]["name"].stringValue,
            imgURL: (json["items"][0]["track"]["album"]["images"][1]["url"].url)!,
            track_url: (json["items"][0]["track"]["preview_url"].url)!
        )
        return handler(tracks)
    }
}