Skip to content

Commit

Permalink
Merge pull request #708 from rgoldberg/707-no-scrape
Browse files Browse the repository at this point in the history
Do not scrape MAS app web page for version
  • Loading branch information
rgoldberg authored Jan 12, 2025
2 parents 0d8d7c1 + 8711b5b commit a561259
Showing 1 changed file with 2 additions and 49 deletions.
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

0 comments on commit a561259

Please sign in to comment.