Improve iTunes Store tv show search, fix some rare cases where it tried to get data for a show no longer on sale.
This commit is contained in:
1 file changed
+22
-28
@@ -195,55 +195,46 @@ public struct iTunesStore: MetadataService {
|
|||||||
|
|
||||||
// MARK: - Search for TV episode metadata
|
// MARK: - Search for TV episode metadata
|
||||||
|
|
||||||
private func extractID(results: [Collection], show: String, season: Int, store: Store) -> Int? {
|
private func extractID(result: Collection, show: String, season: Int, store: Store) -> Int? {
|
||||||
let showPattern = show.replacingOccurrences(of: " ", with: ".*?")
|
let showPattern = show.replacingOccurrences(of: " ", with: ".*?")
|
||||||
let seasonPattern = "\(store.season)\\s\(season)$"
|
let seasonPattern = "\(store.season)\\s\(season)$"
|
||||||
|
|
||||||
guard let showRegex = try? NSRegularExpression(pattern: showPattern, options: [.caseInsensitive]) else { return nil }
|
guard let showRegex = try? NSRegularExpression(pattern: showPattern, options: [.caseInsensitive]) else { return nil }
|
||||||
guard let seasonRegex = try? NSRegularExpression(pattern: seasonPattern, options: [.caseInsensitive]) else { return nil }
|
guard let seasonRegex = try? NSRegularExpression(pattern: seasonPattern, options: [.caseInsensitive]) else { return nil }
|
||||||
|
|
||||||
for result in results {
|
|
||||||
|
|
||||||
// Skip if the artistName doesn't match the show
|
// Skip if the artistName doesn't match the show
|
||||||
if showRegex.matches(in: result.artistName, options: [], range: NSRange(result.artistName.startIndex..., in: result.artistName)).isEmpty {
|
if showRegex.matches(in: result.artistName, options: [], range: NSRange(result.artistName.startIndex..., in: result.artistName)).isEmpty {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.collectionType != "TV Season" {
|
if result.collectionType != "TV Season" {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if seasonRegex.matches(in: result.collectionName, options: [], range: NSRange(result.collectionName.startIndex..., in: result.collectionName)).isEmpty {
|
if seasonRegex.matches(in: result.collectionName, options: [], range: NSRange(result.collectionName.startIndex..., in: result.collectionName)).isEmpty {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.collectionId
|
return result.collectionId
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
private func extractID(result: Artist, show: String, store: Store) -> Int? {
|
||||||
}
|
|
||||||
|
|
||||||
private func extractID(results: [Artist], show: String, store: Store) -> Int? {
|
|
||||||
let showPattern = show.replacingOccurrences(of: " ", with: ".*?")
|
let showPattern = show.replacingOccurrences(of: " ", with: ".*?")
|
||||||
guard let showRegex = try? NSRegularExpression(pattern: showPattern, options: [.caseInsensitive]) else { return nil }
|
guard let showRegex = try? NSRegularExpression(pattern: showPattern, options: [.caseInsensitive]) else { return nil }
|
||||||
|
|
||||||
for result in results {
|
|
||||||
// Skip if the artistName doesn't match the show
|
// Skip if the artistName doesn't match the show
|
||||||
if showRegex.matches(in: result.artistName, options: [], range: NSRange(result.artistName.startIndex..., in: result.artistName)).isEmpty {
|
if showRegex.matches(in: result.artistName, options: [], range: NSRange(result.artistName.startIndex..., in: result.artistName)).isEmpty {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.artistType != "TV Show" {
|
if result.artistType != "TV Show" {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.artistId
|
return result.artistId
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
private func findiTunesIDs(seriesName: String, seasonNum: Int?, store: Store) -> [Int] {
|
||||||
}
|
|
||||||
|
|
||||||
private func findiTunesID(seriesName: String, seasonNum: Int?, store: Store) -> Int? {
|
|
||||||
// Determine artistId/collectionId
|
// Determine artistId/collectionId
|
||||||
guard let url = { () -> URL? in
|
guard let url = { () -> URL? in
|
||||||
if let seasonNum = seasonNum {
|
if let seasonNum = seasonNum {
|
||||||
@@ -255,34 +246,34 @@ public struct iTunesStore: MetadataService {
|
|||||||
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?country=\(store.country2)&lang=\(store.language2.lowercased())&term=\(searchTerm)&attribute=showTerm&entity=tvShow&limit=250")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
else { return nil }
|
else { return [] }
|
||||||
|
|
||||||
if let seasonNum = seasonNum {
|
if let seasonNum = seasonNum {
|
||||||
if let results = sendJSONRequest(url: url, type: Wrapper<Collection>.self) {
|
if let results = sendJSONRequest(url: url, type: Wrapper<Collection>.self)?.results {
|
||||||
return extractID(results: results.results, show: seriesName, season: seasonNum, store: store)
|
return results.flatMap { extractID(result: $0, show: seriesName, season: seasonNum, store: store) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if let results = sendJSONRequest(url: url, type: Wrapper<Artist>.self) {
|
if let results = sendJSONRequest(url: url, type: Wrapper<Artist>.self)?.results {
|
||||||
return extractID(results: results.results, show: seriesName, store: store)
|
return results.flatMap { extractID(result: $0, show: seriesName, store: store) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
public func search(TVSeries: String, language: String, season: Int?, episode: Int?) -> [MetadataResult] {
|
public func search(TVSeries: String, language: String, season: Int?, episode: Int?) -> [MetadataResult] {
|
||||||
guard let store = iTunesStore.store(language: language) else { return [] }
|
guard let store = iTunesStore.store(language: language) else { return [] }
|
||||||
|
|
||||||
// Determine artistId/collectionId
|
// Determine artistId/collectionId
|
||||||
guard let id = { () -> Int? in
|
let ids = { () -> [Int] in
|
||||||
if let id = findiTunesID(seriesName: TVSeries, seasonNum: season, store: store) { return id }
|
let idsWithSeason = self.findiTunesIDs(seriesName: TVSeries, seasonNum: season, store: store)
|
||||||
else if let id = findiTunesID(seriesName: TVSeries, seasonNum: nil, store: store) { return id }
|
if idsWithSeason.isEmpty == false { return idsWithSeason }
|
||||||
else { return nil }
|
return self.findiTunesIDs(seriesName: TVSeries, seasonNum: nil, store: store)
|
||||||
}()
|
}()
|
||||||
else { return [] }
|
|
||||||
|
|
||||||
// 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 {
|
||||||
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=200"),
|
||||||
let results = sendJSONRequest(url: lookupUrl, type: Wrapper<Track>.self) {
|
let results = sendJSONRequest(url: lookupUrl, type: Wrapper<Track>.self) {
|
||||||
|
|
||||||
@@ -296,8 +287,11 @@ public struct iTunesStore: MetadataService {
|
|||||||
filteredResults = filteredResults.filter { $0[.episodeNumber] as! Int == episode }
|
filteredResults = filteredResults.filter { $0[.episodeNumber] as! Int == episode }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filteredResults.isEmpty == false {
|
||||||
return filteredResults.sorted(by: areInIncreasingOrder)
|
return filteredResults.sorted(by: areInIncreasingOrder)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user