-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathmain.m
106 lines (76 loc) · 3.43 KB
/
main.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
//
// main.m
// streaming
//
// Created by Nicolas Seriot on 03/05/14.
// Copyright (c) 2014 Nicolas Seriot. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "STTwitter.h"
NSString *firstTweetID = nil;
void postStatus(STTwitterAPI *twitter,
NSMutableArray *statusesAndMediaURLs,
NSString *previousStatusID) {
NSDictionary *d = [statusesAndMediaURLs firstObject];
if(d == nil) {
NSLog(@"--------------------");
NSLog(@"-- posting complete");
NSLog(@"-- see https://www.twitter.com/statuses/%@/", firstTweetID);
exit(0);
}
[statusesAndMediaURLs removeObjectAtIndex:0];
NSString *status = [d objectForKey:@"status"];
NSString *filePath = [d objectForKey:@"filePath"];
NSData *mediaData = [NSData dataWithContentsOfFile:filePath];
NSLog(@"--------------------");
NSLog(@"-- text: %@", status);
NSLog(@"-- data: %@", [filePath lastPathComponent]);
NSMutableDictionary *md = [NSMutableDictionary dictionary];
md[@"status"] = status;
if(previousStatusID) md[@"in_reply_to_status_id"] = previousStatusID;
md[@"media[]"] = mediaData;
md[kSTPOSTDataKey] = @"media[]";
[twitter postResource:@"statuses/update_with_media.json"
baseURLString:kBaseURLStringAPI_1_1
parameters:md
uploadProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"-- %.02f%%", 100.0 * totalBytesWritten / totalBytesExpectedToWrite);
} downloadProgressBlock:^(id json) {
} successBlock:^(NSDictionary *rateLimits, id response) {
NSLog(@"-- x-mediaratelimit-remaining: %@", rateLimits[@"x-mediaratelimit-remaining"]);
NSString *previousStatusID = [response objectForKey:@"id_str"];
NSLog(@"-- status: %@", previousStatusID);
if(firstTweetID == nil) firstTweetID = previousStatusID;
postStatus(twitter, statusesAndMediaURLs, previousStatusID);
} errorBlock:^(NSError *error) {
NSLog(@"-- %@", error);
exit(1);
}];
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
STTwitterAPI *t = [STTwitterAPI twitterAPIOSWithFirstAccount];
[t verifyCredentialsWithSuccessBlock:^(NSString *username) {
NSString *dirPath = @"/Users/nst/Desktop/sttwitter_cocoaheads/";
NSError *error = nil;
NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:&error];
if(filenames == nil) {
NSLog(@"-- error: %@", error);
exit(1);
}
NSMutableArray *statusesAndMediaURLs = [NSMutableArray array];
for (NSString *filename in filenames) {
if([filename hasPrefix:@"."]) continue;
NSString *filePath = [dirPath stringByAppendingPathComponent:filename];
[statusesAndMediaURLs addObject:@{@"status":filename, @"filePath":filePath}];
}
postStatus(t, statusesAndMediaURLs, nil);
} errorBlock:^(NSError *error) {
NSLog(@"-- %@", error);
}];
/**/
[[NSRunLoop currentRunLoop] run];
}
return 0;
}