Add ability to search apps from the MAS via name

This commit is contained in:
Michael Schneider 2016-04-14 09:14:06 -07:00
parent 30f9bc98ae
commit e27b963d22
5 changed files with 128 additions and 12 deletions

View file

@ -15,6 +15,8 @@
319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 580DF177A1A8B1DF500CADA7 /* ResultType.swift */; }; 319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 580DF177A1A8B1DF500CADA7 /* ResultType.swift */; };
345960DE661C85EB2609263C /* ArgumentType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */; }; 345960DE661C85EB2609263C /* ArgumentType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */; };
3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AE1408CEE41BD4CD64491E3 /* Curry.swift */; }; 3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AE1408CEE41BD4CD64491E3 /* Curry.swift */; };
693A98991CBFFA760004D3B4 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A98981CBFFA760004D3B4 /* Search.swift */; };
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */; };
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; }; AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; };
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */; }; EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */; };
ED031A7C1B5127C00097692E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED031A7B1B5127C00097692E /* main.swift */; }; ED031A7C1B5127C00097692E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED031A7B1B5127C00097692E /* main.swift */; };
@ -53,6 +55,8 @@
326E4D331CCD66ADFE19CE39 /* Command.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Command.swift; path = Seeds/Commandant/Sources/Commandant/Command.swift; sourceTree = "<group>"; }; 326E4D331CCD66ADFE19CE39 /* Command.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Command.swift; path = Seeds/Commandant/Sources/Commandant/Command.swift; sourceTree = "<group>"; };
580DF177A1A8B1DF500CADA7 /* ResultType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ResultType.swift; path = Seeds/Result/Result/ResultType.swift; sourceTree = "<group>"; }; 580DF177A1A8B1DF500CADA7 /* ResultType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ResultType.swift; path = Seeds/Result/Result/ResultType.swift; sourceTree = "<group>"; };
62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ArgumentType.swift; path = Seeds/Commandant/Sources/Commandant/ArgumentType.swift; sourceTree = "<group>"; }; 62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ArgumentType.swift; path = Seeds/Commandant/Sources/Commandant/ArgumentType.swift; sourceTree = "<group>"; };
693A98981CBFFA760004D3B4 /* Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURLSession+Synchronous.swift"; sourceTree = "<group>"; };
8FDC2B8063EC231E28353D23 /* HelpCommand.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = HelpCommand.swift; path = Seeds/Commandant/Sources/Commandant/HelpCommand.swift; sourceTree = "<group>"; }; 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = HelpCommand.swift; path = Seeds/Commandant/Sources/Commandant/HelpCommand.swift; sourceTree = "<group>"; };
9257C5FABA335E5F060CB7F7 /* Result.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Seeds/Result/Result/Result.swift; sourceTree = "<group>"; }; 9257C5FABA335E5F060CB7F7 /* Result.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Seeds/Result/Result/Result.swift; sourceTree = "<group>"; };
9AE1408CEE41BD4CD64491E3 /* Curry.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Curry.swift; path = Seeds/Curry/Source/Curry.swift; sourceTree = "<group>"; }; 9AE1408CEE41BD4CD64491E3 /* Curry.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Curry.swift; path = Seeds/Curry/Source/Curry.swift; sourceTree = "<group>"; };
@ -183,6 +187,7 @@
children = ( children = (
ED0F238E1B87A54700AE40CD /* AppStore */, ED0F238E1B87A54700AE40CD /* AppStore */,
ED0F23801B87524700AE40CD /* Commands */, ED0F23801B87524700AE40CD /* Commands */,
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */,
ED0F238C1B8756E600AE40CD /* Error.swift */, ED0F238C1B8756E600AE40CD /* Error.swift */,
ED031A7B1B5127C00097692E /* main.swift */, ED031A7B1B5127C00097692E /* main.swift */,
EDEAA12C1B51CF8000F2FC3F /* mas-cli-Bridging-Header.h */, EDEAA12C1B51CF8000F2FC3F /* mas-cli-Bridging-Header.h */,
@ -198,6 +203,7 @@
ED0F237E1B87522400AE40CD /* Install.swift */, ED0F237E1B87522400AE40CD /* Install.swift */,
ED0F23821B87533A00AE40CD /* List.swift */, ED0F23821B87533A00AE40CD /* List.swift */,
ED0F23841B87536A00AE40CD /* Outdated.swift */, ED0F23841B87536A00AE40CD /* Outdated.swift */,
693A98981CBFFA760004D3B4 /* Search.swift */,
EDC90B641C70045E0019E396 /* SignIn.swift */, EDC90B641C70045E0019E396 /* SignIn.swift */,
EDE296521C700F4300554778 /* SignOut.swift */, EDE296521C700F4300554778 /* SignOut.swift */,
EDD3B3621C34709400B56B88 /* Upgrade.swift */, EDD3B3621C34709400B56B88 /* Upgrade.swift */,
@ -342,29 +348,31 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
ED0F23871B87537200AE40CD /* Account.swift in Sources */, 319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */,
F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */, 0EBF5CDD379D7462C3389536 /* Result.swift in Sources */,
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */,
345960DE661C85EB2609263C /* ArgumentType.swift in Sources */,
0C47E694564FCB59996690DD /* Command.swift in Sources */,
3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */, 3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */,
ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */, 15E27926A580EABEB1B218EF /* Switch.swift in Sources */,
ED0F238D1B8756E600AE40CD /* Error.swift in Sources */, 693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */,
3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */, 30EA893640B02CCF679F9C57 /* Option.swift in Sources */,
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */, EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */,
3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */,
0C47E694564FCB59996690DD /* Command.swift in Sources */,
345960DE661C85EB2609263C /* ArgumentType.swift in Sources */,
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */,
F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */,
ED0F23871B87537200AE40CD /* Account.swift in Sources */,
ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */,
693A98991CBFFA760004D3B4 /* Search.swift in Sources */,
ED0F238D1B8756E600AE40CD /* Error.swift in Sources */,
ED0F237F1B87522400AE40CD /* Install.swift in Sources */, ED0F237F1B87522400AE40CD /* Install.swift in Sources */,
ED0F23901B87A56F00AE40CD /* ISStoreAccount.swift in Sources */, ED0F23901B87A56F00AE40CD /* ISStoreAccount.swift in Sources */,
ED0F23831B87533A00AE40CD /* List.swift in Sources */, ED0F23831B87533A00AE40CD /* List.swift in Sources */,
ED031A7C1B5127C00097692E /* main.swift in Sources */, ED031A7C1B5127C00097692E /* main.swift in Sources */,
30EA893640B02CCF679F9C57 /* Option.swift in Sources */,
ED0F23851B87536A00AE40CD /* Outdated.swift in Sources */, ED0F23851B87536A00AE40CD /* Outdated.swift in Sources */,
ED0F23891B87543D00AE40CD /* PurchaseDownloadObserver.swift in Sources */, ED0F23891B87543D00AE40CD /* PurchaseDownloadObserver.swift in Sources */,
0EBF5CDD379D7462C3389536 /* Result.swift in Sources */,
319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */,
EDC90B651C70045E0019E396 /* SignIn.swift in Sources */, EDC90B651C70045E0019E396 /* SignIn.swift in Sources */,
EDE296531C700F4300554778 /* SignOut.swift in Sources */, EDE296531C700F4300554778 /* SignOut.swift in Sources */,
EDA3BE521B8B84AF00C18D70 /* SSPurchase.swift in Sources */, EDA3BE521B8B84AF00C18D70 /* SSPurchase.swift in Sources */,
15E27926A580EABEB1B218EF /* Switch.swift in Sources */,
EDD3B3631C34709400B56B88 /* Upgrade.swift in Sources */, EDD3B3631C34709400B56B88 /* Upgrade.swift in Sources */,
EDB6CE8C1BAEC3D400648B4D /* Version.swift in Sources */, EDB6CE8C1BAEC3D400648B4D /* Version.swift in Sources */,
); );

