From be122c0b473db9ca9acaad7b2204132681f05595 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Thu, 16 Aug 2018 14:18:04 +0200 Subject: [PATCH] Update the iTunes Store scraper. --- .../MetadataImporters/MetadataHelper.swift | 4 ++ Classes/MetadataImporters/iTunesStore.swift | 65 +++++++++++------- iTunesStores.json | 66 +++++++++---------- 3 files changed, 78 insertions(+), 57 deletions(-) diff --git a/Classes/MetadataImporters/MetadataHelper.swift b/Classes/MetadataImporters/MetadataHelper.swift index a2015c9..5ad9d6b 100644 --- a/Classes/MetadataImporters/MetadataHelper.swift +++ b/Classes/MetadataImporters/MetadataHelper.swift @@ -104,4 +104,8 @@ extension String { } return .none } + + func trimmingWhitespacesAndNewlinews() -> String { + return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + } } diff --git a/Classes/MetadataImporters/iTunesStore.swift b/Classes/MetadataImporters/iTunesStore.swift index add4b07..e4c3d26 100644 --- a/Classes/MetadataImporters/iTunesStore.swift +++ b/Classes/MetadataImporters/iTunesStore.swift @@ -75,10 +75,12 @@ public struct iTunesStore: MetadataService { let language: String let season: String let country: String - let actor: String + let cast: String let director: String let producer: String let screenwriter: String + let studio: String + let copyright: String } private static let stores: [Store] = { @@ -489,19 +491,46 @@ public struct iTunesStore: MetadataService { } /// Scrape people from iTunes Store website HTML - private func read(type: String, in xml: XMLDocument) -> [String] { - guard let nodes = try? xml.nodes(forXPath: "//div[starts-with(@metrics-loc,'Titledbox_\(type)')]") else { return [] } + func read(type: String, in xml: XMLDocument) -> [String] { + guard let nodes = try? xml.nodes(forXPath: "//dl[contains(@class,'cast-list__role')]") else { return [] } for node in nodes { - if let subXml = try? XMLDocument(xmlString: node.xmlString, options: []), - let subNodes = try? subXml.nodes(forXPath: "//a") { - return subNodes.compactMap { $0.stringValue } + guard let typeNodes = try? node.nodes(forXPath: "dt[contains(@class,'cast-list__term')]"), + let valueNodes = try? node.nodes(forXPath: "dd[contains(@class,'cast-list__detail')]") else { continue } + + if let nodeType = typeNodes.first?.stringValue?.trimmingWhitespacesAndNewlinews(), nodeType.contains(type) { + return valueNodes.compactMap { $0.stringValue } } } return [] } + func readInfo(type: String, in xml: XMLDocument) -> String? { + guard let nodes = try? xml.nodes(forXPath: "//dl[contains(@class,'information-list--episode')]/dd/div/dl") else { return nil } + + for node in nodes { + guard let typeNodes = try? node.nodes(forXPath: "dt[contains(@class,'information-list__item__term')]"), + let valueNodes = try? node.nodes(forXPath: "dd[contains(@class,'information-list__item__definition')]") else { continue } + + if let nodeType = typeNodes.first?.stringValue, nodeType.contains(type), + var value = valueNodes.first?.stringValue?.trimmingWhitespacesAndNewlinews() { + if let range = value.range(of: ". All Rights Reserved", options: .caseInsensitive, + range: value.startIndex ..< value.endIndex, locale: nil) { + value.removeSubrange(range) + } + if let range = value.range(of: " by", options: .caseInsensitive, + range: value.startIndex ..< value.endIndex, locale: nil) { + value.removeSubrange(range) + } + value = value.replacingOccurrences(of: "Â", with: "") + return value + } + } + + return nil + } + public func loadMovieMetadata(_ metadata: MetadataResult, language: String) -> MetadataResult { guard let store = iTunesStore.store(language: language), let url = metadata[.iTunesURL] as? URL, @@ -509,26 +538,14 @@ public struct iTunesStore: MetadataService { let xml = try? XMLDocument(data: data, options: .documentTidyHTML) else { return metadata } - metadata[.cast] = read(type: store.actor, in: xml).joined(separator: ", ") - metadata[.director] = read(type: store.director, in: xml).joined(separator: ", ") + if metadata[.director] == nil { + metadata[.director] = read(type: store.director, in: xml).joined(separator: ", ") + } + metadata[.cast] = read(type: store.cast, in: xml).joined(separator: ", ") metadata[.producers] = read(type: store.producer, in: xml).joined(separator: ", ") metadata[.screenwriters] = read(type: store.screenwriter, in: xml).joined(separator: ", ") - - if let nodes = try? xml.nodes(forXPath: "//li[@class='copyright']") { - for node in nodes { - if var copyright = node.stringValue { - if let range = copyright.range(of: ". All Rights Reserved", options: .caseInsensitive, - range: copyright.startIndex ..< copyright.endIndex, locale: nil) { - copyright.removeSubrange(range) - } - if let range = copyright.range(of: " by", options: .caseInsensitive, - range: copyright.startIndex ..< copyright.endIndex, locale: nil) { - copyright.removeSubrange(range) - } - metadata[.copyright] = copyright - } - } - } + metadata[.studio] = readInfo(type: store.studio, in: xml) + metadata[.copyright] = readInfo(type: store.copyright, in: xml) return metadata } diff --git a/iTunesStores.json b/iTunesStores.json index c8cd33e..772b637 100644 --- a/iTunesStores.json +++ b/iTunesStores.json @@ -1,36 +1,36 @@ [ - {"storeCode": 143441, "country3": "USA", "country2": "US", "language2": "EN", "language": "English", "season": "season", "country": "USA", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143460, "country3": "AUS", "country2": "AU", "language2": "EN", "language": "English", "season": "season", "country": "Australia", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143446, "country3": "BEL", "country2": "BE", "language2": "NL", "language": "Nederlands", "season": "season", "country": "België", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143446, "country3": "BEL", "country2": "BE", "language2": "FR", "language": "Français", "season": "saison", "country": "Belgique", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143503, "country3": "BRA", "country2": "BR", "language2": "PT", "language": "Português", "season": "season", "country": "Brasil", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143505, "country3": "ARG", "country2": "AR", "language2": "ES", "language": "Español", "season": "season", "country": "Argentina", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Scriptwriter"}, - {"storeCode": 143455, "country3": "CAN", "country2": "CA", "language2": "EN", "language": "English", "season": "season", "country": "Canada", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143455, "country3": "CAN", "country2": "CA", "language2": "FR", "language": "Français", "season": "saison", "country": "Canada", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143458, "country3": "DNK", "country2": "DK", "language2": "DA", "language": "Dansk", "season": "season", "country": "Danmark", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143443, "country3": "DEU", "country2": "DE", "language2": "DE", "language": "Deutsch", "season": "staffel", "country": "Deutschland", "actor": "Schauspieler", "director": "Regisseur", "producer": "Produzent", "screenwriter": "Drehbuchautoren"}, - {"storeCode": 143454, "country3": "ESP", "country2": "ES", "language2": "ES", "language": "Español", "season": "season", "country": "España", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143442, "country3": "FRA", "country2": "FR", "language2": "FR", "language": "Français", "season": "saison", "country": "France", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143448, "country3": "GRC", "country2": "GR", "language2": "HE", "language": "ελληνικά", "season": "season", "country": "Greece", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143449, "country3": "IRL", "country2": "IE", "language2": "EN", "language": "English", "season": "season", "country": "Ireland", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143450, "country3": "ITA", "country2": "IT", "language2": "IT", "language": "Italiano", "season": "season", "country": "Italia", "actor": "Attori", "director": "Regista", "producer": "Produttori", "screenwriter": "Sceneggiatori"}, - {"storeCode": 143462, "country3": "JPN", "country2": "JP", "language2": "JA", "language": "日本語", "season": "season", "country": "日本", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143470, "country3": "TWN", "country2": "TW", "language2": "ZH", "language": "中文", "season": "season", "country": "台灣", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143451, "country3": "LUX", "country2": "LU", "language2": "FR", "language": "Français", "season": "saison", "country": "Luxembourg", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143468, "country3": "MEX", "country2": "MX", "language2": "ES", "language": "Español", "season": "season", "country": "Mexico", "actor": "Actor", "director": "Director", "producer": "Productor", "screenwriter": "Guionista"}, - {"storeCode": 143452, "country3": "NLD", "country2": "NL", "language2": "NL", "language": "Nederlands", "season": "season", "country": "Nederland", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143461, "country3": "NZL", "country2": "NZ", "language2": "EN", "language": "English", "season": "season", "country": "New Zealand", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143457, "country3": "NOR", "country2": "NO", "language2": "NO", "language": "Norsk", "season": "season", "country": "Norge", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143453, "country3": "PRT", "country2": "PT", "language2": "PT", "language": "Português", "season": "season", "country": "Portugal", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143459, "country3": "CHE", "country2": "CH", "language2": "DE", "language": "Deutsch", "season": "staffel", "country": "Schweiz", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143459, "country3": "CHE", "country2": "CH", "language2": "FR", "language": "Français", "season": "saison", "country": "Suisse", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143447, "country3": "FIN", "country2": "FI", "language2": "FI", "language": "Suomi", "season": "season", "country": "Suomi", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143456, "country3": "SWE", "country2": "SE", "language2": "SV", "language": "Svenska", "season": "season", "country": "Sverige", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143444, "country3": "GBR", "country2": "GB", "language2": "EN", "language": "English", "season": "season", "country": "UK", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143469, "country3": "RUS", "country2": "RU", "language2": "RU", "language": "Pусский", "season": "сезон", "country": "Россия", "actor": "Актеры", "director": "Режиссер", "producer": "Продюсеры", "screenwriter": "Cценарист"}, - {"storeCode": 143474, "country3": "PHL", "country2": "PH", "language2": "EN", "language": "Pilipino", "season": "season", "country": "Pilipinas", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143463, "country3": "HKG", "country2": "HK", "language2": "EN", "language": "English", "season": "season", "country": "Hong Kong", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143489, "country3": "CZE", "country2": "CZ", "language2": "EN", "language": "Čeština", "season": "season", "country": "Česká republika", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, - {"storeCode": 143467, "country3": "IND", "country2": "IN", "language2": "EN", "language": "English", "season": "season", "country": "India", "actor": "Actor", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter"}, + {"storeCode": 143441, "country3": "USA", "country2": "US", "language2": "EN", "language": "English", "season": "season", "country": "USA", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143460, "country3": "AUS", "country2": "AU", "language2": "EN", "language": "English", "season": "season", "country": "Australia", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143446, "country3": "BEL", "country2": "BE", "language2": "NL", "language": "Nederlands", "season": "season", "country": "België", "cast": "Cast", "director": "Regisseur", "producer": "Producenten", "screenwriter": "Scenarioschrijver", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143446, "country3": "BEL", "country2": "BE", "language2": "FR", "language": "Français", "season": "saison", "country": "Belgique", "cast": "Cast", "director": "Réalisateur", "producer": "Producteurs", "screenwriter": "Scénariste", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143503, "country3": "BRA", "country2": "BR", "language2": "PT", "language": "Português", "season": "season", "country": "Brasil", "cast": "Elenco", "director": "Diretor", "producer": "Produtores", "screenwriter": "Roteirista", "studio": "Estúdio", "copyright": "Direitos autorais"}, + {"storeCode": 143505, "country3": "ARG", "country2": "AR", "language2": "ES", "language": "Español", "season": "season", "country": "Argentina", "cast": "Reparto", "director": "Director", "producer": "Productores", "screenwriter": "Guionista", "studio": "Estudio", "copyright": "Copyright"}, + {"storeCode": 143455, "country3": "CAN", "country2": "CA", "language2": "EN", "language": "English", "season": "season", "country": "Canada", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143455, "country3": "CAN", "country2": "CA", "language2": "FR", "language": "Français", "season": "saison", "country": "Canada", "cast": "Distribution", "director": "Réalisateur", "producer": "Producteurs", "screenwriter": "Screenwriter", "studio": "Scénariste", "copyright": "Droit d’auteur"}, + {"storeCode": 143458, "country3": "DNK", "country2": "DK", "language2": "DA", "language": "Dansk", "season": "season", "country": "Danmark", "cast": "Medvirkende", "director": "Instruktør", "producer": "Producenter", "screenwriter": "Manuskriptforfatter", "studio": "Filmselskab", "copyright": "Copyright"}, + {"storeCode": 143443, "country3": "DEU", "country2": "DE", "language2": "DE", "language": "Deutsch", "season": "staffel", "country": "Deutschland", "cast": "Besetzung", "director": "Regisseur", "producer": "Produzenten", "screenwriter": "Drehbuchautor", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143454, "country3": "ESP", "country2": "ES", "language2": "ES", "language": "Español", "season": "season", "country": "España", "cast": "Reparto", "director": "Director", "producer": "Productores", "screenwriter": "Guionista", "studio": "Estudio", "copyright": "Copyright"}, + {"storeCode": 143442, "country3": "FRA", "country2": "FR", "language2": "FR", "language": "Français", "season": "saison", "country": "France", "cast": "Distribution", "director": "Réalisateur", "producer": "Producteurs", "screenwriter": "Scénariste", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143448, "country3": "GRC", "country2": "GR", "language2": "HE", "language": "ελληνικά", "season": "season", "country": "Greece", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143449, "country3": "IRL", "country2": "IE", "language2": "EN", "language": "English", "season": "season", "country": "Ireland", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143450, "country3": "ITA", "country2": "IT", "language2": "IT", "language": "Italiano", "season": "season", "country": "Italia", "cast": "Cast", "director": "Regista", "producer": "Produttori", "screenwriter": "Sceneggiatore", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143462, "country3": "JPN", "country2": "JP", "language2": "JA", "language": "日本語", "season": "season", "country": "日本", "cast": "キャスト", "director": "監督", "producer": "プロデューサー", "screenwriter": "Screenwriter", "studio": "スタジオ", "copyright": "著作権"}, + {"storeCode": 143470, "country3": "TWN", "country2": "TW", "language2": "ZH", "language": "中文", "season": "season", "country": "台灣", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143451, "country3": "LUX", "country2": "LU", "language2": "FR", "language": "Français", "season": "saison", "country": "Luxembourg", "cast": "Distribution", "director": "Réalisateur", "producer": "Producteurs", "screenwriter": "Scénariste", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143468, "country3": "MEX", "country2": "MX", "language2": "ES", "language": "Español", "season": "season", "country": "Mexico", "cast": "Reparto", "director": "Director", "producer": "Productores", "screenwriter": "Guionista", "studio": "Estudio", "copyright": "Copyright"}, + {"storeCode": 143452, "country3": "NLD", "country2": "NL", "language2": "NL", "language": "Nederlands", "season": "season", "country": "Nederland", "cast": "Cast", "director": "Regisseur", "producer": "Producenten", "screenwriter": "Scenarioschrijver", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143461, "country3": "NZL", "country2": "NZ", "language2": "EN", "language": "English", "season": "season", "country": "New Zealand", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143457, "country3": "NOR", "country2": "NO", "language2": "NO", "language": "Norsk", "season": "season", "country": "Norge", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143453, "country3": "PRT", "country2": "PT", "language2": "PT", "language": "Português", "season": "season", "country": "Portugal", "cast": "Elenco", "director": "Realizador", "producer": "Produtores", "screenwriter": "Argumentistas", "studio": "Estúdio", "copyright": "Copyright"}, + {"storeCode": 143459, "country3": "CHE", "country2": "CH", "language2": "DE", "language": "Deutsch", "season": "staffel", "country": "Schweiz", "cast": "Besetzung", "director": "Regisseur", "producer": "Produzenten", "screenwriter": "Drehbuchautor", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143459, "country3": "CHE", "country2": "CH", "language2": "FR", "language": "Français", "season": "saison", "country": "Suisse", "cast": "Distribution", "director": "Réalisateur", "producer": "Producteurs", "screenwriter": "Scénariste", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143447, "country3": "FIN", "country2": "FI", "language2": "FI", "language": "Suomi", "season": "season", "country": "Suomi", "cast": "Näyttelijät", "director": "Ohjaaja", "producer": "Tuottajat", "screenwriter": "Käsikirjoittaja", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143456, "country3": "SWE", "country2": "SE", "language2": "SV", "language": "Svenska", "season": "season", "country": "Sverige", "cast": "Medverkande", "director": "Regissör", "producer": "Producenter", "screenwriter": "Manus", "studio": "Filmbolag", "copyright": "Copyright"}, + {"storeCode": 143444, "country3": "GBR", "country2": "GB", "language2": "EN", "language": "English", "season": "season", "country": "UK", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143469, "country3": "RUS", "country2": "RU", "language2": "RU", "language": "Pусский", "season": "сезон", "country": "Россия", "cast": "В ролях", "director": "Режиссеры", "producer": "Продюсеры", "screenwriter": "Сценарий", "studio": "Студия", "copyright": "Copyright"}, + {"storeCode": 143474, "country3": "PHL", "country2": "PH", "language2": "EN", "language": "Pilipino", "season": "season", "country": "Pilipinas", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143463, "country3": "HKG", "country2": "HK", "language2": "EN", "language": "English", "season": "season", "country": "Hong Kong", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143489, "country3": "CZE", "country2": "CZ", "language2": "EN", "language": "Čeština", "season": "season", "country": "Česká republika", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, + {"storeCode": 143467, "country3": "IND", "country2": "IN", "language2": "EN", "language": "English", "season": "season", "country": "India", "cast": "Cast", "director": "Director", "producer": "Producer", "screenwriter": "Screenwriter", "studio": "Studio", "copyright": "Copyright"}, ]