/
TwitterFriendsGetter.m
170 lines (126 loc) · 3.89 KB
/
TwitterFriendsGetter.m
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// TwitterFriendsGetter.m
// NowPlayingFriends
//
// Created by Hiroe Shin on 11/01/12.
// Copyright 2011 hiroe_orz17. All rights reserved.
//
#import "JSON/JSON.h"
#import "NowPlayingFriendsAppDelegate.h"
#import "OAuthConsumer/OAConsumer.h"
#import "OAuthConsumer/OADataFetcher.h"
#import "OAuthConsumer/OAMutableURLRequest.h"
#import "OAuthConsumer/OARequestParameter.h"
#import "TwitterClient.h"
#import "TwitterFriendsGetter.h"
@interface TwitterFriendsGetter (Local)
- (NSString *)lastName;
@end
@implementation TwitterFriendsGetter
@dynamic appDelegate;
@synthesize nextCursor;
- (void)dealloc {
[nextCursor release];
[super dealloc];
}
#pragma mark -
#pragma Initilizer
- (id)init {
self = [super init];
if (self != nil) {
trycount = 3;
}
return self;
}
#pragma mark -
#pragma mark URLConnection Delegate Methods
- (void)ticket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSLog(@"didFinishWithData(getting friends list)");
trycount = 3;
NSString *dataString = [[NSString alloc]
initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *result = [dataString JSONValue];
[dataString release];
if ([result objectForKey:@"error"] != nil) {
NSLog(@"Error while friends getting");
return;
}
NSNumber *next_cursor = [result objectForKey:@"next_cursor"];
NSArray *users = [result objectForKey:@"users"];
NSMutableArray *array = nil;
NSString *tmpFilePath = [self tmpFilePath];
BOOL knownAddressFlag = NO;
NSString *lastName = [self lastName];
if ([[NSFileManager defaultManager] fileExistsAtPath:tmpFilePath]) {
array = [[NSMutableArray alloc] initWithContentsOfFile:tmpFilePath];
} else {
array = [[NSMutableArray alloc] init];
}
for (NSDictionary *user in users) {
NSString *screen_name = [user objectForKey:@"screen_name"];
if ([screen_name compare:lastName] == NSOrderedSame) {
knownAddressFlag = YES;
}
if (!knownAddressFlag) {
[array addObject:screen_name];
}
}
[array writeToFile:tmpFilePath atomically:YES];
[array release];
if ([next_cursor integerValue] == 0 || knownAddressFlag) {
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:tmpFilePath
toPath:[self filePath]
error:&error];
NSLog(@"friends getter: getting friends is finished.");
} else {
NSLog(@"friends getter: next cursor: %@", next_cursor);
TwitterClient *client = [[[TwitterClient alloc] init] autorelease];
self.nextCursor = next_cursor;
[client saveFriendsWithCursor:next_cursor];
}
[self autorelease];
}
- (void)ticket:(OAServiceTicket *)ticket didFailWithError:(NSError *)error {
NSLog(@"didFailWithError(getting friends list)");
if (trycount > 0) {
trycount--;
TwitterClient *client = [[[TwitterClient alloc] init] autorelease];
[client saveFriendsWithCursor:nextCursor];
[self autorelease];
}
}
#pragma mark
- (NSString *)tmpFilePath {
return [NSString stringWithFormat:@"%@.tmp", [self filePath]];
}
- (NSString *)filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath =
[documentsDirectory stringByAppendingPathComponent:kFriendsFileName];
return filePath;
}
#pragma mark -
#pragma mark Local Methods
- (NSString *)lastName {
NSArray *array = nil;
NSString *filePath = [self filePath];
NSString *lastName = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
if (array == nil || [array count] == 0) {
lastName = nil;
} else {
lastName = [array objectAtIndex:0];
}
} else {
lastName = nil;
}
return lastName;
}
- (NowPlayingFriendsAppDelegate *)appDelegate {
return [[UIApplication sharedApplication] delegate];
}
@end