Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not scrape MAS app web page for version #708

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 2 additions & 49 deletions Sources/mas/Controllers/ITunesSearchAppStoreSearcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

import Foundation
import PromiseKit
import Regex
import Version

/// Manages searching the MAS catalog. Uses the iTunes Search and Lookup APIs:
/// https://performance-partners.apple.com/search-api
struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
private static let appVersionRegex = Regex(#""versionDisplay":"([^"]+)""#)

private let networkManager: NetworkManager

/// Designated initializer.
Expand All @@ -35,37 +31,12 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
guard let url = lookupURL(forAppID: appID, inRegion: region) else {
fatalError("Failed to build URL for \(appID)")
}
return
loadSearchResults(url)
return loadSearchResults(url)
.then { results -> Guarantee<SearchResult> in
guard let result = results.first else {
throw MASError.unknownAppID(appID)
}

guard let pageURL = URL(string: result.trackViewUrl) else {
return .value(result)
}

return
scrapeAppStoreVersion(pageURL)
.map { pageVersion in
guard
let pageVersion,
let searchVersion = Version(tolerant: result.version),
pageVersion > searchVersion
else {
return result
}

// Update the search result with the version from the App Store page.
var result = result
result.version = pageVersion.description
return result
}
.recover { _ in
// If we were unable to scrape the App Store page, assume compatibility.
.value(result)
}
return .value(result)
}
}

Expand Down Expand Up @@ -111,24 +82,6 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
}
}

/// Scrape the app version from the App Store webpage at the given URL.
///
/// App Store webpages frequently report a version that is newer than what is reported by the iTunes Search API.
private func scrapeAppStoreVersion(_ pageURL: URL) -> Promise<Version?> {
networkManager.loadData(from: pageURL)
.map { data in
guard
let html = String(data: data, encoding: .utf8),
let capture = Self.appVersionRegex.firstMatch(in: html)?.captures[0],
let version = Version(tolerant: capture)
else {
return nil
}

return version
}
}

/// Builds the search URL for an app.
///
/// - Parameters:
Expand Down