53 lines
1.4 KiB
Swift
53 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
public struct Artist: MusicBrainzSearchable, Identifiable, Hashable, Equatable {
|
|
public static let entityType: MusicBrainzEntity = .artist
|
|
|
|
public let id: String
|
|
public let name: String
|
|
public let sortName: String?
|
|
public let type: String?
|
|
public let country: String?
|
|
public let disambiguation: String?
|
|
public let lifeSpan: LifeSpan?
|
|
public let relations: [Relation]?
|
|
public let releases: [Release]?
|
|
public let releaseGroups: [ReleaseGroup]?
|
|
public let score: Int?
|
|
|
|
enum CodingKeys: String, CodingKey, Hashable, Equatable {
|
|
case id
|
|
case name
|
|
case sortName = "sort-name"
|
|
case type
|
|
case country
|
|
case disambiguation
|
|
case lifeSpan = "life-span"
|
|
case relations
|
|
case releases
|
|
case releaseGroups = "release-groups"
|
|
case score
|
|
}
|
|
|
|
public var imageURL: URL? {
|
|
guard let relations else { return nil }
|
|
// Look for 'image' or 'wikimedia commons'
|
|
for rel in relations {
|
|
if rel.type == "image" || rel.type == "wikimedia commons" {
|
|
if let resource = rel.url?.resource {
|
|
var urlString = resource
|
|
// Handle Wikimedia Commons page URLs by converting to Special:FilePath
|
|
if urlString.contains("commons.wikimedia.org/wiki/File:") {
|
|
let filename = urlString.components(separatedBy: "/wiki/File:").last ?? ""
|
|
if !filename.isEmpty {
|
|
urlString = "https://commons.wikimedia.org/wiki/Special:FilePath/\(filename)"
|
|
}
|
|
}
|
|
return URL(string: urlString)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|