TVDB movie metadata search
This commit is contained in:
3 files changed
+230
-24
No files matched your search
@@ -131,7 +131,7 @@ public enum MetadataSearch {
|
||||
|
||||
extension MetadataSearch {
|
||||
|
||||
public static var movieProviders: [String] { get { return [AppleTV().name, TheMovieDB().name, iTunesStore().name] } }
|
||||
public static var movieProviders: [String] { get { return [AppleTV().name, TheMovieDB().name, TheTVDB().name, iTunesStore().name] } }
|
||||
public static var tvProviders: [String] { get { return [AppleTV().name, TheMovieDB().name, TheTVDB().name, iTunesStore().name] } }
|
||||
|
||||
public static func service(name: String?) -> MetadataService {
|
||||
|
||||
@@ -49,7 +49,7 @@ public struct TheTVDB : MetadataService {
|
||||
|
||||
// MARK: - TV Series ID search
|
||||
|
||||
private func match(series: TVDBSeriesSearchResult, name: String) -> Bool {
|
||||
private func match(series: TVDBSearchResult, name: String) -> Bool {
|
||||
if let seriesName = series.name, seriesName.caseInsensitiveCompare(name) == .orderedSame {
|
||||
return true
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public struct TheTVDB : MetadataService {
|
||||
let sorted = series.sorted { el1, el2 -> Bool in
|
||||
return el1.name?.caseInsensitiveCompare(seriesName) == .orderedSame ? true : false
|
||||
}
|
||||
let filteredSeries = sorted.filter { $0.status.isEmpty == false && match(series: $0, name: seriesName) }.map { $0.tvdb_id }
|
||||
let filteredSeries = sorted.filter { $0.status?.isEmpty == false && match(series: $0, name: seriesName) }.map { $0.tvdb_id }
|
||||
|
||||
if filteredSeries.isEmpty == false {
|
||||
return filteredSeries
|
||||
@@ -94,13 +94,19 @@ public struct TheTVDB : MetadataService {
|
||||
return genres.map {$0.name }.reduce("", { $0 + ($0.isEmpty ? "" : ", ") + $1 })
|
||||
}
|
||||
|
||||
private func cleanList(ratings: [TVDBContentRating]) -> String? {
|
||||
private func cleanList(ratings: [TVDBContentRating], mediaKind: MediaKind) -> String? {
|
||||
if let rating = ratings.filter({ $0.country == "usa" }).first {
|
||||
return Ratings.shared.rating(countryCode: "USA", mediaKind: .tvShow, name: rating.name)?.iTunesCode
|
||||
return Ratings.shared.rating(countryCode: "USA", mediaKind: mediaKind, name: rating.name)?.iTunesCode
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func cleanList(companies: [TVDBCompany]) -> String {
|
||||
return companies
|
||||
.map { $0.name }
|
||||
.reduce("", { $0 + ($0.isEmpty ? "" : ", ") + $1 })
|
||||
}
|
||||
|
||||
private func areInIncreasingOrder(ep1: MetadataResult, ep2: MetadataResult) -> Bool {
|
||||
guard let v1 = ep1[.episodeNumber] as? Int,
|
||||
let v2 = ep2[.episodeNumber] as? Int,
|
||||
@@ -116,11 +122,56 @@ public struct TheTVDB : MetadataService {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func cleanArtworks(_ info: TVDBSeriesExtendedRecord) -> [Artwork] {
|
||||
fileprivate func cleanArtworks(_ records: [TVDBArtworBaseRecord]) -> [Artwork] {
|
||||
var artworks: [Artwork] = []
|
||||
let artworkTypes = session.fetchArtworkTypes()
|
||||
|
||||
for artwork in info.artworks {
|
||||
for artwork in records {
|
||||
let url = URL(string: artwork.image)
|
||||
let thumbnailURL = URL(string: artwork.thumbnail)
|
||||
|
||||
var type: ArtworkType?
|
||||
var size: ArtworkSize = .default
|
||||
|
||||
let artworkType = artworkTypes.first(where: { $0.id == artwork.type })
|
||||
switch (artworkType?.name, artworkType?.recordType) {
|
||||
case ("Poster", "movie"):
|
||||
type = .poster
|
||||
case ("Background", "movie"):
|
||||
type = .backdrop
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
switch (artworkType?.width ?? 1, artworkType?.height ?? 1) {
|
||||
case (let width, let height) where width == height:
|
||||
size = .square
|
||||
case (let width, let height) where width >= height:
|
||||
size = .rectangle
|
||||
case (let width, let height) where height < width:
|
||||
size = .default
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if let url, let thumbnailURL, let type {
|
||||
let entry = Artwork(url: url,
|
||||
thumbURL: thumbnailURL,
|
||||
service: self.name,
|
||||
type: type,
|
||||
size: size)
|
||||
artworks.append(entry)
|
||||
}
|
||||
}
|
||||
|
||||
return artworks
|
||||
}
|
||||
|
||||
fileprivate func cleanArtworks(_ records: [TVDBArtworkExtendedRecord]) -> [Artwork] {
|
||||
var artworks: [Artwork] = []
|
||||
let artworkTypes = session.fetchArtworkTypes()
|
||||
|
||||
for artwork in records {
|
||||
let url = URL(string: artwork.image)
|
||||
let thumbnailURL = URL(string: artwork.thumbnail)
|
||||
|
||||
@@ -175,8 +226,8 @@ public struct TheTVDB : MetadataService {
|
||||
result[.seriesDescription] = (translation?.overview ?? info.overview)?.trimmingWhitespacesAndNewlinews()
|
||||
result[.genre] = cleanList(genres: info.genres)
|
||||
result[.network] = info.originalNetwork?.name
|
||||
result[.rating] = cleanList(ratings: info.contentRatings)
|
||||
result[.cast] = cleanList(characters: info.characters ?? [], type: "Actor")
|
||||
result[.rating] = cleanList(ratings: info.contentRatings, mediaKind: .tvShow)
|
||||
result[.cast] = cleanList(characters: info.characters ?? [], type: "Actor")
|
||||
|
||||
// Episode Info
|
||||
result[.serviceEpisodeID] = episode.id
|
||||
@@ -188,7 +239,7 @@ public struct TheTVDB : MetadataService {
|
||||
result[.episodeNumber] = episode.number
|
||||
result[.trackNumber] = episode.number
|
||||
|
||||
result.remoteArtworks += cleanArtworks(info)
|
||||
result.remoteArtworks += cleanArtworks(info.artworks)
|
||||
|
||||
// TheTVDB does not provide the following fields normally associated with TV shows in SBMetadataResult:
|
||||
// "Copyright", "Comments", "Producers", "Artist"
|
||||
@@ -380,13 +431,71 @@ public struct TheTVDB : MetadataService {
|
||||
return metadata
|
||||
}
|
||||
|
||||
// MARK: - Unimplemented movie search
|
||||
// MARK: - Movie search
|
||||
|
||||
private func metadata(forMoviePartialResult result: TVDBSearchResult, language: String?) -> MetadataResult {
|
||||
let metadata = MetadataResult()
|
||||
|
||||
metadata.mediaKind = .movie
|
||||
|
||||
metadata[.serviceContentID] = result.tvdb_id
|
||||
metadata[.name] = result.translations?[language ?? defaultLanguage] ?? result.name
|
||||
metadata[.releaseDate] = result.first_air_time
|
||||
metadata[.longDescription] = (result.overviews?[language ?? defaultLanguage] ?? result.overview)?.trimmingWhitespacesAndNewlinews()
|
||||
metadata[.studio] = result.studios?.first
|
||||
|
||||
return metadata
|
||||
}
|
||||
|
||||
public func search(movie: String, language: String) -> [MetadataResult] {
|
||||
return []
|
||||
let results = session.fetch(movie: movie)
|
||||
return results.map { metadata(forMoviePartialResult: $0, language: language) }
|
||||
}
|
||||
|
||||
public func loadMovieMetadata(_ metadata: MetadataResult, language: String) -> MetadataResult {
|
||||
guard let movieID = metadata[.serviceContentID] as? String,
|
||||
let info = session.fetch(movieInfo: movieID)
|
||||
else { return metadata }
|
||||
|
||||
metadata[.releaseDate] = info.first_release?.date
|
||||
metadata[.rating] = cleanList(ratings: info.contentRatings ?? [], mediaKind: .movie)
|
||||
metadata[.genre] = cleanList(genres: info.genres)
|
||||
metadata[.studio] = cleanList(companies: info.companies?.studio ?? [])
|
||||
metadata[.cast] = cleanList(characters: info.characters ?? [], type: "Actor")
|
||||
metadata[.director] = cleanList(characters: info.characters ?? [], type: "Director")
|
||||
metadata[.screenwriters] = cleanList(characters: info.characters ?? [], type: "Writer")
|
||||
metadata[.producers] = cleanList(characters: info.characters ?? [], type: "Producer")
|
||||
metadata[.executiveProducer] = cleanList(characters: info.characters ?? [], type: "Executive Producer")
|
||||
metadata[.composer] = cleanList(characters: info.characters ?? [], type: "Original Music Composer")
|
||||
|
||||
metadata[.serviceContentID] = info.id
|
||||
|
||||
var artworks: [Artwork] = []
|
||||
artworks += cleanArtworks(info.artworks)
|
||||
|
||||
var iTunesImage = [Artwork](), appleTV = [Artwork]()
|
||||
let group = DispatchGroup()
|
||||
let queue = DispatchQueue.global()
|
||||
|
||||
queue.async(group: group) {
|
||||
// add iTunes artwork
|
||||
if let iTunesMetadata = iTunesStore.quickiTunesSearch(movieName: info.name) {
|
||||
iTunesImage = iTunesMetadata.remoteArtworks
|
||||
}
|
||||
}
|
||||
|
||||
queue.async(group: group) {
|
||||
if let store = iTunesStore.Store(language: "USA (English)") {
|
||||
appleTV = AppleTV().searchArtwork(term: info.name, store: store, type: .movie)
|
||||
}
|
||||
}
|
||||
group.wait()
|
||||
|
||||
artworks.insert(contentsOf: iTunesImage, at: 0)
|
||||
artworks.insert(contentsOf: appleTV, at: 0)
|
||||
|
||||
metadata.remoteArtworks = artworks
|
||||
|
||||
return metadata
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,18 @@ public class TVDBArtworkStatus : Codable {
|
||||
let name: String?
|
||||
}
|
||||
|
||||
public class TVDBArtworBaseRecord : Codable {
|
||||
public let height: Int64
|
||||
public let id: Int64
|
||||
public let image: String
|
||||
public let includesText: Bool
|
||||
public let language: String?
|
||||
public let score: Double?
|
||||
public let thumbnail: String
|
||||
public let type: Int64
|
||||
public let width: Int64
|
||||
}
|
||||
|
||||
public class TVDBArtworkExtendedRecord : Codable {
|
||||
public let episodeId: Int?
|
||||
public let height: Int64
|
||||
@@ -79,7 +91,7 @@ public struct TVDBCharacter : Codable {
|
||||
public let peopleId: Int
|
||||
public let personImgURL: String?
|
||||
public let peopleType: String
|
||||
public let seriesId: Int
|
||||
public let seriesId: Int?
|
||||
// public let series: TVDBRecordInfo
|
||||
public let sort: Int
|
||||
// public let tagOptions: TVDBTagOptions
|
||||
@@ -95,19 +107,33 @@ public struct TVDBParentCompany : Codable {
|
||||
|
||||
public struct TVDBCompany : Codable {
|
||||
public let activeDate: String?
|
||||
public let aliases: [TVDBAlias]
|
||||
public let aliases: [TVDBAlias]?
|
||||
public let country: String?
|
||||
public let id: Int64
|
||||
public let inactiveDate: String?
|
||||
public let name: String
|
||||
public let nameTranslations: [String]
|
||||
public let overviewTranslations: [String]
|
||||
public let nameTranslations: [String]?
|
||||
public let overviewTranslations: [String]?
|
||||
public let primaryCompanyType: Int64
|
||||
public let slug: String
|
||||
public let parentCompany: TVDBParentCompany?
|
||||
// public let tagOptions: TVDBTagOptions
|
||||
}
|
||||
|
||||
public struct TVDBCompanies : Codable {
|
||||
public let studio: [TVDBCompany]?
|
||||
public let network: [TVDBCompany]?
|
||||
public let production: [TVDBCompany]?
|
||||
public let distributor: [TVDBCompany]?
|
||||
public let special_effects: [TVDBCompany]?
|
||||
}
|
||||
|
||||
public struct TVDBStudioBaseRecord : Codable {
|
||||
public let id: Int64
|
||||
public let name: String
|
||||
public let parentStudio: Int?
|
||||
}
|
||||
|
||||
public struct TVDBContentRating : Codable {
|
||||
public let id: Int64
|
||||
public let name: String
|
||||
@@ -140,6 +166,12 @@ public struct TVDBTranslationExtended : Codable {
|
||||
public let alias: [String]?
|
||||
}
|
||||
|
||||
public struct TVDBRelease : Codable {
|
||||
public let country: String
|
||||
public let date: String
|
||||
public let detail: String?
|
||||
}
|
||||
|
||||
public class TVDBStatus : Codable {
|
||||
public let id: Int64?
|
||||
public let keepUpdated: Bool
|
||||
@@ -147,7 +179,7 @@ public class TVDBStatus : Codable {
|
||||
public let recordType: String
|
||||
}
|
||||
|
||||
public struct TVDBSeriesSearchResult : Codable {
|
||||
public struct TVDBSearchResult : Codable {
|
||||
public let aliases: [String]?
|
||||
public let companies: [String]?
|
||||
public let companyType: String?
|
||||
@@ -169,7 +201,7 @@ public struct TVDBSeriesSearchResult : Codable {
|
||||
public let poster: String?
|
||||
public let posters: [String]?
|
||||
public let primary_language: String?
|
||||
public let status: String
|
||||
public let status: String?
|
||||
public let slug: String?
|
||||
public let studios: [String]?
|
||||
public let title: String?
|
||||
@@ -181,6 +213,44 @@ public struct TVDBSeriesSearchResult : Codable {
|
||||
public let year: String?
|
||||
}
|
||||
|
||||
public struct TVDBMovieExtendedRecord : Codable {
|
||||
public let aliases: [TVDBAlias]?
|
||||
public let artworks: [TVDBArtworBaseRecord]
|
||||
public let audioLanguages: [String]?
|
||||
// public let awards: [TVDBAwardBaseRecord]
|
||||
public let boxOffice: String?
|
||||
public let boxOfficeUS: String?
|
||||
public let budget: String?
|
||||
public let characters: [TVDBCharacter]?
|
||||
public let companies: TVDBCompanies?
|
||||
public let contentRatings: [TVDBContentRating]?
|
||||
public let first_release: TVDBRelease?
|
||||
public let genres: [TVDBGenreBaseRecord]
|
||||
public let id: Int64
|
||||
public let image: String?
|
||||
// public let inspirations: [TVDBInspiration]?
|
||||
public let lastUpdated: String
|
||||
// public let lists: TVDBList
|
||||
public let name: String
|
||||
public let nameTranslations: [String]
|
||||
public let originalCountry: String?
|
||||
public let originalLanguage: String?
|
||||
public let overviewTranslations: [String]?
|
||||
// public let production_countries: [TVDBProductionCountry]?
|
||||
public let releases: [TVDBRelease]?
|
||||
public let remoteIds: [TVDBRemoteId]
|
||||
public let runtime: Int?
|
||||
public let score: Double?
|
||||
public let slug: String?
|
||||
public let spoken_languages: [String]?
|
||||
public let studios: [TVDBStudioBaseRecord]?
|
||||
public let subtitleLanguages: [String]?
|
||||
// public let tagOptions: [TVDBTagOption]
|
||||
// public let trailers: [TVDBTrailer]
|
||||
public let translations: TVDBTranslationExtended?
|
||||
public let year: String
|
||||
}
|
||||
|
||||
public struct TVDBEpisodeBaseRecord : Codable {
|
||||
public let absoluteNumber: Double?
|
||||
public let aired: String?
|
||||
@@ -200,7 +270,7 @@ public struct TVDBEpisodeBaseRecord : Codable {
|
||||
public let runtime: Int?
|
||||
public let seasonNumber: Int
|
||||
// public let seasons: []?
|
||||
public let seriesId: Int
|
||||
public let seriesId: Int?
|
||||
public let year: String?
|
||||
}
|
||||
|
||||
@@ -211,7 +281,7 @@ public struct TVDBEpisodeExtendedRecord : Codable {
|
||||
public let airsBeforeSeason: Int?
|
||||
// public let awards: [TVDBAwardBaseRecord]
|
||||
public let characters: [TVDBCharacter]?
|
||||
public let companies: [TVDBCompany]
|
||||
public let companies: [TVDBCompany]?
|
||||
public let contentRatings: [TVDBContentRating]
|
||||
public let finaleType: String?
|
||||
public let id: Int64
|
||||
@@ -285,7 +355,7 @@ public struct TVDBSeriesExtendedRecord : Codable {
|
||||
public let lastUpdated: String
|
||||
public let name: String
|
||||
public let nameTranslations: [String]
|
||||
public let companies: [TVDBCompany]
|
||||
public let companies: [TVDBCompany]?
|
||||
public let nextAired: String
|
||||
public let originalCountry: String
|
||||
public let originalLanguage: String
|
||||
@@ -469,13 +539,40 @@ final public class TheTVDBService {
|
||||
}
|
||||
|
||||
// MARK: - Service calls
|
||||
|
||||
public func fetch(series: String) -> [TVDBSeriesSearchResult] {
|
||||
|
||||
public func fetch(movie: String) -> [TVDBSearchResult] {
|
||||
// Remove + because it breaks search
|
||||
let encodedName = movie.replacingOccurrences(of: "+", with: "").replacingOccurrences(of: "*", with: "-").urlEncoded()
|
||||
|
||||
guard let url = URL(string: "\(TheTVDBService.basePath)search?query=\(encodedName)&type=movie"),
|
||||
let result = sendJSONRequest(url: url, type: Wrapper<[TVDBSearchResult]>.self)
|
||||
else { return [] }
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
public func fetch(movieInfo movieID: String) -> TVDBMovieExtendedRecord? {
|
||||
guard let url = URL(string: "\(TheTVDBService.basePath)movies/\(movieID)/extended"),
|
||||
let result = sendJSONRequest(url: url, type: Wrapper<TVDBMovieExtendedRecord>.self)
|
||||
else { return nil }
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
public func fetch(movieTranslation movieID: String, language: String) -> TVDBTranslation? {
|
||||
guard let url = URL(string: "\(TheTVDBService.basePath)movies/\(movieID)/translations/\(language)"),
|
||||
let result = sendJSONRequest(url: url, type: Wrapper<TVDBTranslation>.self)
|
||||
else { return nil }
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
public func fetch(series: String) -> [TVDBSearchResult] {
|
||||
// Remove + because it breaks search
|
||||
let encodedName = series.replacingOccurrences(of: "+", with: "").replacingOccurrences(of: "*", with: "-").urlEncoded()
|
||||
|
||||
guard let url = URL(string: "\(TheTVDBService.basePath)search?query=\(encodedName)&type=series"),
|
||||
let result = sendJSONRequest(url: url, type: Wrapper<[TVDBSeriesSearchResult]>.self)
|
||||
let result = sendJSONRequest(url: url, type: Wrapper<[TVDBSearchResult]>.self)
|
||||
else { return [] }
|
||||
|
||||
return result.data
|
||||
|
||||
Reference in new issue
Block a user