57 lines
1.7 KiB
Swift
57 lines
1.7 KiB
Swift
import Foundation
|
|
import MusicBrainz
|
|
|
|
let client = MusicBrainzClient(
|
|
clientAgent: .init(appName: "MusicD", version: "0.0.1", email: "test@gmail.com"))
|
|
|
|
do {
|
|
print("--- Artist Search: 'Michael Jackson' ---")
|
|
let artistSearch = try await client.search(
|
|
.artist(name: "Michael Jackson", gender: .male, country: .us, type: .person), limit: 1)
|
|
|
|
if let artist = artistSearch.entities.first {
|
|
print("- Found Artist: \(artist.name) (\(artist.id))")
|
|
|
|
// Fetch artist with URL relationships and releases
|
|
print(" Fetching artist details (with url-rels + releases)...")
|
|
let detailedArtist = try await client.fetch(
|
|
.artist, id: artist.id, includes: ["url-rels", "releases"])
|
|
|
|
if let imageURL = detailedArtist.imageURL {
|
|
print(" Artist Image Shortcut: \(imageURL)")
|
|
}
|
|
|
|
if let releases = detailedArtist.releases {
|
|
print(" Releases count: \(releases.count)")
|
|
for release in releases.prefix(5) {
|
|
print(" - \(release.title) (\(release.id))")
|
|
}
|
|
}
|
|
|
|
// Fetch tracks for the first release
|
|
if let release = detailedArtist.releases?.first {
|
|
print("\n--- Fetching tracks for release: '\(release.title)' ---")
|
|
let detailedRelease = try await client.fetch(
|
|
.release, id: release.id, includes: ["recordings"])
|
|
if let media = detailedRelease.media {
|
|
for medium in media {
|
|
print(
|
|
" Medium \(medium.position) (\(medium.format?.rawValue ?? "Unknown format")):"
|
|
)
|
|
if let tracks = medium.tracks {
|
|
for track in tracks {
|
|
print(" \(track.number). \(track.title)")
|
|
if let youtubeURL = track.recording?.youtubeURL {
|
|
print(" YouTube: \(youtubeURL)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch {
|
|
print("Error: \(error)")
|
|
}
|