Change the way iTunes Store tv shows are found.
This commit is contained in:
2 files changed
+45
-61
No files matched your search
@@ -267,7 +267,8 @@ final public class TheTVDBService {
|
|||||||
// MARK: - Service calls
|
// MARK: - Service calls
|
||||||
|
|
||||||
public func fetch(series: String, language: String) -> [TVDBSeriesSearchResult] {
|
public func fetch(series: String, language: String) -> [TVDBSeriesSearchResult] {
|
||||||
let encodedName = series.urlEncoded()
|
// Remove + because it breaks search
|
||||||
|
let encodedName = series.replacingOccurrences(of: "+", with: "").urlEncoded()
|
||||||
|
|
||||||
guard let url = URL(string: "\(basePath)search/series?name=\(encodedName)"),
|
guard let url = URL(string: "\(basePath)search/series?name=\(encodedName)"),
|
||||||
let result = sendJSONRequest(url: url, language: language, type: Wrapper<[TVDBSeriesSearchResult]>.self)
|
let result = sendJSONRequest(url: url, language: language, type: Wrapper<[TVDBSeriesSearchResult]>.self)
|
||||||
|
|||||||
@@ -100,36 +100,46 @@ private struct Collection : Codable {
|
|||||||
let trackCount: Int?
|
let trackCount: Int?
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Collection {
|
extension KeyedDecodingContainer {
|
||||||
init(from decoder: Decoder) throws {
|
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
public func decodeIntOrString(forKey key: KeyedDecodingContainer<K>.Key) throws -> Int {
|
||||||
do {
|
do {
|
||||||
artistId = try container.decode(Int.self, forKey: .artistId)
|
return try self.decode(Int.self, forKey: key)
|
||||||
} catch {
|
} catch {
|
||||||
if let artistIdString = try? container.decode(String.self, forKey: .artistId),
|
if let stringValue = try? self.decode(String.self, forKey: key),
|
||||||
let artistIdInt = Int(artistIdString) {
|
let intValue = Int(stringValue){
|
||||||
artistId = artistIdInt
|
return intValue
|
||||||
} else {
|
} else {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func decodeIntOrStringIfPreset(forKey key: KeyedDecodingContainer<K>.Key) throws -> Int? {
|
||||||
|
do {
|
||||||
|
return try self.decodeIfPresent(Int.self, forKey: key)
|
||||||
|
} catch {
|
||||||
|
if let stringValue = try self.decodeIfPresent(String.self, forKey: key) {
|
||||||
|
return Int(stringValue)
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Collection {
|
||||||
|
init(from decoder: Decoder) throws {
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
artistId = try container.decodeIntOrString(forKey: .artistId)
|
||||||
artistName = try container.decode(String.self, forKey: .artistName)
|
artistName = try container.decode(String.self, forKey: .artistName)
|
||||||
artistViewUrl = try container.decodeIfPresent(URL.self, forKey: .artistViewUrl)
|
artistViewUrl = try container.decodeIfPresent(URL.self, forKey: .artistViewUrl)
|
||||||
artworkUrl100 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl100)
|
artworkUrl100 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl100)
|
||||||
artworkUrl60 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl60)
|
artworkUrl60 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl60)
|
||||||
collectionCensoredName = try container.decodeIfPresent(String.self, forKey: .collectionCensoredName)
|
collectionCensoredName = try container.decodeIfPresent(String.self, forKey: .collectionCensoredName)
|
||||||
collectionExplicitness = try container.decodeIfPresent(String.self, forKey: .collectionExplicitness)
|
collectionExplicitness = try container.decodeIfPresent(String.self, forKey: .collectionExplicitness)
|
||||||
|
collectionId = try container.decodeIntOrString(forKey: .collectionId)
|
||||||
do {
|
|
||||||
collectionId = try container.decode(Int.self, forKey: .collectionId)
|
|
||||||
} catch {
|
|
||||||
if let collectionIdString = try? container.decode(String.self, forKey: .collectionId),
|
|
||||||
let collectionIdInt = Int(collectionIdString) {
|
|
||||||
collectionId = collectionIdInt
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
collectionName = try container.decode(String.self, forKey: .collectionName)
|
collectionName = try container.decode(String.self, forKey: .collectionName)
|
||||||
collectionType = try container.decode(String.self, forKey: .collectionType)
|
collectionType = try container.decode(String.self, forKey: .collectionType)
|
||||||
collectionViewUrl = try container.decodeIfPresent(String.self, forKey: .collectionViewUrl)
|
collectionViewUrl = try container.decodeIfPresent(String.self, forKey: .collectionViewUrl)
|
||||||
@@ -188,34 +198,12 @@ extension Track {
|
|||||||
artworkUrl100 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl100)
|
artworkUrl100 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl100)
|
||||||
artworkUrl30 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl30)
|
artworkUrl30 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl30)
|
||||||
artworkUrl60 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl60)
|
artworkUrl60 = try container.decodeIfPresent(URL.self, forKey: .artworkUrl60)
|
||||||
|
artistId = try container.decodeIntOrStringIfPreset(forKey: .artistId)
|
||||||
do {
|
|
||||||
artistId = try container.decodeIfPresent(Int.self, forKey: .artistId)
|
|
||||||
} catch {
|
|
||||||
if let artistIdString = try container.decodeIfPresent(String.self, forKey: .artistId),
|
|
||||||
let artistIdInt = Int(artistIdString) {
|
|
||||||
artistId = artistIdInt
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
collectionArtistId = try container.decodeIfPresent(String.self, forKey: .collectionArtistId)
|
collectionArtistId = try container.decodeIfPresent(String.self, forKey: .collectionArtistId)
|
||||||
collectionArtistViewUrl = try container.decodeIfPresent(URL.self, forKey: .collectionArtistViewUrl)
|
collectionArtistViewUrl = try container.decodeIfPresent(URL.self, forKey: .collectionArtistViewUrl)
|
||||||
collectionCensoredName = try container.decodeIfPresent(String.self, forKey: .collectionCensoredName)
|
collectionCensoredName = try container.decodeIfPresent(String.self, forKey: .collectionCensoredName)
|
||||||
collectionExplicitness = try container.decodeIfPresent(String.self, forKey: .collectionExplicitness)
|
collectionExplicitness = try container.decodeIfPresent(String.self, forKey: .collectionExplicitness)
|
||||||
|
collectionId = try container.decodeIntOrStringIfPreset(forKey: .collectionId)
|
||||||
do {
|
|
||||||
collectionId = try container.decodeIfPresent(Int.self, forKey: .collectionId)
|
|
||||||
} catch {
|
|
||||||
if let collectionIdString = try container.decodeIfPresent(String.self, forKey: .collectionId),
|
|
||||||
let collectionIdInt = Int(collectionIdString) {
|
|
||||||
collectionId = collectionIdInt
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
collectionName = try container.decodeIfPresent(String.self, forKey: .collectionName)
|
collectionName = try container.decodeIfPresent(String.self, forKey: .collectionName)
|
||||||
collectionViewUrl = try container.decodeIfPresent(URL.self, forKey: .collectionViewUrl)
|
collectionViewUrl = try container.decodeIfPresent(URL.self, forKey: .collectionViewUrl)
|
||||||
contentAdvisoryRating = try container.decodeIfPresent(String.self, forKey: .contentAdvisoryRating)
|
contentAdvisoryRating = try container.decodeIfPresent(String.self, forKey: .contentAdvisoryRating)
|
||||||
@@ -233,16 +221,7 @@ extension Track {
|
|||||||
trackCensoredName = try container.decodeIfPresent(String.self, forKey: .trackCensoredName)
|
trackCensoredName = try container.decodeIfPresent(String.self, forKey: .trackCensoredName)
|
||||||
trackCount = try container.decodeIfPresent(Int.self, forKey: .trackCount)
|
trackCount = try container.decodeIfPresent(Int.self, forKey: .trackCount)
|
||||||
trackExplicitness = try container.decodeIfPresent(String.self, forKey: .trackExplicitness)
|
trackExplicitness = try container.decodeIfPresent(String.self, forKey: .trackExplicitness)
|
||||||
do {
|
trackId = try container.decodeIntOrStringIfPreset(forKey: .trackId)
|
||||||
trackId = try container.decodeIfPresent(Int.self, forKey: .trackId)
|
|
||||||
} catch {
|
|
||||||
if let trackIdString = try container.decodeIfPresent(String.self, forKey: .trackId),
|
|
||||||
let trackIdInt = Int(trackIdString) {
|
|
||||||
trackId = trackIdInt
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
trackName = try container.decodeIfPresent(String.self, forKey: .trackName)
|
trackName = try container.decodeIfPresent(String.self, forKey: .trackName)
|
||||||
trackNumber = try container.decodeIfPresent(Int.self, forKey: .trackNumber)
|
trackNumber = try container.decodeIfPresent(Int.self, forKey: .trackNumber)
|
||||||
trackTimeMillis = try container.decodeIfPresent(Double.self, forKey: .trackTimeMillis)
|
trackTimeMillis = try container.decodeIfPresent(Double.self, forKey: .trackTimeMillis)
|
||||||
@@ -437,16 +416,16 @@ public struct iTunesStore: MetadataService {
|
|||||||
if let seasonNum = seasonNum {
|
if let seasonNum = seasonNum {
|
||||||
let searchTerm = "\(seriesName) \(store.season) \(seasonNum)".urlEncoded()
|
let searchTerm = "\(seriesName) \(store.season) \(seasonNum)".urlEncoded()
|
||||||
if relaxSearch {
|
if relaxSearch {
|
||||||
return URL(string: "https://itunes.apple.com/search?country=\(store.country2)&lang=\(store.language2.lowercased())&term=\(searchTerm)&attribute=tvSeasonTerm&limit=250")
|
return URL(string: "https://itunes.apple.com/search?term=\(searchTerm)&entity=tvSeason&country=\(store.country2)&lang=\(store.language2.lowercased())&limit=250")
|
||||||
} else {
|
} else {
|
||||||
return URL(string: "https://itunes.apple.com/search?country=\(store.country2)&lang=\(store.language2.lowercased())&term=\(searchTerm)&attribute=tvSeasonTerm&entity=tvSeason&limit=250")
|
return URL(string: "https://itunes.apple.com/search?term=\(searchTerm)&attribute=tvSeasonTerm&entity=tvSeason&country=\(store.country2)&lang=\(store.language2.lowercased())&limit=250")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let searchTerm = seriesName.urlEncoded()
|
let searchTerm = seriesName.urlEncoded()
|
||||||
if relaxSearch {
|
if relaxSearch {
|
||||||
return URL(string: "https://itunes.apple.com/search?country=\(store.country2)&lang=\(store.language2.lowercased())&term=\(searchTerm)&attribute=tvSeasonTerm&limit=250")
|
return URL(string: "https://itunes.apple.com/search?term=\(searchTerm)&entity=tvSeason&country=\(store.country2)&lang=\(store.language2.lowercased())&limit=250")
|
||||||
} else {
|
} else {
|
||||||
return URL(string: "https://itunes.apple.com/search?country=\(store.country2)&lang=\(store.language2.lowercased())&term=\(searchTerm)&attribute=showTerm&entity=tvShow&limit=250")
|
return URL(string: "https://itunes.apple.com/search?term=\(searchTerm)&attribute=showTerm&entity=tvShow&country=\(store.country2)&lang=\(store.language2.lowercased())&limit=250")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -504,7 +483,7 @@ public struct iTunesStore: MetadataService {
|
|||||||
|
|
||||||
// If we have an ID, use the lookup API to get episodes for that show/season
|
// If we have an ID, use the lookup API to get episodes for that show/season
|
||||||
for id in ids {
|
for id in ids {
|
||||||
if let lookupUrl = URL(string: "https://itunes.apple.com/lookup?country=\(store.country2)&id=\(id)&entity=tvEpisode&limit=200"),
|
if let lookupUrl = URL(string: "https://itunes.apple.com/lookup?country=\(store.country2)&id=\(id)&entity=tvEpisode&limit=250"),
|
||||||
let results = sendJSONRequest(url: lookupUrl, type: Wrapper<Track>.self) {
|
let results = sendJSONRequest(url: lookupUrl, type: Wrapper<Track>.self) {
|
||||||
|
|
||||||
var filteredResults = results.results.filter { $0.wrapperType == "track" } .map { metadata(forTVResult: $0, store: store) }
|
var filteredResults = results.results.filter { $0.wrapperType == "track" } .map { metadata(forTVResult: $0, store: store) }
|
||||||
@@ -562,17 +541,21 @@ public struct iTunesStore: MetadataService {
|
|||||||
separated = s.components(separatedBy: ", vol. ")
|
separated = s.components(separatedBy: ", vol. ")
|
||||||
}
|
}
|
||||||
|
|
||||||
let trackCount = result.trackCount ?? 1
|
|
||||||
let season = { () -> Int in
|
let season = { () -> Int in
|
||||||
if separated.count > 1 {
|
if separated.count > 1 {
|
||||||
let season = separated[1]
|
let season = separated[1]
|
||||||
if season.contains("season") {
|
if season.contains("season") {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return Int(separated[1].trimmingCharacters(in: CharacterSet.decimalDigits.inverted)) ?? 1
|
let subparts = season.components(separatedBy: ",")
|
||||||
|
if subparts.count > 1 {
|
||||||
|
return Int(subparts[0].trimmingCharacters(in: CharacterSet.decimalDigits.inverted)) ?? 1
|
||||||
|
} else {
|
||||||
|
return Int(separated[1].trimmingCharacters(in: CharacterSet.decimalDigits.inverted)) ?? 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return trackCount > 1 ? 1 : 0
|
return 0
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user