View file

@ -0,0 +1,54 @@
//
// Search.swift
// mas-cli
//
// Created by Michael Schneider on 4/14/16.
// Copyright © 2016 Andrew Naylor. All rights reserved.
//
struct SearchCommand: CommandType {
typealias Options = SearchOptions
let verb = "search"
let function = "Search for apps from the Mac App Store"
func run(options: Options) -> Result<(), MASError> {
let searchRequest = NSURLRequest(URL: NSURL(string: searchURLString(options.appName))!)
guard let searchData = NSURLSession.requestSynchronousData(searchRequest),
let searchJsonString = try? NSJSONSerialization.JSONObjectWithData(searchData, options: []) as! Dictionary<String, AnyObject> else {
return .Failure(MASError(code:.SearchError))
}
guard let resultCount = searchJsonString["resultCount"] as? Int where resultCount > 0,
let results = searchJsonString["results"] as? Array<Dictionary<String, AnyObject>> else {
print("No apps found")
return .Failure(MASError(code:.NoSearchResultsFound))
}
for result in results {
if let appName = result["trackName"] as? String,
appId = result["trackId"] as? Int {
print("\(String(appId)) \(appName)")
}
}
return .Success(())
}
func searchURLString(appName: String) -> String {
return "https://itunes.apple.com/search?entity=macSoftware&term=\(appName)&attribute=allTrackTerm"
}
}
struct SearchOptions: OptionsType {
let appName: String
static func create(appName: String) -> SearchOptions {
return SearchOptions(appName: appName)
}
static func evaluate(m: CommandMode) -> Result<SearchOptions, CommandantError<MASError>> {
return create
<*> m <| Argument(usage: "the app name to search")
}
}

