These models are shared across multiple resource types in the Spotify API. They represent common data structures like URLs, images, pagination, and identifiers.
from spotify_sdk.models.common import ExternalUrlsexternal_urls = ExternalUrls(spotify="https://open.spotify.com/track/abc123")print(f"Open in browser: {external_urls.spotify}")
from spotify_sdk.models.common import Image# Images are typically provided in descending size orderimages = [ Image(url="https://i.scdn.co/image/large.jpg", height=640, width=640), Image(url="https://i.scdn.co/image/medium.jpg", height=300, width=300), Image(url="https://i.scdn.co/image/small.jpg", height=64, width=64),]# Get the largest imagelargest = images[0]print(f"Large image: {largest.url} ({largest.width}x{largest.height})")# Get the smallest image for thumbnailsthumbnail = images[-1]print(f"Thumbnail: {thumbnail.url}")
from spotify_sdk.models.common import Restrictionif track.restrictions: reason = track.restrictions.reason if reason == "market": print("This track is not available in your country") elif reason == "product": print("This track requires Spotify Premium") elif reason == "explicit": print("This track is restricted by explicit content settings")
from spotify_sdk.models.common import ExternalIds# Track with ISRCif track.external_ids.isrc: print(f"ISRC: {track.external_ids.isrc}")# Album with UPC or EANif album.external_ids.upc: print(f"UPC: {album.external_ids.upc}")elif album.external_ids.ean: print(f"EAN: {album.external_ids.ean}")
from spotify_sdk.models.common import Pagefrom spotify_sdk.models.track import Track# Collect all items from multiple pagesall_tracks = []page: Page[Track] = Page[Track](**first_page_response)while True: all_tracks.extend(page.items) if not page.next: break # Fetch next page using page.next URL next_response = api_client.get(page.next) page = Page[Track](**next_response)print(f"Collected {len(all_tracks)} tracks total")
Show Working with Image Sizes
from spotify_sdk.models.common import Imagedef get_image_by_size(images: list[Image], target_size: int = 300) -> Image | None: """Get the image closest to the target size.""" if not images: return None # Filter images with known dimensions sized_images = [img for img in images if img.width and img.height] if not sized_images: return images[0] # Return first image if no dimensions # Find closest to target size return min( sized_images, key=lambda img: abs(img.width - target_size) )# Usagealbum_image = get_image_by_size(album.images, 300)if album_image: print(f"Album art: {album_image.url}")
Show Handling Content Restrictions
from spotify_sdk.models.track import Trackdef can_play_track(track: Track) -> tuple[bool, str]: """Check if a track can be played and why not if it can't.""" if not track.is_playable: if track.restrictions: reason = track.restrictions.reason messages = { "market": "Not available in your country", "product": "Requires Spotify Premium", "explicit": "Blocked by explicit content filter" } return False, messages.get(reason, "Not playable") return False, "Not playable" return True, "Can play"# Usagecan_play, message = can_play_track(track)if not can_play: print(f"⚠ {message}")