Skip to content

Instantly share code, notes, and snippets.

@yuichirokato
Created November 24, 2015 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuichirokato/fa15a450bc2cc3672b62 to your computer and use it in GitHub Desktop.
Save yuichirokato/fa15a450bc2cc3672b62 to your computer and use it in GitHub Desktop.
#!/usr/bin/env xcrun swift -F Carthage/Build/Mac/
import Foundation
import Alamofire
import SwiftScriptRunner
import Kanna
import Bond
let scrapeUrl = "http://gamefavo.com/db/splatoon/weapon-list/"
let html = Observable(HTML(html: NSData(), encoding: NSUTF8StringEncoding))
func getHtml() {
Alamofire.request(.GET, scrapeUrl).responseData { response in
if let data = response.data {
html.next(HTML(html: data, encoding: NSUTF8StringEncoding))
}
}
}
let runner = SwiftScriptRunner()
runner.lock()
getHtml()
html.observe { html in
let tableCSS = "table"
let css = "h2.m30"
let jenres = html?.css(css).map { $0.text ?? "" }.filter { $0 != "" } ?? []
let bukiList = html?.css(tableCSS).map { node -> [String] in
node.css("tr").map { $0.css("td").at(2)?.text ?? "" }.filter { $0 != "" }
}.filter { $0.count > 0 } ?? [[]]
let result = zip(jenres, bukiList).map { (jenre, buki) in
return [jenre: buki]
}
print("\(jenres)")
print("\(result)")
if html != nil {
runner.unlock()
}
}
runner.wait()
github "Alamofire/Alamofire" ~> 3.0
github "mgrebenets/SwiftScriptRunner"
github "SwiftBond/Bond"
github "tid-kijyun/Kanna" >= 0.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment