高木のブログ

【Spotify Web API】現在再生中の曲を取得する

2023/01/10

Spotify で現在再生中の曲を取得したかった

Spotify は API を公開しているので、Spotify Web API で取得することができる

$ curl -s -X GET https://api.spotify.com/v1/me/player/currently-playing \
-H 'Authorization: Bearer {アクセストークン}' | jq -r .item.name
君はロックを聴かない

アクセストークンを取得する方法

【Spotify Web API】アクセストークンの取得と更新(リフレッシュ) に書いた

API のレスポンス

$ curl -X GET https://api.spotify.com/v1/me/player/currently-playing \
-H 'Authorization: Bearer {アクセストークン}'
{
  "timestamp" : 1673277556270,
  "context" : {
    "external_urls" : {
      "spotify" : "https://open.spotify.com/playlist/37i9dQZF1DWZ7hCgzgU48z"
    },
    "href" : "https://api.spotify.com/v1/playlists/37i9dQZF1DWZ7hCgzgU48z",
    "type" : "playlist",
    "uri" : "spotify:playlist:37i9dQZF1DWZ7hCgzgU48z"
  },
  "progress_ms" : 5344,
  "item" : {
    "album" : {
      "album_type" : "album",
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/5kVZa4lFUmAQlBogl1fkd6"
        },
        "href" : "https://api.spotify.com/v1/artists/5kVZa4lFUmAQlBogl1fkd6",
        "id" : "5kVZa4lFUmAQlBogl1fkd6",
        "name" : "Aimyon",
        "type" : "artist",
        "uri" : "spotify:artist:5kVZa4lFUmAQlBogl1fkd6"
      } ],
      "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
      "external_urls" : {
        "spotify" : "https://open.spotify.com/album/0ct8ESCAYEpDGYJOndCfft"
      },
      "href" : "https://api.spotify.com/v1/albums/0ct8ESCAYEpDGYJOndCfft",
      "id" : "0ct8ESCAYEpDGYJOndCfft",
      "images" : [ {
        "height" : 640,
        "url" : "https://i.scdn.co/image/ab67616d0000b27333c05e1d08fec494a30bd240",
        "width" : 640
      }, {
        "height" : 300,
        "url" : "https://i.scdn.co/image/ab67616d00001e0233c05e1d08fec494a30bd240",
        "width" : 300
      }, {
        "height" : 64,
        "url" : "https://i.scdn.co/image/ab67616d0000485133c05e1d08fec494a30bd240",
        "width" : 64
      } ],
      "name" : "青春のエキサイトメント",
      "release_date" : "2017-09-12",
      "release_date_precision" : "day",
      "total_tracks" : 11,
      "type" : "album",
      "uri" : "spotify:album:0ct8ESCAYEpDGYJOndCfft"
    },
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/5kVZa4lFUmAQlBogl1fkd6"
      },
      "href" : "https://api.spotify.com/v1/artists/5kVZa4lFUmAQlBogl1fkd6",
      "id" : "5kVZa4lFUmAQlBogl1fkd6",
      "name" : "Aimyon",
      "type" : "artist",
      "uri" : "spotify:artist:5kVZa4lFUmAQlBogl1fkd6"
    } ],
    "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
    "disc_number" : 1,
    "duration_ms" : 246306,
    "explicit" : false,
    "external_ids" : {
      "isrc" : "JPWP01771557"
    },
    "external_urls" : {
      "spotify" : "https://open.spotify.com/track/59eluCMn0XbOWqeWQ91FTM"
    },
    "href" : "https://api.spotify.com/v1/tracks/59eluCMn0XbOWqeWQ91FTM",
    "id" : "59eluCMn0XbOWqeWQ91FTM",
    "is_local" : false,
    "name" : "君はロックを聴かない",
    "popularity" : 68,
    "preview_url" : "https://p.scdn.co/mp3-preview/7e26c8a2bd4b3fa7207d78baf796dd28994a8547?cid=1f2f34c40ba8411bbe57cdc7bd3f3632",
    "track_number" : 3,
    "type" : "track",
    "uri" : "spotify:track:59eluCMn0XbOWqeWQ91FTM"
  },
  "currently_playing_type" : "track",
  "actions" : {
    "disallows" : {
      "resuming" : true
    }
  },
  "is_playing" : true
}

SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub