-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSocialVideoHelper.m
executable file
·381 lines (302 loc) · 19.5 KB
/
SocialVideoHelper.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
//
// SocialVideoHelper.m
//
// Originally Created by ryu-ushin on 6/5/15.
// Updated async Twitter upload methods by LongMA and YuSong on 29/1/17.
// Now you should able to upload large video to Twitter (no more than 512MB)
// According to https://dev.twitter.com/rest/media/uploading-media
// Copyright (c) 2015 ryu-ushin. All rights reserved.
//
#import "SocialVideoHelper.h"
@implementation SocialVideoHelper
#define DispatchMainThread(block, ...) if(block) dispatch_async(dispatch_get_main_queue(), ^{ block(__VA_ARGS__); })
#define Video_Chunk_Max_size 1000 * 1000 * 5
+(void)uploadError:(NSError*)error withCompletion:(VideoUploadCompletion)completion{
NSString *errorDes = [error localizedDescription];
NSLog(@"There was an error:%@", errorDes);
DispatchMainThread(^(){completion(NO, errorDes);});
}
+(void)uploadSuccessWithCompletion:(VideoUploadCompletion)completion{
DispatchMainThread(^(){completion(YES, nil);});
}
#pragma mark - For Facebook
+(BOOL)userHasAccessToFacebook
{
return [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook];
}
+(void)uploadFacebookVideo:(NSData*)videoData comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *facebookPostURL = [[NSURL alloc] initWithString:@"https://graph-video.facebook.com/v2.3/me/videos"];
NSDictionary *postParams = @{
@"access_token": account.credential.oauthToken,
@"upload_phase" : @"start",
@"file_size" : [NSNumber numberWithInteger: videoData.length].stringValue
};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:facebookPostURL parameters:postParams];
request.account = account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook Stage1 HTTP Response: %li, responseData: %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Facebook Error stage 1 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
} else {
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Facebook Stage1 dic -> %@", returnedData);
NSString *upload_session_id = returnedData[@"upload_session_id"];
[SocialVideoHelper facebookVideoStage2:videoData comment:(NSString*)comment upload_session_id:upload_session_id account:account withCompletion:completion];
}
}];
}
+(void)facebookVideoStage2:(NSData*)videoData comment:(NSString*)comment upload_session_id:(NSString *)upload_session_id account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *facebookPostURL = [[NSURL alloc] initWithString:@"https://graph-video.facebook.com/v2.3/me/videos"];
NSDictionary *postParams = @{
@"access_token": account.credential.oauthToken,
@"upload_phase" : @"transfer",
@"start_offset" : @"0",
@"upload_session_id" : upload_session_id
};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:facebookPostURL parameters:postParams];
request.account = account;
[request addMultipartData:videoData withName:@"video_file_chunk" type:@"video/mp4" filename:@"video"];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook Stage2 HTTP Response: %li, responseData: %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Facebook Error stage 2 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
} else {
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Facebook Stage2 dic -> %@", returnedData);
[SocialVideoHelper facebookVideoStage3:videoData comment:(NSString*)comment upload_session_id:upload_session_id account:account withCompletion:completion];
}
}];
}
+(void)facebookVideoStage3:(NSData*)videoData comment:(NSString*)comment upload_session_id:(NSString *)upload_session_id account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *facebookPostURL = [[NSURL alloc] initWithString:@"https://graph-video.facebook.com/v2.3/me/videos"];
if (comment == nil) {
comment = [NSString stringWithFormat:@"#SocialVideoHelper# https://github.com/liu044100/SocialVideoHelper"];
}
NSDictionary *postParams = @{
@"access_token": account.credential.oauthToken,
@"upload_phase" : @"finish",
@"upload_session_id" : upload_session_id,
@"description": comment
};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:facebookPostURL parameters:postParams];
request.account = account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook Stage3 HTTP Response: %li, responseData: %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Facebook Error stage 3 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
} else {
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Facebook Stage3 dic -> %@", returnedData);
if ([urlResponse statusCode] == 200){
NSLog(@"Facebook upload success !");
[SocialVideoHelper uploadSuccessWithCompletion:completion];
}
}
}];
}
#pragma mark - For Twitter
+(BOOL)userHasAccessToTwitter
{
return [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];
}
// change media_category to amplify_video for async video upload
+(void)uploadTwitterVideo:(NSData*)videoData comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *twitterPostURL = [[NSURL alloc] initWithString:@"https://upload.twitter.com/1.1/media/upload.json"];
NSDictionary *postParams = @{@"command": @"INIT",
@"media_category" : @"amplify_video",
@"total_bytes" : [NSNumber numberWithInteger: videoData.length].stringValue,
@"media_type" : @"video/mp4"
};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:twitterPostURL parameters:postParams];
request.account = account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter Stage1 HTTP Response: %li, responseData: %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Twitter Error stage 1 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSString *mediaID = [NSString stringWithFormat:@"%@", [returnedData valueForKey:@"media_id_string"]];
NSLog(@"stage one success, mediaID -> %@", mediaID);
[SocialVideoHelper tweetVideoStage2:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}];
}
// Change the Group to Operation
+(void)tweetVideoStage2:(NSData*)videoData mediaID:(NSString *)mediaID comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *twitterPostURL = [[NSURL alloc] initWithString:@"https://upload.twitter.com/1.1/media/upload.json"];
NSArray *chunks = [SocialVideoHelper separateToMultipartData:videoData];
NSMutableArray *requests = [NSMutableArray array];
for (int i = 0; i < chunks.count; i++) {
NSString *seg_index = [NSString stringWithFormat:@"%d",i];
NSDictionary *postParams = @{@"command": @"APPEND",
@"media_id" : mediaID,
@"segment_index" : seg_index,
};
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:twitterPostURL parameters:postParams];
postRequest.account = account;
[postRequest addMultipartData:chunks[i] withName:@"media" type:@"video/mp4" filename:@"video"];
[requests addObject:postRequest];
}
// __block NSError *theError = nil;
// dispatch_queue_t chunksRequestQueue = dispatch_queue_create("chunksRequestQueue", DISPATCH_QUEUE_SERIAL);
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
operationQueue.maxConcurrentOperationCount = 1;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[operationQueue addOperationWithBlock:^{
for (int i = 0; i < (requests.count - 1); i++) {
SLRequest *postRequest = requests[i];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter Stage2 - %d HTTP Response: %li, %@", (i+1),(long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Twitter Error stage 2 - %d, error - %@", (i+1), error);
// theError = error;
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
if (i == requests.count - 1) {
[SocialVideoHelper tweetVideoStage3:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}
dispatch_semaphore_signal(semaphore);
// dispatch_group_leave(requestGroup);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// dispatch_group_wait(requestGroup, DISPATCH_TIME_FOREVER);
}
// if (theError) {
// [SocialVideoHelper uploadError:theError withCompletion:completion];
// } else {
SLRequest *postRequest = requests.lastObject;
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter Stage2 - final, HTTP Response: %li, %@",(long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Twitter Error stage 2 - final, error - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
NSLog(@"stage two success, mediaID -> %@", mediaID);
[SocialVideoHelper tweetVideoStage3:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}];
// }
}];
}
+(void)tweetVideoStage3:(NSData*)videoData mediaID:(NSString *)mediaID comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *twitterPostURL = [[NSURL alloc] initWithString:@"https://upload.twitter.com/1.1/media/upload.json"];
NSDictionary *postParams = @{@"command": @"FINALIZE",
@"media_id" : mediaID };
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:twitterPostURL parameters:postParams];
// Set the account and begin the request.
postRequest.account = account;
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter Stage3 HTTP Response: %li, %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Twitter Error stage 3 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
NSLog(@"stage three success, mediaID -> %@", mediaID);
[SocialVideoHelper tweetVideoStage4:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}];
}
// add NEW tweetVideoStage4: STATUS command for async video upload
+(void)tweetVideoStage4:(NSData*)videoData mediaID:(NSString *)mediaID comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *twitterPostURL = [[NSURL alloc] initWithString:@"https://upload.twitter.com/1.1/media/upload.json"];
NSDictionary *postParams = @{@"command": @"STATUS",
@"media_id" : mediaID };
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:twitterPostURL parameters:postParams];
postRequest.account = account;
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// NSLog(@"Twitter Stage4 HTTP Response: %li", (long)[urlResponse statusCode]);
// NSLog(@"Twitter stage 4 urlResponse - %@", urlResponse);
NSLog(@"Twitter Stage4 HTTP Response: %li, %@", (long)[urlResponse statusCode], [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Twitter Error stage 4 - %@", error);
NSLog(@"Twitter Error stage 4 - %@", urlResponse);
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
if ([urlResponse statusCode] == 200){
// NSLog(@"Twitter STATUS upload success !");
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSDictionary *processing_infor = [returnedData valueForKey:@"processing_info"];
NSString *state = [NSString stringWithFormat:@"%@", [processing_infor valueForKey:@"state"]];
NSString *check_after_secs = [NSString stringWithFormat:@"%@", [processing_infor valueForKey:@"check_after_secs"]];
NSString *progress_percent = [NSString stringWithFormat:@"%@", [processing_infor valueForKey:@"progress_percent"]];
NSLog(@"state : %@, check_after_secs: %@, progress_percent: %@\n",state, check_after_secs, progress_percent);
if ([state isEqual: @"in_progress"]) {
NSLog(@"%@",[NSThread currentThread]);
sleep([check_after_secs intValue]);
[SocialVideoHelper tweetVideoStage4:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
} else if ([state isEqual: @"pending"]){
NSLog(@"%@",[NSThread currentThread]);
sleep([check_after_secs intValue]);
[SocialVideoHelper tweetVideoStage4:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
} else if ([state isEqual: @"failed"]) {
NSLog(@"tweetVideoStage4 return state : failed");
return; //TODO HUD
} else if ([state isEqual: @"succeeded"]) {
NSLog(@"stage four success, mediaID -> %@", mediaID);
[SocialVideoHelper tweetVideoStage5:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}
}
}];
}
+(void)tweetVideoStage5:(NSData*)videoData mediaID:(NSString *)mediaID comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion{
NSURL *twitterPostURL = [[NSURL alloc] initWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
if (comment == nil) {
comment = [NSString stringWithFormat:@"#SocialVideoHelper# https://github.com/liu044100/SocialVideoHelper"];
// comment = [NSString stringWithFormat:@" "];
}
NSDictionary *postParams = @{@"status": comment,
@"media_ids" : @[mediaID]};
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:twitterPostURL parameters:postParams];
postRequest.account = account;
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// NSLog(@"Twitter Stage4 HTTP Response: %li", (long)[urlResponse statusCode]);
NSLog(@"Twitter stage 5 urlResponse - %@", urlResponse);
if (error) {
NSLog(@"Twitter Error stage 5 - %@", error);
NSLog(@"Twitter Error stage 5 - %@", urlResponse);
[SocialVideoHelper uploadError:error withCompletion:completion];
return;
} else {
if ([urlResponse statusCode] == 200){
NSLog(@"stage five success, mediaID -> %@", mediaID);
NSLog(@"Twitter upload success !");
[SocialVideoHelper uploadSuccessWithCompletion:completion];
}
}
}];
}
+(NSArray*)separateToMultipartData:(NSData*)videoData{
NSMutableArray *multipartData = [NSMutableArray new];
CGFloat length = videoData.length;
CGFloat standard_length = Video_Chunk_Max_size;
if (length <= standard_length) {
[multipartData addObject:videoData];
NSLog(@"need not separate as chunk, data size -> %ld bytes", (long)videoData.length);
} else {
NSUInteger count = ceil(length/standard_length);
for (int i = 0; i < count; i++) {
NSRange range;
if (i == count - 1) {
range = NSMakeRange(i * standard_length, length - i * standard_length);
} else {
range = NSMakeRange(i * standard_length, standard_length);
}
NSData *part_data = [videoData subdataWithRange:range];
[multipartData addObject:part_data];
NSLog(@"chunk index -> %d, data size -> %ld bytes", (i+1), (long)part_data.length);
}
}
return multipartData.copy;
}
@end