forked from liu044100/SocialVideoHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialVideoHelper.m
298 lines (239 loc) · 15.1 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
//
// SocialVideoHelper.m
//
// Created by ryu-ushin on 6/5/15.
// 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];
}
+(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",
@"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];
} else {
NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSString *mediaID = [NSString stringWithFormat:@"%@", [returnedData valueForKey:@"media_id_string"]];
[SocialVideoHelper tweetVideoStage2:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
NSLog(@"stage one success, mediaID -> %@", mediaID);
}
}];
}
+(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);
dispatch_async(chunksRequestQueue, ^{
dispatch_group_t requestGroup = dispatch_group_create();
for (int i = 0; i < (requests.count - 1); i++) {
dispatch_group_enter(requestGroup);
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;
} else {
if (i == requests.count - 1) {
[SocialVideoHelper tweetVideoStage3:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}
dispatch_group_leave(requestGroup);
}];
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);
} else {
[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];
} else {
[SocialVideoHelper tweetVideoStage4:videoData mediaID:mediaID comment:comment account:account withCompletion:completion];
}
}];
}
+(void)tweetVideoStage4:(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"];
}
// Set the parameters for the third twitter video request.
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]);
if (error) {
NSLog(@"Twitter Error stage 4 - %@", error);
[SocialVideoHelper uploadError:error withCompletion:completion];
} else {
if ([urlResponse statusCode] == 200){
NSLog(@"Twitter upload success !");
[SocialVideoHelper uploadSuccessWithCompletion:completion];
} else {
[SocialVideoHelper uploadError:[NSError errorWithDomain:@"SocialVideoHelper" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Twitter upload failure"}] withCompletion: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