Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Created August 4, 2016 13:35
Show Gist options
  • Save kiliankoe/368c82a788888a51085584aa49e15e87 to your computer and use it in GitHub Desktop.
Save kiliankoe/368c82a788888a51085584aa49e15e87 to your computer and use it in GitHub Desktop.
#!/usr/bin/env xcrun swift
import Foundation
struct Pod {
let source: String
let name: String
let version: String
let platforms: String
init(dictionary: [String: AnyObject]) {
self.source = dictionary["link"] as? String ?? ""
self.name = dictionary["id"] as? String ?? ""
self.version = dictionary["version"] as? String ?? ""
self.platforms = (dictionary["platforms"] as? [String])?.joined(separator: ", ") ?? ""
}
var xml: String {
return "<item arg=\"\(source)\"><title>\(name) (\(version))</title><subtitle>\(platforms)</subtitle></item>"
}
}
let searchterm = Process.arguments.dropFirst().joined(separator: "+")
let req = URLRequest(url: URL(string: "https://search.cocoapods.org/api/v1/pods.picky.hash.json?query=\(searchterm)&ids=20&offset=0&sort=quality")!)
let semaphore = DispatchSemaphore(value: 0)
URLSession.shared.dataTask(with: req) {
data, response, error in
defer {
semaphore.signal()
}
guard let data = data else {
return
}
guard let json = try? JSONSerialization.jsonObject(with: data, options: []),
let dic = json as? [String: AnyObject]
else {
return
}
guard let arr = dic["allocations"] as? [AnyObject] else {
return
}
guard let podsJson = (arr.first as? [AnyObject])?[5] as? [[String: AnyObject]] else {
return
}
let pods = podsJson.map(Pod.init).map { $0.xml }
print("<?xml version=\"1.0\"?><items>")
print(pods.reduce("") {$0+$1})
print("</items></xml>")
}.resume()
semaphore.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment