Last active
August 29, 2015 14:15
-
-
Save laiso/00d3e75f0fd784b2d45a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ViewController.swift | |
import UIKit | |
import Alamofire | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let d = Downloader(path: "/tmp/qiitags") | |
d.download(1, endpoint: 100) | |
} | |
} | |
struct Downloader { | |
var path: String | |
init(path: String){ | |
self.path = path | |
} | |
func download(start: Int, endpoint: Int) -> Void { | |
if start > endpoint { | |
return | |
} | |
let p = String(start) | |
Alamofire | |
.request(.GET, "https://qiita.com/api/v2/tags", parameters: ["page": p]) | |
.response { (request, response, data, error) in | |
assert(error == nil) | |
let json = NSString(data: data as NSData, encoding: NSUTF8StringEncoding) | |
let path = "\(self.path)/\(p).json" | |
let ret = json?.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil) as Bool! | |
assert(ret) | |
NSThread.sleepForTimeInterval(1) | |
self.download(start+1, endpoint: endpoint) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment