From 9d301a7e6521a73b46ec8af23fdf083de629b2ec Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Thu, 24 Oct 2019 12:21:26 +0200 Subject: [PATCH] Experimental code to get iTunes new artworks. --- .../MetadataImporters/MetadataResult.swift | 4 + Classes/MetadataImporters/iTunesStore.swift | 7 + Subler.xcodeproj/project.pbxproj | 4 + iTunesStoreArtworks.swift | 147 ++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 iTunesStoreArtworks.swift diff --git a/Classes/MetadataImporters/MetadataResult.swift b/Classes/MetadataImporters/MetadataResult.swift index 5fcf841..f35ec99 100644 --- a/Classes/MetadataImporters/MetadataResult.swift +++ b/Classes/MetadataImporters/MetadataResult.swift @@ -19,12 +19,15 @@ public enum ArtworkType : Int, CustomStringConvertible { return "season" case .square: return "square" + case .rectangle: + return "rectangle" case .episode: return "episode" case .backdrop: return "backdrop" case .iTunes: return "iTunes" + case .none: return NSLocalizedString("None", comment: "Queue metadata search artwork type") } @@ -33,6 +36,7 @@ public enum ArtworkType : Int, CustomStringConvertible { case poster case season case square + case rectangle case episode case backdrop case iTunes diff --git a/Classes/MetadataImporters/iTunesStore.swift b/Classes/MetadataImporters/iTunesStore.swift index d67c39c..660e58d 100644 --- a/Classes/MetadataImporters/iTunesStore.swift +++ b/Classes/MetadataImporters/iTunesStore.swift @@ -756,6 +756,13 @@ public struct iTunesStore: MetadataService { } } + /* + guard let title = metadata[.name] as? String else { return metadata } + + let additionalArtworks = iTunesStoreArtworks().search(term: title, iTunesStore: store.storeCode, locale: "en-US", type: "Movie") + metadata.remoteArtworks.append(contentsOf: additionalArtworks) + */ + return metadata } diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index af25d17..188394b 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ A9067A0C1F3CA4E6008011DC /* MetadataPrefsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9067A0B1F3CA4E6008011DC /* MetadataPrefsViewController.swift */; }; A906B5B11E434872007D12B1 /* Queue.sdef in Resources */ = {isa = PBXBuildFile; fileRef = A906B5B01E434872007D12B1 /* Queue.sdef */; }; A90B023B10F3290A000416F3 /* PrefsWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A90B023910F3290A000416F3 /* PrefsWindowController.xib */; }; + A90B094C235614D200F1B191 /* iTunesStoreArtworks.swift in Sources */ = {isa = PBXBuildFile; fileRef = A90B094B235614D200F1B191 /* iTunesStoreArtworks.swift */; }; A90BC5E31F3A139700F9F3F3 /* MetadataResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = A90BC5E21F3A139700F9F3F3 /* MetadataResult.swift */; }; A90C3E0C1FD16AF200F64532 /* TokensViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A90C3E0A1FD16AF200F64532 /* TokensViewController.swift */; }; A90CD5C01FD177230070A067 /* TokensViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A90CD5C21FD177230070A067 /* TokensViewController.xib */; }; @@ -226,6 +227,7 @@ A90300C91DAE72C4008237D8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/PresetPrefsViewController.xib; sourceTree = ""; }; A9067A0B1F3CA4E6008011DC /* MetadataPrefsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MetadataPrefsViewController.swift; path = Classes/MetadataPrefsViewController.swift; sourceTree = ""; }; A906B5B01E434872007D12B1 /* Queue.sdef */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Queue.sdef; sourceTree = ""; }; + A90B094B235614D200F1B191 /* iTunesStoreArtworks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iTunesStoreArtworks.swift; sourceTree = ""; }; A90BC5E21F3A139700F9F3F3 /* MetadataResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MetadataResult.swift; path = Classes/MetadataImporters/MetadataResult.swift; sourceTree = ""; }; A90C3E0A1FD16AF200F64532 /* TokensViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TokensViewController.swift; path = Classes/TokensViewController.swift; sourceTree = ""; }; A90CD5C11FD177230070A067 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/TokensViewController.xib; sourceTree = ""; }; @@ -688,6 +690,7 @@ A90BC5E21F3A139700F9F3F3 /* MetadataResult.swift */, A900CE3D1F3ACF9700CCA08E /* MetadataResultMap.swift */, A9E97F131F274F1100DBA586 /* iTunesStore.swift */, + A90B094B235614D200F1B191 /* iTunesStoreArtworks.swift */, A98D98451F0280CB00A77ABC /* TheMovieDB.swift */, A98D98471F02820F00A77ABC /* TheMovieDBService.swift */, A9B514181EFC12310055A035 /* TheTVDB.swift */, @@ -890,6 +893,7 @@ A98D6BAB21FDC62800F0D886 /* Queue.swift in Sources */, A98542DC20D1A7E500EB9803 /* ArtworkSelectorViewItem.swift in Sources */, A90C3E0C1FD16AF200F64532 /* TokensViewController.swift in Sources */, + A90B094C235614D200F1B191 /* iTunesStoreArtworks.swift in Sources */, A97D99DA22A0240C001FA3A7 /* OCRManager.swift in Sources */, A9DA03E322A0211C0056AE7B /* OCRPrefsViewController.swift in Sources */, A9C09C7C1F99F460007939EE /* ExceptionAlertController.swift in Sources */, diff --git a/iTunesStoreArtworks.swift b/iTunesStoreArtworks.swift new file mode 100644 index 0000000..2c6d5b0 --- /dev/null +++ b/iTunesStoreArtworks.swift @@ -0,0 +1,147 @@ +// +// iTunesStore.swift +// iTunes Artwork +// +// Created by Damiano Galassi on 15/10/2019. +// Copyright © 2019 Damiano Galassi. All rights reserved. +// + +import Foundation + +public struct iTunesStoreArtworks { + + private func sendJSONRequest(url: URL, type: T.Type) -> T? where T : Decodable { + guard let data = URLSession.data(from: url) else { return nil } + + do { + let result = try JSONDecoder().decode(type, from: data) + return result + } catch { + print("error: \(error)") + } + + return nil + } + + private let baseURL = "https://tv.apple.com/api/uts/v2/uts/v2/search/incremental?&utsk=0&caller=0&v=31" + + func search(term: String, iTunesStore: Int, locale: String, type: String = "Movie") -> [Artwork] { + if let url = URL(string: "\(baseURL)&sf=\(iTunesStore)&pfm=desktop&locale=\(locale)&q=\(term.urlEncoded())"), + let result = sendJSONRequest(url: url, type: Wrapper.self) { + + let filteredResults = result.data.results.filter { $0.type == type }.flatMap { $0.items }.filter { $0.title == term } + let urls = filteredResults.compactMap { $0.images }.compactMap { $0.coverArt16X9 }.compactMap { $0.url } + + let artworks = urls.compactMap { (url: String) -> Artwork? in + let baseURL = url.replacingOccurrences(of: "{w}x{h}.{f}", with: "") + if let artworkURL = URL(string: baseURL + "1920x1080.jpg"), let thumbURL = URL(string: baseURL + "320x180.jpg") { + return Artwork(url: artworkURL, thumbURL: thumbURL, service: "iTunes", type: .rectangle) + } else { + return nil + } + } + return artworks; + } + return [] + } + + func search(term: String, contentID: Int, iTunesStore: Int, locale: String, type: String = "Movie") -> [Artwork] { + if let url = URL(string: "\(baseURL)&sf=\(iTunesStore)&pfm=desktop&locale=\(locale)&q=\(term.urlEncoded())"), + let result = sendJSONRequest(url: url, type: Wrapper.self) { + + let contentID = String(contentID) + let filteredResults = result.data.results.filter { $0.type == type }.flatMap { $0.items } + let urls = filteredResults.compactMap { $0.images }.compactMap { $0.coverArt16X9 }.compactMap { $0.url }.filter { $0.contains(contentID) } + + let artworks = urls.compactMap { (url: String) -> Artwork? in + let baseURL = url.replacingOccurrences(of: "{w}x{h}.{f}", with: "") + if let artworkURL = URL(string: baseURL + "1920x1080.jpg"), let thumbURL = URL(string: baseURL + "320x180.jpg") { + return Artwork(url: artworkURL, thumbURL: thumbURL, service: "iTunes Store", type: .rectangle) + } else { + return nil + } + } + return artworks; + } + return [] + } + + struct Target: Codable { + let id: String + let type: String + } + + struct Image: Codable { + let format: String + let width: UInt + let height: UInt + let hasAlpha: Bool? + let joeColor: String? + let url: String + } + + struct Images: Codable { + let bannerUberImage: Image? + let contentLogo: Image? + let coverArt: Image? + let coverArt16X9: Image? + let fullColorContentLogo: Image? + let fullScreenBackground: Image? + let previewFrame: Image? + } + + struct Link: Codable { + let target: Target + } + + struct Rating: Codable { + let displayName: String + let name: String + let system: String + let value: UInt + } + + struct Roles: Codable { + let cast: [String]? + let directors: [String]? + } + + struct Item: Codable { + let canonicalId: String? + let commonSenseRecommendedAge: UInt? + let contentAdvisories: [String]? + let description: String? + let duration: UInt? + let id: String + let images: Images + let isEntitledToPlay: Bool? + let links: Link + let rating: Rating? + let ratingValue: UInt? + let releaseDate: UInt? + let rolesSummary: Roles? + let title: String? + let tomatometerFreshness: String? + let tomatometerPercentage: UInt? + let type: String + let url: URL + } + + struct ItemCollection: Codable { + let id: String + let items: [Item] + let locTitle: String + let score: Double + let type: String + } + + struct Result: Codable { + let q: String + let results: [ItemCollection] + } + + struct Wrapper: Codable { + let data: Result + } + +}