From 4cccffedd01660d9518d9805d810560fc2dc8224 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 4 Feb 2020 17:19:16 +0100 Subject: [PATCH] Improve AppleTV ratings. --- Classes/MetadataImporters/AppleTV.swift | 24 +++++++++--------------- Ratings.json | 22 ++++++++++++++++++++++ Ratings.swift | 9 +++++++-- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Classes/MetadataImporters/AppleTV.swift b/Classes/MetadataImporters/AppleTV.swift index 88ff986..853cc91 100644 --- a/Classes/MetadataImporters/AppleTV.swift +++ b/Classes/MetadataImporters/AppleTV.swift @@ -17,7 +17,7 @@ private let formatter = { () -> DateFormatter in }() private extension MetadataResult { - convenience init(item: AppleTV.Item) { + convenience init(item: AppleTV.Item, store: iTunesStore.Store) { self.init() self.mediaKind = .movie @@ -35,7 +35,7 @@ private extension MetadataResult { self.remoteArtworks = [item.images.coverArt16X9, item.images.coverArt].compactMap { $0?.artwork(type: .poster) } } - convenience init(item: AppleTV.Item, episode: AppleTV.Episode) { + convenience init(item: AppleTV.Item, episode: AppleTV.Episode, store: iTunesStore.Store) { self.init() self.mediaKind = .tvShow @@ -55,7 +55,9 @@ private extension MetadataResult { self[.episodeNumber] = episode.episodeNumber self[.trackNumber] = episode.episodeNumber - self[.rating] = episode.rating?.formatted + if let ratingCode = episode.rating?.displayName { + self[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "TV", code: ratingCode)?.iTunesCode + } self[.iTunesURL] = episode.showURL self[.serviceSeriesID] = episode.showID @@ -142,7 +144,7 @@ public struct AppleTV: MetadataService { let length = seasons[seasonNumber - 1].episodeCount let episodes = fetchEpisodes(id: tvShow.id, store: store, range: (startIndex, length)) .filter { episode != nil ? $0.episodeNumber == episode : true } - let results = episodes.map { MetadataResult(item: tvShow, episode: $0) } + let results = episodes.map { MetadataResult(item: tvShow, episode: $0, store: store) } return results } } @@ -161,9 +163,8 @@ public struct AppleTV: MetadataService { metadata[.studio] = content.studio if metadata[.rating] == nil { - metadata[.rating] = content.rating.formatted + metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "TV", code: content.rating.displayName)?.iTunesCode } - metadata[.cast] = details.roles.filter { $0.type == "Actor" }.map { $0.personName }.joined(separator: ", ") + details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ") metadata[.screenwriters] = details.roles.filter { $0.type == "Writer" }.map { $0.personName }.joined(separator: ", ") @@ -185,7 +186,7 @@ public struct AppleTV: MetadataService { guard let store = iTunesStore.Store(language: language) else { return [] } let items = search(term: movie, store: store) - let results = items.map { MetadataResult(item: $0) } + let results = items.map { MetadataResult(item: $0, store: store) } return results } @@ -199,7 +200,7 @@ public struct AppleTV: MetadataService { metadata[.genre] = content.genres.map { $0.name }.joined(separator: ", ") metadata[.studio] = content.studio - metadata[.rating] = content.rating.formatted + metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "movie", code: content.rating.displayName)?.iTunesCode metadata[.cast] = details.roles.filter { $0.type == "Actor" }.map { $0.personName }.joined(separator: ", ") + details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ") @@ -388,13 +389,6 @@ public struct AppleTV: MetadataService { let name: String let system: String let value: UInt - - var formatted: String { - get { - let formattedSystem = system.replacingOccurrences(of: "_", with: "-").replacingOccurrences(of: "movies", with: "movie") - return "\(formattedSystem)|\(displayName)|\(value)|" - } - } } private struct Genre: Codable { diff --git a/Ratings.json b/Ratings.json index 49a4eb7..20f8b7b 100644 --- a/Ratings.json +++ b/Ratings.json @@ -1,6 +1,7 @@ [ { "country" : "USA", + "storeCode" : 143441, "ratings" : [ { "media" : "movie", "prefix" : "mpaa", "itunes-code" : "NR", "itunes-value" : "000", "description" : "NR" }, { "media" : "movie", "prefix" : "mpaa", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, @@ -20,6 +21,7 @@ }, { "country" : "Australia", + "storeCode" : 143460, "ratings" : [ { "media" : "movie", "prefix" : "au-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, { "media" : "movie", "prefix" : "au-movie", "itunes-code" : "PG", "itunes-value" : "200", "description" : "PG" }, @@ -39,6 +41,7 @@ }, { "country" : "Brasil", + "storeCode" : 143503, "ratings" : [ { "media" : "movie", "prefix" : "br-movie", "itunes-code" : "L", "itunes-value" : "000", "description" : "L" }, { "media" : "movie", "prefix" : "br-movie", "itunes-code" : "12", "itunes-value" : "100", "description" : "12" }, @@ -49,6 +52,7 @@ }, { "country" : "Canada", + "storeCode" : 143455, "ratings" : [ { "media" : "movie", "prefix" : "ca-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, { "media" : "movie", "prefix" : "ca-movie", "itunes-code" : "PG", "itunes-value" : "200", "description" : "PG" }, @@ -70,6 +74,7 @@ }, { "country" : "Deutschland", + "storeCode" : 143443, "ratings" : [ { "media" : "movie", "prefix" : "de-movie", "itunes-code" : "Ab 0 Jahren", "itunes-value" : "075", "description" : "Ab 0 Jahren" }, { "media" : "movie", "prefix" : "de-movie", "itunes-code" : "Ab 6 Jahren", "itunes-value" : "100", "description" : "Ab 6 Jahren" }, @@ -85,6 +90,7 @@ }, { "country" : "France", + "storeCode" : 143442, "ratings" : [ { "media" : "movie & TV", "prefix" : "fr-movie", "itunes-code" : "Tout public", "itunes-value" : "000", "description" : "Tout public" }, { "media" : "movie & TV", "prefix" : "fr-movie", "itunes-code" : "-10", "itunes-value" : "100", "description" : "-10" }, @@ -96,6 +102,7 @@ }, { "country" : "Italia", + "storeCode" : 143450, "ratings" : [ { "media" : "movie", "prefix" : "it-movie", "itunes-code" : "T", "itunes-value" : "100", "description" : "T" }, { "media" : "movie", "prefix" : "it-movie", "itunes-code" : "VPA", "itunes-value" : "125", "description" : "VPA" }, @@ -107,6 +114,7 @@ }, { "country" : "Ireland", + "storeCode" : 143449, "ratings" : [ { "media" : "movie", "prefix" : "ie-movie", "itunes-code" : "Not Rated", "itunes-value" : "000", "description" : "Not Rated" }, { "media" : "movie", "prefix" : "ie-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, @@ -125,6 +133,7 @@ }, { "country" : "日本", + "storeCode" : 143462, "ratings" : [ { "media" : "movie", "prefix" : "jp-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, { "media" : "movie", "prefix" : "jp-movie", "itunes-code" : "PG12", "itunes-value" : "200", "description" : "PG12" }, @@ -135,6 +144,7 @@ }, { "country" : "Mexico", + "storeCode" : 143468, "ratings" : [ { "media" : "movie", "prefix" : "mx-movie", "itunes-code" : "AA", "itunes-value" : "000", "description" : "AA" }, { "media" : "movie", "prefix" : "mx-movie", "itunes-code" : "A", "itunes-value" : "100", "description" : "A" }, @@ -145,6 +155,7 @@ }, { "country" : "Nederland", + "storeCode" : 143452, "ratings" : [ { "media" : "movie & TV", "prefix" : "nl-movie", "itunes-code" : "AL/G", "itunes-value" : "000", "description" : "AL/G" }, { "media" : "movie & TV", "prefix" : "nl-movie", "itunes-code" : "6", "itunes-value" : "100", "description" : "6" }, @@ -156,6 +167,7 @@ }, { "country" : "New Zealand", + "storeCode" : 143461, "ratings" : [ { "media" : "movie & TV", "prefix" : "nz-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, { "media" : "movie & TV", "prefix" : "nz-movie", "itunes-code" : "PG", "itunes-value" : "200", "description" : "PG" }, @@ -173,6 +185,7 @@ }, { "country" : "Sverige", + "storeCode" : 143456, "ratings" : [ { "media" : "movie & TV", "prefix" : "se-movie", "itunes-code" : "A", "itunes-value" : "000", "description" : "Barntillåten" }, { "media" : "movie & TV", "prefix" : "se-movie", "itunes-code" : "7", "itunes-value" : "125", "description" : "Från 7 år" }, @@ -184,6 +197,7 @@ }, { "country" : "Schweiz", + "storeCode" : 143459, "ratings" : [ { "media" : "movie", "prefix" : "ch-movie", "itunes-code" : "0", "itunes-value" : "050", "description" : "0" }, { "media" : "movie", "prefix" : "ch-movie", "itunes-code" : "6", "itunes-value" : "075", "description" : "6" }, @@ -198,6 +212,7 @@ }, { "country" : "Suisse", + "storeCode" : 143459, "ratings" : [ { "media" : "movie", "prefix" : "ch-movie", "itunes-code" : "0", "itunes-value" : "050", "description" : "0" }, { "media" : "movie", "prefix" : "ch-movie", "itunes-code" : "6", "itunes-value" : "075", "description" : "6" }, @@ -212,6 +227,7 @@ }, { "country" : "UK", + "storeCode" : 143444, "ratings" : [ { "media" : "movie & TV", "prefix" : "uk-movie", "itunes-code" : "NR", "itunes-value" : "000", "description" : "Not Rated" }, { "media" : "movie & TV", "prefix" : "uk-movie", "itunes-code" : "U", "itunes-value" : "100", "description" : "U" }, @@ -230,6 +246,7 @@ }, { "country" : "Россия", + "storeCode" : 143469, "ratings" : [ { "media" : "movie & TV", "prefix" : "ru-movie", "itunes-code" : "0+", "itunes-value" : "000", "description" : "0+" }, { "media" : "movie & TV", "prefix" : "ru-movie", "itunes-code" : "6+", "itunes-value" : "150", "description" : "6+" }, @@ -241,6 +258,7 @@ }, { "country" : "Pilipinas", + "storeCode" : 143474, "ratings" : [ { "media" : "movie & TV", "prefix" : "ph-movie", "itunes-code" : "G", "itunes-value" : "100", "description" : "G" }, { "media" : "movie & TV", "prefix" : "ph-movie", "itunes-code" : "PG", "itunes-value" : "300", "description" : "PG" }, @@ -252,6 +270,7 @@ }, { "country" : "Hong Kong", + "storeCode" : 143463, "ratings" : [ { "media" : "movie & TV", "prefix" : "hk-movie", "itunes-code" : "第i類", "itunes-value" : "100", "description" : "第i類" }, { "media" : "movie & TV", "prefix" : "hk-movie", "itunes-code" : "第ii類", "itunes-value" : "400", "description" : "第ii類" }, @@ -260,6 +279,7 @@ }, { "country" : "Česká republika", + "storeCode" : 143489, "ratings" : [ { "media" : "movie & TV", "prefix" : "cz-movies", "itunes-code" : "U", "itunes-value" : "100", "description" : "U" }, { "media" : "movie & TV", "prefix" : "cz-movies", "itunes-code" : "12+", "itunes-value" : "200", "description" : "12+" }, @@ -270,6 +290,7 @@ }, { "country" : "India", + "storeCode" : 143467, "ratings" : [ { "media" : "movie", "prefix" : "in-movies", "itunes-code" : "U", "itunes-value" : "000", "description" : "U" }, { "media" : "movie", "prefix" : "in-movies", "itunes-code" : "UA", "itunes-value" : "200", "description" : "UA" }, @@ -284,6 +305,7 @@ }, { "country" : "Unknown", + "storeCode" : 0, "ratings" : [ { "media" : "movie & TV", "prefix" : "mpaa", "itunes-code" : "Unrated", "itunes-value" : "???", "description" : "Unknown" } ] diff --git a/Ratings.swift b/Ratings.swift index d7d6962..7938fc6 100644 --- a/Ratings.swift +++ b/Ratings.swift @@ -9,7 +9,6 @@ import Foundation final class Ratings { static let shared = Ratings() - let countries: [Country] private init() { @@ -24,14 +23,20 @@ final class Ratings { guard let country = countries.first(where: { $0.displayName == countryCode }) else { return nil } return country.ratings.first(where: {$0.media.contains(media) && $0.displayName == name}) } + + func rating(storeCode: Int, media: String, code: String) -> Rating? { + guard let country = countries.first(where: { $0.storeCode == storeCode }) else { return nil } + return country.ratings.first(where: {$0.media.contains(media) && $0.code == code}) + } } struct Country: Codable { let displayName: String + let storeCode: Int let ratings: [Rating] enum CodingKeys: String, CodingKey { - case displayName = "country", ratings + case displayName = "country", storeCode, ratings } }