View file

@ -19,6 +19,8 @@ public enum MASErrorCode: Int {
case DownloadFailed case DownloadFailed
case SignInError case SignInError
case AlreadySignedIn case AlreadySignedIn
case SearchError
case NoSearchResultsFound
var exitCode: Int32 { var exitCode: Int32 {
return Int32(self.rawValue) return Int32(self.rawValue)

View file

@ -0,0 +1,51 @@
//
// NSURLSession+Synchronous.swift
// mas-cli
//
// Created by Michael Schneider on 4/14/16.
// Copyright © 2016 Andrew Naylor. All rights reserved.
//
import Foundation
/// NSURLSession synchronous behavior
/// Particularly for playground sessions that need to run sequentially
public extension NSURLSession {
/// Return data from synchronous URL request
public static func requestSynchronousData(request: NSURLRequest) -> NSData? {
var data: NSData? = nil
let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(0)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {
taskData, _, error -> () in
data = taskData
if data == nil, let error = error {print(error)}
dispatch_semaphore_signal(semaphore);
})
task.resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
return data
}
/// Return data synchronous from specified endpoint
public static func requestSynchronousDataWithURLString(requestString: String) -> NSData? {
guard let url = NSURL(string:requestString) else {return nil}
let request = NSURLRequest(URL: url)
return NSURLSession.requestSynchronousData(request)
}
/// Return JSON synchronous from URL request
public static func requestSynchronousJSON(request: NSURLRequest) -> AnyObject? {
guard let data = NSURLSession.requestSynchronousData(request) else {return nil}
return try? NSJSONSerialization.JSONObjectWithData(data, options: [])
}
/// Return JSON synchronous from specified endpoint
public static func requestSynchronousJSONWithURLString(requestString: String) -> AnyObject? {
guard let url = NSURL(string: requestString) else {return nil}
let request = NSMutableURLRequest(URL:url)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
return NSURLSession.requestSynchronousJSON(request)
}
}

View file

@ -17,6 +17,7 @@ public struct StderrOutputStream: OutputStreamType {
let registry = CommandRegistry<MASError>() let registry = CommandRegistry<MASError>()
let helpCommand = HelpCommand(registry: registry) let helpCommand = HelpCommand(registry: registry)
registry.register(AccountCommand()) registry.register(AccountCommand())
registry.register(SearchCommand())
registry.register(InstallCommand()) registry.register(InstallCommand())
registry.register(ListCommand()) registry.register(ListCommand())
registry.register(OutdatedCommand()) registry.register(OutdatedCommand())