forked from sonofsky2010/TQT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TQTWeiboRequest.m
318 lines (297 loc) · 13.6 KB
/
TQTWeiboRequest.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// TQTWeiboRequest.m
// TQT
//
// Created by lishunnian on 11-7-13.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "SBJsonParser.h"
#import "TQTApiUrl.h"
#import "QWeiboRequest.h"
#import "TQTWeiboRequest.h"
#import "TQTWeiBo.h"
#import "TQTApiUrl.h"
#import "QOauthKey.h"
#import "TQTAppDelegate.h"
@implementation TQTWeiboRequest
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (int)postWeiboText:(NSString *)text
{
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"json" forKey:@"format"];
[dict setObject:text forKey:@"content"];
[dict setObject:[self myIP] forKey:@"clientip"];
[dict setObject:@"1" forKey:@"jing"];
[dict setObject:@"1" forKey:@"wei"];
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
NSString *response = [request syncRequestWithUrl:kAddWeiboUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:dict files:nil];
if (!response) {
return -1;
}
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *retDict = [parser objectWithString:response];
if (retDict && [[retDict objectForKey:@"ret"] intValue] == 0) {
return [[retDict objectForKey:@"errcode"] intValue];
}
return -1;
}
- (int)postWeiboText:(NSString *)text withPicture:(NSString *)picPath
{
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"json" forKey:@"format"];
[dict setObject:text forKey:@"content"];
[dict setObject:[self myIP] forKey:@"clientip"];
[dict setObject:@"" forKey:@"jing"];
[dict setObject:@"" forKey:@"wei"];
NSString *response = nil;
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
if (picPath) {
NSMutableDictionary *picDict = [NSMutableDictionary dictionary];
[picDict setObject:picPath forKey:@"pic"];
response = [request syncRequestWithUrl:kAddPicWeiboUrl httpMethod:@"POST" oauthKey:oauthKey parameters:dict files:picDict];
}
else
{
response = [request syncRequestWithUrl:kAddWeiboUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:dict files:nil];
}
if (!response) {
return -1;
}
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *retDict = [parser objectWithString:response];
if (retDict && [[retDict objectForKey:@"ret"] intValue] == 0) {
return [[retDict objectForKey:@"errcode"] intValue];
}
return -1;
}
- (NSMutableArray *)homeTimeLines
{
return [self homeTimeLinesWithType:0 OfTimeStamp:0];
}
- (NSMutableArray *)homeTimeLinesWithType:(int)type OfTimeStamp:(long)timeStamp
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:[NSString stringWithFormat:@"%d", type] forKey:@"pageflag"];
[parameters setObject:[NSString stringWithFormat:@"%ld", timeStamp] forKey:@"pagetime"];
NSString *response = [request syncRequestWithUrl:kHomeTimeLineUrl httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil];
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *dict = [parser objectWithString:response];
if (dict && [[dict objectForKey:@"msg"] isEqualToString:@"ok"])
{
NSArray *dicts = [[dict objectForKey:@"data"] objectForKey:@"info"];
NSMutableArray *weibos = [NSMutableArray arrayWithCapacity:[dicts count]];
for (id aDict in dicts)
{
[weibos addObject:[TQTWeiBo weiBoFromDict:aDict]];
}
return weibos;
}
return nil;
}
- (NSMutableArray *)publicTimeLinesWithType:(int)type ofTimeStamp:(long)timeStamp reqNum:(int)number
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:[NSString stringWithFormat:@"%ld", timeStamp] forKey:@"pos"];
[parameters setObject:[NSString stringWithFormat:@"%d", number] forKey:@"reqnum"];
NSString *response = [request syncRequestWithUrl:kPublicTimeLineUrl httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil];
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *dict = [parser objectWithString:response];
if (dict && [[dict objectForKey:@"msg"] isEqualToString:@"ok"])
{
NSArray *dicts = [[dict objectForKey:@"data"] objectForKey:@"info"];
NSMutableArray *weibos = [NSMutableArray arrayWithCapacity:[dicts count]];
for (id aDict in dicts)
{
[weibos addObject:[TQTWeiBo weiBoFromDict:aDict]];
}
return weibos;
}
return nil;
}
- (int)reAdd:(NSString *)content weiboId:(long long)weiboId
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:content forKey:@"content"];
[parameters setObject:[self myIP] forKey:@"clientip"];
[parameters setObject:[NSString stringWithFormat:@"%lld", weiboId] forKey:@"reid"];
NSString *response = [request syncRequestWithUrl:kReAddWeiboUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:parameters
files:nil];
if (!response) {
return -1;
}
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *retDict = [parser objectWithString:response];
if (retDict && [[retDict objectForKey:@"ret"] intValue] == 0) {
return [[retDict objectForKey:@"errcode"] intValue];
}
return -1;
}
- (int)reply:(NSString *)content weiboId:(long long)weiboId
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:content forKey:@"content"];
[parameters setObject:[self myIP] forKey:@"clientip"];
[parameters setObject:[NSString stringWithFormat:@"%lld", weiboId] forKey:@"reid"];
NSString *response = [request syncRequestWithUrl:kReplyWeiboUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:parameters
files:nil];
if (!response) {
return -1;
}
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *retDict = [parser objectWithString:response];
if (retDict && [[retDict objectForKey:@"ret"] intValue] == 0) {
return [[retDict objectForKey:@"errcode"] intValue];
}
return -1;
}
- (int)comment:(NSString *)content weiboId:(long long)weiboId
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:content forKey:@"content"];
[parameters setObject:[self myIP] forKey:@"clientip"];
[parameters setObject:[NSString stringWithFormat:@"%lld", weiboId] forKey:@"reid"];
NSString *response = [request syncRequestWithUrl:kCommentWeiboUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:parameters
files:nil];
if (!response) {
return -1;
}
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *retDict = [parser objectWithString:response];
if (retDict && [[retDict objectForKey:@"ret"] intValue] == 0) {
return [[retDict objectForKey:@"errcode"] intValue];
}
return -1;
}
- (NSMutableArray *)replyListOfWeiboId:(long long) weiboId
type:(int)flag
pageFlag:(int)pageFlag
pageTime:(long)pageTime
reqNum:(int)reqNum
tId:(long long)tId
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:[NSString stringWithFormat:@"%lld", weiboId] forKey:@"rootid"];
[parameters setObject:[NSString stringWithFormat:@"%d", flag] forKey:@"flag"];
[parameters setObject:[NSString stringWithFormat:@"%d", pageFlag] forKey:@"pageflag"];
[parameters setObject:[NSString stringWithFormat:@"%ld", pageTime] forKey:@"pagetime"];
[parameters setObject:[NSString stringWithFormat:@"%d", reqNum] forKey:@"reqnum"];
[parameters setObject:[NSString stringWithFormat:@"%lld", tId] forKey:@"twitterid"];
NSString *response = [request syncRequestWithUrl:kReListUrl
httpMethod:@"GET"
oauthKey:oauthKey
parameters:parameters
files:nil];
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *dict = [parser objectWithString:response];
if (dict && [[dict objectForKey:@"msg"] isEqualToString:@"ok"])
{
NSArray *dicts = [[dict objectForKey:@"data"] objectForKey:@"info"];
NSMutableArray *weibos = [NSMutableArray arrayWithCapacity:[dicts count]];
for (id aDict in dicts)
{
[weibos addObject:[TQTWeiBo weiBoFromDict:aDict]];
}
return weibos;
}
return nil;
}
- (NSMutableArray *)publicTimeLines
{
return [self publicTimeLinesWithType:0 ofTimeStamp:0 reqNum:20];
}
- (NSMutableArray *)userTimeLinesOfName:(NSString *)userName
pageFlag:(int)pageFlag
pageTime:(long)pageTime
reqNum:(int)reqNum
lastId:(long long)lastId
pullType:(int)pullType
contentType:(int)contentType
accessLevel:(int)level
{
QOauthKey *oauthKey = [(TQTAppDelegate *)[[NSApplication sharedApplication] delegate] oauthKey];
QWeiboRequest *request = [[[QWeiboRequest alloc] init] autorelease];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"json" forKey:@"format"];
[parameters setObject:[NSString stringWithFormat:@"%d", pageFlag] forKey:@"pageflag"];
[parameters setObject:[NSString stringWithFormat:@"%ld", pageTime] forKey:@"pagetime"];
[parameters setObject:[NSString stringWithFormat:@"%d", reqNum] forKey:@"reqnum"];
[parameters setObject:[NSString stringWithFormat:@"%lld", lastId] forKey:@"lastid"];
[parameters setObject:[NSString stringWithFormat:@"%d", pullType] forKey:@"type"];
[parameters setObject:userName forKey:@"name"];
[parameters setObject:[NSString stringWithFormat:@"%d", contentType] forKey:@"contenttype"];
[parameters setObject:[NSString stringWithFormat:@"%d", level] forKey:@"accesslevel"];
NSString *response = [request syncRequestWithUrl:kUserTimeLineUrl
httpMethod:@"POST"
oauthKey:oauthKey
parameters:parameters
files:nil];
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSMutableDictionary *dict = [parser objectWithString:response];
if (dict && [[dict objectForKey:@"msg"] isEqualToString:@"ok"])
{
NSArray *dicts = [[dict objectForKey:@"data"] objectForKey:@"info"];
NSMutableArray *weibos = [NSMutableArray arrayWithCapacity:[dicts count]];
for (id aDict in dicts)
{
[weibos addObject:[TQTWeiBo weiBoFromDict:aDict]];
}
return weibos;
}
return nil;
}
- (NSString *)myIP
{
NSString *result = @"127.0.0.1";
NSHost *currentHost = [NSHost currentHost];
NSString *ad = [currentHost address];
if (ad)
return ad;
return result;
}
@end