forked from natesymer/FHSTwitterEngine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FHSTwitterEngine.m
executable file
·2030 lines (1527 loc) · 73.7 KB
/
FHSTwitterEngine.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// FHSTwitterEngine.m
// FHSTwitterEngine
//
// Created by Nathaniel Symer on 8/22/12.
// Copyright (C) 2012 Nathaniel Symer.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "FHSTwitterEngine.h"
#import <QuartzCore/QuartzCore.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <CommonCrypto/CommonHMAC.h>
#import <objc/runtime.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <ifaddrs.h>
static NSString * const newPinJS = @"var d = document.getElementById('oauth-pin'); if (d == null) d = document.getElementById('oauth_pin'); if (d) { var d2 = d.getElementsByTagName('code'); if (d2.length > 0) d2[0].innerHTML; }";
static NSString * const oldPinJS = @"var d = document.getElementById('oauth-pin'); if (d == null) d = document.getElementById('oauth_pin'); if (d) d = d.innerHTML; d;";
NSString * const FHSProfileBackgroundColorKey = @"profile_background_color";
NSString * const FHSProfileLinkColorKey = @"profile_link_color";
NSString * const FHSProfileSidebarBorderColorKey = @"profile_sidebar_border_color";
NSString * const FHSProfileSidebarFillColorKey = @"profile_sidebar_fill_color";
NSString * const FHSProfileTextColorKey = @"profile_text_color";
NSString * const FHSProfileNameKey = @"name";
NSString * const FHSProfileURLKey = @"url";
NSString * const FHSProfileLocationKey = @"location";
NSString * const FHSProfileDescriptionKey = @"description";
NSString * const FHSErrorDomain = @"FHSErrorDomain";
static NSString * const authBlockKey = @"FHSTwitterEngineOAuthCompletion";
static NSString * const url_search_tweets = @"https://api.twitter.com/1.1/search/tweets.json";
static NSString * const url_users_search = @"https://api.twitter.com/1.1/users/search.json";
static NSString * const url_users_show = @"https://api.twitter.com/1.1/users/show.json";
static NSString * const url_users_report_spam = @"https://api.twitter.com/1.1/users/report_spam.json";
static NSString * const url_users_lookup = @"https://api.twitter.com/1.1/users/lookup.json";
static NSString * const url_lists_create = @"https://api.twitter.com/1.1/lists/create.json";
static NSString * const url_lists_show = @"https://api.twitter.com/1.1/lists/show.json";
static NSString * const url_lists_update = @"https://api.twitter.com/1.1/lists/update.json";
static NSString * const url_lists_members = @"https://api.twitter.com/1.1/lists/members.json";
static NSString * const url_lists_members_destroy_all = @"https://api.twitter.com/1.1/lists/members/destroy_all.json";
static NSString * const url_lists_members_create_all = @"https://api.twitter.com/1.1/lists/members/create_all.json";
static NSString * const url_lists_statuses = @"https://api.twitter.com/1.1/lists/statuses.json";
static NSString * const url_lists_list = @"https://api.twitter.com/1.1/lists/list.json";
static NSString * const url_statuses_home_timeline = @"https://api.twitter.com/1.1/statuses/home_timeline.json";
static NSString * const url_statuses_update = @"https://api.twitter.com/1.1/statuses/update.json";
static NSString * const url_statuses_retweets_of_me = @"https://api.twitter.com/1.1/statuses/retweets_of_me.json";
static NSString * const url_statuses_user_timeline = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
static NSString * const url_statuses_metions_timeline = @"https://api.twitter.com/1.1/statuses/mentions_timeline.json";
static NSString * const url_statuses_update_with_media = @"https://api.twitter.com/1.1/statuses/update_with_media.json";
static NSString * const url_statuses_destroy = @"https://api.twitter.com/1.1/statuses/destroy.json";
static NSString * const url_statuses_show = @"https://api.twitter.com/1.1/statuses/show.json";
static NSString * const url_blocks_exists = @"https://api.twitter.com/1.1/blocks/exists.json";
static NSString * const url_blocks_blocking = @"https://api.twitter.com/1.1/blocks/blocking.json";
static NSString * const url_blocks_blocking_ids = @"https://api.twitter.com/1.1/blocks/blocking/ids.json";
static NSString * const url_blocks_destroy = @"https://api.twitter.com/1.1/blocks/destroy.json";
static NSString * const url_blocks_create = @"https://api.twitter.com/1.1/blocks/create.json";
static NSString * const url_help_languages = @"https://api.twitter.com/1.1/help/languages.json";
static NSString * const url_help_configuration = @"https://api.twitter.com/1.1/help/configuration.json";
static NSString * const url_help_privacy = @"https://api.twitter.com/1.1/help/privacy.json";
static NSString * const url_help_tos = @"https://api.twitter.com/1.1/help/tos.json";
static NSString * const url_help_test = @"https://api.twitter.com/1.1/help/test.json";
static NSString * const url_direct_messages_show = @"https://api.twitter.com/1.1/direct_messages/show.json";
static NSString * const url_direct_messages_new = @"https://api.twitter.com/1.1/direct_messages/new.json";
static NSString * const url_direct_messages_sent = @"https://api.twitter.com/1.1/direct_messages/sent.json";
static NSString * const url_direct_messages_destroy = @"https://api.twitter.com/1.1/direct_messages/destroy.json";
static NSString * const url_direct_messages = @"https://api.twitter.com/1.1/direct_messages.json";
static NSString * const url_friendships_no_retweets_ids = @"https://api.twitter.com/1.1/friendships/no_retweets/ids.json";
static NSString * const url_friendships_update = @"https://api.twitter.com/1.1/friendships/update.json";
static NSString * const url_friendships_outgoing = @"https://api.twitter.com/1.1/friendships/outgoing.json";
static NSString * const url_friendships_incoming = @"https://api.twitter.com/1.1/friendships/incoming.json";
static NSString * const url_friendships_lookup = @"https://api.twitter.com/1.1/friendships/lookup.json";
static NSString * const url_friendships_destroy = @"https://api.twitter.com/1.1/friendships/destroy.json";
static NSString * const url_friendships_create = @"https://api.twitter.com/1.1/friendships/create.json";
static NSString * const url_account_verify_credentials = @"https://api.twitter.com/1.1/account/verify_credentials.json";
static NSString * const url_account_update_profile_colors = @"https://api.twitter.com/1.1/account/update_profile_colors.json";
static NSString * const url_account_update_profile_background_image = @"https://api.twitter.com/1.1/account/update_profile_background_image.json";
static NSString * const url_account_update_profile_image = @"https://api.twitter.com/1.1/account/update_profile_image.json";
static NSString * const url_account_settings = @"https://api.twitter.com/1.1/account/settings.json";
static NSString * const url_account_update_profile = @"https://api.twitter.com/1.1/account/update_profile.json";
static NSString * const url_favorites_list = @"https://api.twitter.com/1.1/favorites/list.json";
static NSString * const url_favorites_create = @"https://api.twitter.com/1.1/favorites/create.json";
static NSString * const url_favorites_destroy = @"https://api.twitter.com/1.1/favorites/destroy.json";
static NSString * const url_application_rate_limit_status = @"https://api.twitter.com/1.1/application/rate_limit_status.json";
static NSString * const url_followers_ids = @"https://api.twitter.com/1.1/followers/ids.json";
static NSString * const url_followers_list = @"https://api.twitter.com/1.1/followers/list.json";
static NSString * const url_friends_ids = @"https://api.twitter.com/1.1/friends/ids.json";
static NSString * const url_friends_list = @"https://api.twitter.com/1.1/friends/list.json";
NSString * fhs_url_remove_params(NSURL *url) {
if (url.absoluteString.length == 0) {
return nil;
}
NSArray *parts = [url.absoluteString componentsSeparatedByString:@"?"];
return (parts.count == 0)?nil:parts[0];
}
id removeNull(id rootObject) {
if ([rootObject isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *sanitizedDictionary = [NSMutableDictionary dictionaryWithDictionary:rootObject];
[rootObject enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id sanitized = removeNull(obj);
if (!sanitized) {
[sanitizedDictionary setObject:@"" forKey:key];
} else {
[sanitizedDictionary setObject:sanitized forKey:key];
}
}];
return [NSMutableDictionary dictionaryWithDictionary:sanitizedDictionary];
}
if ([rootObject isKindOfClass:[NSArray class]]) {
NSMutableArray *sanitizedArray = [NSMutableArray arrayWithArray:rootObject];
[rootObject enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id sanitized = removeNull(obj);
if (!sanitized) {
[sanitizedArray replaceObjectAtIndex:[sanitizedArray indexOfObject:obj] withObject:@""];
} else {
[sanitizedArray replaceObjectAtIndex:[sanitizedArray indexOfObject:obj] withObject:sanitized];
}
}];
return [NSMutableArray arrayWithArray:sanitizedArray];
}
if ([rootObject isKindOfClass:[NSNull class]]) {
return (id)nil;
} else {
return rootObject;
}
}
@interface FHSConsumer : NSObject
@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) NSString *secret;
+ (FHSConsumer *)consumerWithKey:(NSString *)key secret:(NSString *)secret;
@end
@implementation FHSConsumer
+ (FHSConsumer *)consumerWithKey:(NSString *)key secret:(NSString *)secret {
return [[[self class]alloc]initWithKey:key secret:secret];
}
- (instancetype)initWithKey:(NSString *)key secret:(NSString *)secret {
self = [super init];
if (self) {
self.key = key;
self.secret = secret;
}
return self;
}
@end
@implementation FHSToken
+ (FHSToken *)tokenWithHTTPResponseBody:(NSString *)body {
return [[[self class]alloc]initWithHTTPResponseBody:body];
}
- (id)initWithHTTPResponseBody:(NSString *)body {
self = [super init];
if (self) {
if (body.length > 0) {
NSArray *pairs = [body componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
NSArray *elements = [pair componentsSeparatedByString:@"="];
if (elements.count > 1) {
NSString *field = elements[0];
NSString *value = elements[1];
if ([field isEqualToString:@"oauth_token"]) {
self.key = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else if ([field isEqualToString:@"oauth_token_secret"]) {
self.secret = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
}
}
}
}
return self;
}
@end
@interface FHSTwitterEngineController : UIViewController <UIWebViewDelegate>
@property (nonatomic, strong) UINavigationBar *navBar;
@property (nonatomic, strong) UIView *blockerView;
@property (nonatomic, strong) UIToolbar *pinCopyBar;
@property (nonatomic, strong) UIWebView *theWebView;
@property (nonatomic, strong) FHSToken *requestToken;
- (NSString *)locatePin;
- (void)showPinCopyPrompt;
@end
@interface FHSTwitterEngine ()
// Login stuff
- (NSString *)getRequestTokenString;
// General Get request sender
- (id)sendRequest:(NSURLRequest *)request;
// These are here to obfuscate them from prying eyes
@property (strong, nonatomic) FHSConsumer *consumer;
@property (assign, nonatomic) BOOL shouldClearConsumer;
@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@end
@implementation NSError (FHSTwitterEngine)
+ (NSError *)badRequestError {
return [NSError errorWithDomain:FHSErrorDomain code:400 userInfo:@{NSLocalizedDescriptionKey:@"The request has missing or malformed parameters."}];
}
+ (NSError *)noDataError {
return [NSError errorWithDomain:FHSErrorDomain code:204 userInfo:@{NSLocalizedDescriptionKey:@"The request did not return any content."}];
}
+ (NSError *)imageTooLargeError {
return [NSError errorWithDomain:FHSErrorDomain code:422 userInfo:@{NSLocalizedDescriptionKey:@"The image you are trying to upload is too large."}];
}
@end
@implementation NSString (FHSTwitterEngine)
- (NSString *)fhs_URLEncode {
CFStringRef url = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
return (__bridge NSString *)url;
}
- (NSString *)fhs_trimForTwitter {
NSString *string = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return (string.length > 140)?[string substringToIndex:140]:string;
}
- (NSString *)fhs_stringWithRange:(NSRange)range {
return [[self substringFromIndex:range.location]substringToIndex:range.length];
}
+ (NSString *)fhs_UUID {
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 6.0f) {
return [[NSUUID UUID]UUIDString];
} else {
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef string = CFUUIDCreateString(kCFAllocatorDefault, theUUID);
CFRelease(theUUID);
NSString *uuid = [NSString stringWithString:(__bridge NSString *)string];
CFRelease(string);
return uuid;
}
}
- (BOOL)fhs_isNumeric {
const char *raw = (const char *)[self UTF8String];
for (int i = 0; i < strlen(raw); i++) {
if (raw[i] < '0' || raw[i] > '9') {
return NO;
}
}
return YES;
}
@end
@implementation FHSTwitterEngine
- (id)listFollowersForUser:(NSString *)user isID:(BOOL)isID withCursor:(NSString *)cursor {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_friends_list];
return [self sendGETRequestForURL:baseURL andParams:@{@"skip_status":@"true", @"include_entities":(_includeEntities?@"true":@"false"), (isID?@"user_id":@"screen_name"):user, @"cursor":cursor }];
}
- (id)listFriendsForUser:(NSString *)user isID:(BOOL)isID withCursor:(NSString *)cursor {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_friends_list];
return [self sendGETRequestForURL:baseURL andParams:@{@"skip_status":@"true", @"include_entities":(_includeEntities?@"true":@"false"), (isID?@"user_id":@"screen_name"):user, @"cursor":cursor }];
}
- (id)searchUsersWithQuery:(NSString *)q andCount:(int)count {
if (count == 0) {
return nil;
}
if (q.length == 0) {
return [NSError badRequestError];
}
if (q.length > 1000) {
q = [q substringToIndex:1000];
}
NSURL *baseURL = [NSURL URLWithString:url_users_search];
return [self sendGETRequestForURL:baseURL andParams:@{ @"include_entities":(_includeEntities?@"true":@"false"), @"count":@(count).stringValue, @"q":q }];
}
- (id)searchTweetsWithQuery:(NSString *)q count:(int)count resultType:(FHSTwitterEngineResultType)resultType unil:(NSDate *)untilDate sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
if (count == 0) {
return nil;
}
if (q.length == 0) {
return [NSError badRequestError];
}
if (q.length > 1000) {
q = [q substringToIndex:1000];
}
NSURL *baseURL = [NSURL URLWithString:url_search_tweets];
NSMutableDictionary *params = [@{ @"include_entities":(_includeEntities?@"true":@"false"), @"count":@(count).stringValue, @"q":q } mutableCopy];
[_dateFormatter setDateFormat:@"YYYY-MM-DD"];
params[@"until"] = [_dateFormatter stringFromDate:untilDate];
[_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZZ yyyy"];
if (resultType == FHSTwitterEngineResultTypeMixed) {
params[@"result_type"] = @"mixed";
} else if (resultType == FHSTwitterEngineResultTypeRecent) {
params[@"result_type"] = @"recent";
} else if (resultType == FHSTwitterEngineResultTypePopular) {
params[@"result_type"] = @"popular";
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (NSError *)createListWithName:(NSString *)name isPrivate:(BOOL)isPrivate description:(NSString *)description {
if (name.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_create];
NSMutableDictionary *params = [@{@"name": name, @"mode":isPrivate?@"private":@"public"} mutableCopy];
if (description.length > 0) {
params[@"description"] = description;
}
return [self sendPOSTRequestForURL:baseURL andParams:params];
}
- (id)getListWithID:(NSString *)listID {
if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_show];
return [self sendGETRequestForURL:baseURL andParams:@{ @"list_id": listID }];
}
- (NSError *)updateListWithID:(NSString *)listID name:(NSString *)name {
if (listID.length == 0) {
return [NSError badRequestError];
} else if (name.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_update];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"list_id": listID, @"name": name}];
}
- (NSError *)updateListWithID:(NSString *)listID description:(NSString *)description {
if (listID.length == 0) {
return [NSError badRequestError];
}
if (description == nil) {
description = @"";
}
NSURL *baseURL = [NSURL URLWithString:url_lists_update];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"list_id": listID, @"description": description}];
}
- (NSError *)updateListWithID:(NSString *)listID mode:(BOOL)isPrivate {
if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_update];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"list_id": listID, @"mode": isPrivate?@"private":@"public"}];
}
- (NSError *)updateListWithID:(NSString *)listID name:(NSString *)name description:(NSString *)description mode:(BOOL)isPrivate {
if (listID.length == 0) {
return [NSError badRequestError];
} else if (name.length == 0) {
return [NSError badRequestError];
}
if (description == nil) {
description = @"";
}
NSURL *baseURL = [NSURL URLWithString:url_lists_update];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"list_id": listID, @"name": name, @"description": description, @"mode": isPrivate?@"private":@"public"}];
}
- (id)listUsersInListWithID:(NSString *)listID {
if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_members];
return [self sendGETRequestForURL:baseURL andParams:@{ @"list_id": listID }];
}
- (NSError *)removeUsersFromListWithID:(NSString *)listID users:(NSArray *)users {
if (users.count > 100 || users.count == 0) {
return [NSError badRequestError];
} else if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_members_destroy_all];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"screen_name": [users componentsJoinedByString:@","]}];
}
- (NSError *)addUsersToListWithID:(NSString *)listID users:(NSArray *)users {
if (users.count > 100 || users.count == 0) {
return [NSError badRequestError];
} else if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_members_create_all];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"screen_name": [users componentsJoinedByString:@","]}];
}
- (id)getTimelineForListWithID:(NSString *)listID count:(int)count {
return [self getTimelineForListWithID:listID count:count sinceID:nil maxID:nil];
}
- (id)getTimelineForListWithID:(NSString *)listID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
return [self getTimelineForListWithID:listID count:count sinceID:sinceID maxID:maxID excludeRetweets:YES excludeReplies:YES];
}
- (id)getTimelineForListWithID:(NSString *)listID count:(int)count excludeRetweets:(BOOL)excludeRetweets excludeReplies:(BOOL)excludeReplies {
return [self getTimelineForListWithID:listID count:count sinceID:nil maxID:nil excludeRetweets:excludeRetweets excludeReplies:excludeReplies];
}
- (id)getTimelineForListWithID:(NSString *)listID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID excludeRetweets:(BOOL)excludeRetweets excludeReplies:(BOOL)excludeReplies {
if (count == 0) {
return nil;
}
if (listID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_statuses];
NSMutableDictionary *params = [@{ @"count":@(count).stringValue, @"exclude_replies":(excludeReplies?@"true":@"false"), @"include_rts":(excludeRetweets?@"false":@"true"),@"list_id":listID } mutableCopy];
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (id)getListsForUser:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_lists_list];
return [self sendGETRequestForURL:baseURL andParams:@{ (isID?@"user_id":@"screen_name"): user }];
}
- (id)getRetweetsForTweet:(NSString *)identifier count:(int)count {
if (count == 0) {
return nil;
}
if (identifier.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/retweets/%@.json",identifier]];
return [self sendGETRequestForURL:baseURL andParams:@{ @"count":@(count).stringValue }];
}
- (id)getRetweetedTimelineWithCount:(int)count {
return [self getRetweetedTimelineWithCount:count sinceID:nil maxID:nil];
}
- (id)getRetweetedTimelineWithCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
if (count == 0) {
return nil;
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_retweets_of_me];
NSMutableDictionary *params = [@{ @"count":@(count).stringValue, @"exclude_replies":@"false", @"include_rts":@"true"} mutableCopy];
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (id)getMentionsTimelineWithCount:(int)count {
return [self getMentionsTimelineWithCount:count sinceID:nil maxID:nil];
}
- (id)getMentionsTimelineWithCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
if (count == 0) {
return nil;
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_metions_timeline];
NSMutableDictionary *params = [@{ @"count":@(count).stringValue, @"exclude_replies":@"false", @"include_rts":@"true" } mutableCopy];
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (NSError *)postTweet:(NSString *)tweetString withImageData:(NSData *)theData {
return [self postTweet:tweetString withImageData:theData inReplyTo:nil];
}
- (NSError *)postTweet:(NSString *)tweetString withImageData:(NSData *)theData inReplyTo:(NSString *)irt {
if (tweetString.length == 0) {
return [NSError badRequestError];
} else if (theData.length == 0) {
if (irt.length == 0) {
return [self postTweet:tweetString];
} else {
return [self postTweet:tweetString inReplyTo:irt];
}
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_update_with_media];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"status"] = tweetString;
params[@"media[]"] = theData;
if (irt.length > 0) {
params[@"in_reply_to_status_id"] = irt;
}
return [self sendPOSTRequestForURL:baseURL andParams:params];
}
- (NSError *)destroyTweet:(NSString *)identifier {
if (identifier.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_destroy];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"id": identifier}];
}
- (id)getDetailsForTweet:(NSString *)identifier {
if (identifier.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_show];
return [self sendGETRequestForURL:baseURL andParams:@{ @"id":identifier, @"include_my_retweet":@"true" }];
}
- (NSError *)retweet:(NSString *)identifier {
if (identifier.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/retweet/%@.json",identifier]];
return [self sendPOSTRequestForURL:baseURL andParams:nil];
}
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count {
return [self getTimelineForUser:user isID:isID count:count sinceID:nil maxID:nil];
}
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
if (count == 0) {
return nil;
}
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_statuses_user_timeline];
NSMutableDictionary *params = [@{ @"count":@(count).stringValue, (isID?@"user_id":@"screen_name"):user, @"exclude_replies":@"false", @"include_rts":@"true" } mutableCopy];
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (id)getProfileImageForUsername:(NSString *)username andSize:(FHSTwitterEngineImageSize)size {
if (username.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_users_show];
id userShowReturn = [self sendGETRequestForURL:baseURL andParams:@{ @"screen_name":username }];
if ([userShowReturn isKindOfClass:[NSError class]]) {
return userShowReturn;
} else if ([userShowReturn isKindOfClass:[NSDictionary class]]) {
NSString *url = userShowReturn[@"profile_image_url"]; // normal
if (size == 0) { // mini
url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@"_mini"];
} else if (size == 2) { // bigger
url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
} else if (size == 3) { // original
url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@""];
}
id ret = [self sendRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
if ([ret isKindOfClass:[NSData class]]) {
return [UIImage imageWithData:(NSData *)ret];
}
return ret;
}
return [NSError badRequestError];
}
- (id)authenticatedUserIsBlocking:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_blocks_exists];
return [self sendGETRequestForURL:baseURL andParams:@{ (isID?@"user_id":@"screen_name"):@"true", @"skip_status":@"true" }];
}
- (id)listBlockedUsers {
NSURL *baseURL = [NSURL URLWithString:url_blocks_blocking];
return [self sendGETRequestForURL:baseURL andParams:@{ @"skip_status":@"true" }];
}
- (id)listBlockedIDs {
NSURL *baseURL = [NSURL URLWithString:url_blocks_blocking_ids];
return [self sendGETRequestForURL:baseURL andParams:@{ @"stringify_ids": @"true" }];
}
- (id)getLanguages {
NSURL *baseURL = [NSURL URLWithString:url_help_languages];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (id)getConfiguration {
NSURL *baseURL = [NSURL URLWithString:url_help_configuration];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (NSError *)reportUserAsSpam:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_users_report_spam];
return [self sendPOSTRequestForURL:baseURL andParams:@{(isID?@"user_id":@"screen_name"): user}];
}
- (id)showDirectMessage:(NSString *)messageID {
if (messageID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_direct_messages_show];
return [self sendGETRequestForURL:baseURL andParams:@{ @"id":messageID }];
}
- (NSError *)sendDirectMessage:(NSString *)body toUser:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
if (body.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_direct_messages_new];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"text": [body fhs_trimForTwitter], (isID?@"user_id":@"screen_name"):user}];
}
- (id)getSentDirectMessages:(int)count {
if (count == 0) {
return nil;
}
NSURL *baseURL = [NSURL URLWithString:url_direct_messages_sent];
return [self sendGETRequestForURL:baseURL andParams:@{ @"count":@(count).stringValue }];
}
- (NSError *)deleteDirectMessage:(NSString *)messageID {
if (messageID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_direct_messages_destroy];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"id": messageID, @"include_entities": (_includeEntities?@"true":@"false")}];
}
- (id)getDirectMessages:(int)count {
if (count == 0) {
return nil;
}
NSURL *baseURL = [NSURL URLWithString:url_direct_messages];
return [self sendGETRequestForURL:baseURL andParams:@{ @"count":@(count).stringValue,@"skip_status":@"true" }];
}
- (id)getPrivacyPolicy {
NSURL *baseURL = [NSURL URLWithString:url_help_privacy];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (id)getTermsOfService {
NSURL *baseURL = [NSURL URLWithString:url_help_tos];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (id)getNoRetweetIDs {
NSURL *baseURL = [NSURL URLWithString:url_friendships_no_retweets_ids];
return [self sendGETRequestForURL:baseURL andParams:@{ @"stringify_ids":@"true" }];
}
- (NSError *)enableRetweets:(BOOL)enableRTs andDeviceNotifs:(BOOL)devNotifs forUser:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_friendships_update];
return [self sendPOSTRequestForURL:baseURL andParams:@{(isID?@"user_id":@"screen_name"): user, @"retweets": (enableRTs?@"true":@"false"), @"device": (devNotifs?@"true":@"false")}];
}
- (id)getPendingOutgoingFollowers {
NSURL *baseURL = [NSURL URLWithString:url_friendships_outgoing];
return [self sendGETRequestForURL:baseURL andParams:@{ @"stringify_ids":@"true" }];
}
- (id)getPendingIncomingFollowers {
NSURL *baseURL = [NSURL URLWithString:url_friendships_incoming];
return [self sendGETRequestForURL:baseURL andParams:@{ @"stringify_ids":@"true" }];
}
- (id)lookupFriendshipStatusForUsers:(NSArray *)users areIDs:(BOOL)areIDs {
if (users.count == 0) {
return nil;
}
NSMutableArray *returnedDictionaries = [NSMutableArray array];
NSArray *reqStrings = [self generateRequestStringsFromArray:users];
NSURL *baseURL = [NSURL URLWithString:url_friendships_lookup];
for (NSString *reqString in reqStrings) {
id retObj = [self sendGETRequestForURL:baseURL andParams:@{ (areIDs?@"user_id":@"screen_name"):reqString }];
if ([retObj isKindOfClass:[NSArray class]]) {
[returnedDictionaries addObjectsFromArray:(NSArray *)retObj];
} else if ([retObj isKindOfClass:[NSError class]]) {
return retObj;
}
}
return returnedDictionaries;
}
- (NSError *)unfollowUser:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_friendships_destroy];
return [self sendPOSTRequestForURL:baseURL andParams:@{(isID?@"user_id":@"screen_name"): user}];
}
- (NSError *)followUser:(NSString *)user isID:(BOOL)isID {
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_friendships_create];
return [self sendPOSTRequestForURL:baseURL andParams:@{(isID?@"user_id":@"screen_name"): user}];
}
- (id)verifyCredentials {
NSURL *baseURL = [NSURL URLWithString:url_account_verify_credentials];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (id)getFavoritesForUser:(NSString *)user isID:(BOOL)isID andCount:(int)count {
return [self getFavoritesForUser:user isID:isID andCount:count sinceID:nil maxID:nil];
}
- (id)getFavoritesForUser:(NSString *)user isID:(BOOL)isID andCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID {
if (count == 0) {
return nil;
}
if (user.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:url_favorites_list];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:5];
params[@"count"] = [NSString stringWithFormat:@"%d",count];
params[(isID?@"user_id":@"screen_name")] = user;
params[@"include_entities"] = _includeEntities?@"true":@"false";
if (sinceID.length > 0) {
params[@"since_id"] = sinceID;
}
if (maxID.length > 0) {
params[@"max_id"] = maxID;
}
return [self sendGETRequestForURL:baseURL andParams:params];
}
- (NSError *)markTweet:(NSString *)tweetID asFavorite:(BOOL)flag {
if (tweetID.length == 0) {
return [NSError badRequestError];
}
NSURL *baseURL = [NSURL URLWithString:flag?url_favorites_create:url_favorites_destroy];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"id": tweetID}];
}
- (id)getRateLimitStatus {
NSURL *baseURL = [NSURL URLWithString:url_application_rate_limit_status];
return [self sendGETRequestForURL:baseURL andParams:nil];
}
- (NSError *)updateProfileColorsWithDictionary:(NSDictionary *)dictionary {
if (!dictionary) {
return [NSError badRequestError];
}
// Each parameter is a hex color
NSString *profile_background_color = dictionary[FHSProfileBackgroundColorKey];
NSString *profile_link_color = dictionary[FHSProfileLinkColorKey];
NSString *profile_sidebar_border_color = dictionary[FHSProfileSidebarBorderColorKey];
NSString *profile_sidebar_fill_color = dictionary[FHSProfileSidebarFillColorKey];
NSString *profile_text_color = dictionary[FHSProfileTextColorKey];
NSURL *baseURL = [NSURL URLWithString:url_account_update_profile_colors];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:6];
params[@"skip_status"] = @"true";
if (profile_background_color.length > 0) {
params[@"profile_background_color"] = profile_background_color;
}
if (profile_link_color.length > 0) {
params[@"profile_link_color"] = profile_link_color;
}
if (profile_sidebar_border_color.length > 0) {
params[@"profile_sidebar_border_color"] = profile_sidebar_border_color;
}
if (profile_sidebar_fill_color.length > 0) {
params[@"profile_sidebar_fill_color"] = profile_sidebar_fill_color;
}
if (profile_text_color.length > 0) {
params[@"profile_text_color"] = profile_text_color;
}
return [self sendPOSTRequestForURL:baseURL andParams:params];
}
- (NSError *)setUseProfileBackgroundImage:(BOOL)shouldUseBGImg {
NSURL *baseURL = [NSURL URLWithString:url_account_update_profile_background_image];
return [self sendPOSTRequestForURL:baseURL andParams:@{@"skip_status": @"true", @"use": (shouldUseBGImg?@"true":@"false")}];
}