Avoid Weibo API 30% limits

Use resursion to get more 90% data, still can’t upto 100%.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

import Foundation
import Alamofire
import SwiftyJSON
import Alamofire-SwiftyJSON
import RealmSwift

// MARK: - friendships

// Weibo 30% more or less limits how to aovid this.
// Thus count each response json user nubmer, to calculate true next cursor.
// But Weibo return much fewer than 30%, fanially we got 93% data.
// http://open.weibo.com/wiki/2/friendships/friends

static func allFriends(uid uid: Int?, cursor: Int?, completionHandler: (stateCode: WeiboServiceError, error: String?, nextCursor: Int?) -> Void) {

let pageNumber: Int = 200
var parameters: [String: AnyObject] = ["count": pageNumber]

AccountManager.currentAcvtiveWeiboAccount { (stateCode, error, weiboAccount) -> Void in
if let weiboAccount = weiboAccount {
parameters.updateValue(weiboAccount.accessToken, forKey: "access_token")
parameters.updateValue(weiboAccount.accountId, forKey: "uid")
}
}

if let uid = uid {
parameters.updateValue(uid, forKey: "uid")
}
if let cursor = cursor {
parameters.updateValue(cursor, forKey: "cursor")
}
Alamofire.request(.GET, WeiboApi.FriendshipsFriends, parameters: parameters)
.responseSwiftyJSON({ (request, response, json, error) in
if error != nil {
completionHandler(stateCode: .Error, error: "Please check internet connection.", nextCursor: nil)
} else if json["error"] != JSON.null {
completionHandler(stateCode: .Error, error: json["error"].stringValue, nextCursor: nil)
} else {
WeiboStore.friendsJSONToUserAndSave(json: json, completionHandler: { (stateCode, error, count) -> Void in
if stateCode == .Error {
completionHandler(stateCode: .Error, error: error, nextCursor: nil)
} else {

let previousCursor = json["previous_cursor"].intValue
let nextCursor = previousCursor + count!

if count > 0 {

self.allFriends(uid: uid, cursor: nextCursor, completionHandler: { (stateCode, error, nextCursor) -> Void in
})
} else {

completionHandler(stateCode: .Success, error: nil, nextCursor: nextCursor)
}
}
})
}
})
}


// Model
static func friendsJSONToUserAndSave(json json: JSON, completionHandler: (stateCode: WeiboStoreError, error: String?, count: Int?) -> Void) {

var count = 0

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) {

let realm = try! Realm()
realm.beginWrite()

if json["users"] != JSON.null {

for (_, subJson): (String, JSON) in json["users"] {
let friend = self.JSONToUserModel(subJson)
friend.isFriend = true
realm.add(friend, update: true)

count += 1
}
}

do {
try realm.commitWrite()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(stateCode: .Success, error: nil, count: count)
})
} catch {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(stateCode: .Error, error: "Realm Database save error.", count: nil)
})
}
}
}