2016-04-14 16:14:06 +00:00
|
|
|
//
|
|
|
|
// Search.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Michael Schneider on 4/14/16.
|
|
|
|
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 20:56:10 +00:00
|
|
|
import Commandant
|
|
|
|
import Result
|
|
|
|
|
2016-04-14 19:27:07 +00:00
|
|
|
struct ResultKeys {
|
|
|
|
static let ResultCount = "resultCount"
|
|
|
|
static let Results = "results"
|
|
|
|
static let TrackName = "trackName"
|
|
|
|
static let TrackId = "trackId"
|
2018-02-08 23:25:16 +00:00
|
|
|
static let Version = "version"
|
2018-06-25 14:18:00 +00:00
|
|
|
static let Price = "price"
|
2016-04-14 19:27:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 16:50:44 +00:00
|
|
|
/// Search the Mac App Store using the iTunes Search API:
|
|
|
|
/// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct SearchCommand: CommandProtocol {
|
|
|
|
public typealias Options = SearchOptions
|
|
|
|
public let verb = "search"
|
|
|
|
public let function = "Search for apps from the Mac App Store"
|
|
|
|
|
2019-01-06 19:26:08 +00:00
|
|
|
private let networkSession: NetworkSession
|
2018-11-14 04:47:06 +00:00
|
|
|
|
2019-01-06 19:26:08 +00:00
|
|
|
public init(networkSession: NetworkSession = URLSession.shared) {
|
|
|
|
self.networkSession = networkSession
|
2018-11-14 04:47:06 +00:00
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public func run(_ options: Options) -> Result<(), MASError> {
|
2016-04-14 19:27:07 +00:00
|
|
|
guard let searchURLString = searchURLString(options.appName),
|
2019-01-06 19:26:08 +00:00
|
|
|
let searchJson = networkSession.requestSynchronousJSONWithURLString(searchURLString) as? [String: Any] else {
|
2016-09-25 21:13:23 +00:00
|
|
|
return .failure(.searchFailed)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2016-10-21 21:59:33 +00:00
|
|
|
guard let resultCount = searchJson[ResultKeys.ResultCount] as? Int, resultCount > 0,
|
|
|
|
let results = searchJson[ResultKeys.Results] as? [[String: Any]] else {
|
2016-04-14 19:27:07 +00:00
|
|
|
print("No results found")
|
2016-09-25 21:13:23 +00:00
|
|
|
return .failure(.noSearchResultsFound)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2018-06-25 14:18:00 +00:00
|
|
|
// find out longest appName for formatting
|
|
|
|
var appNameMaxLength = 0
|
|
|
|
for result in results {
|
|
|
|
if let appName = result[ResultKeys.TrackName] as? String {
|
|
|
|
if appName.count > appNameMaxLength {
|
|
|
|
appNameMaxLength = appName.count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if appNameMaxLength > 50 {
|
|
|
|
appNameMaxLength = 50
|
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2016-04-14 16:14:06 +00:00
|
|
|
for result in results {
|
2016-04-14 19:27:07 +00:00
|
|
|
if let appName = result[ResultKeys.TrackName] as? String,
|
2018-06-25 14:18:00 +00:00
|
|
|
let appVersion = result[ResultKeys.Version] as? String,
|
|
|
|
let appId = result[ResultKeys.TrackId] as? Int,
|
|
|
|
let appPrice = result[ResultKeys.Price] as? Double {
|
|
|
|
|
|
|
|
// add empty spaces to app name that every app name has the same length
|
|
|
|
let countedAppName = String((appName + String(repeating: " ", count: appNameMaxLength)).prefix(appNameMaxLength))
|
|
|
|
|
|
|
|
if options.price {
|
|
|
|
print(String(format:"%12d %@ $%5.2f (%@)", appId, countedAppName, appPrice, appVersion))
|
|
|
|
} else {
|
|
|
|
print(String(format:"%12d %@ (%@)", appId, countedAppName, appVersion))
|
|
|
|
}
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
return .success(())
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2018-11-12 21:34:17 +00:00
|
|
|
|
|
|
|
/// Builds a URL to search the MAS for an app
|
|
|
|
///
|
|
|
|
/// - Parameter appName: Name of the app to find.
|
|
|
|
/// - Returns: String URL for app search or nil if the app name could not be encoded.
|
2016-09-17 12:58:38 +00:00
|
|
|
func searchURLString(_ appName: String) -> String? {
|
2018-11-12 21:34:17 +00:00
|
|
|
guard let urlEncodedAppName = appName.URLEncodedString else { return nil }
|
|
|
|
|
|
|
|
return "https://itunes.apple.com/search?entity=macSoftware&term=\(urlEncodedAppName)&attribute=allTrackTerm"
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct SearchOptions: OptionsProtocol {
|
2016-04-14 16:14:06 +00:00
|
|
|
let appName: String
|
2018-06-25 14:18:00 +00:00
|
|
|
let price: Bool
|
2018-10-14 21:35:48 +00:00
|
|
|
|
|
|
|
public static func create(_ appName: String) -> (_ price: Bool) -> SearchOptions {
|
2018-06-25 14:18:00 +00:00
|
|
|
return { price in
|
|
|
|
SearchOptions(appName: appName, price: price)
|
|
|
|
}
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-10-14 21:35:48 +00:00
|
|
|
|
|
|
|
public static func evaluate(_ m: CommandMode) -> Result<SearchOptions, CommandantError<MASError>> {
|
2016-04-14 16:14:06 +00:00
|
|
|
return create
|
|
|
|
<*> m <| Argument(usage: "the app name to search")
|
2018-06-25 14:18:00 +00:00
|
|
|
<*> m <| Option(key: "price", defaultValue: false, usage: "Show price of found apps")
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|