forked from oikyn/ExtractVideoURLManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractVideoURLManager.m
More file actions
316 lines (237 loc) · 12.6 KB
/
ExtractVideoURLManager.m
File metadata and controls
316 lines (237 loc) · 12.6 KB
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
#import "ExtractVideoURLManager.h"
#import <JavaScriptCore/JavaScriptCore.h>
typedef void (^FetchSourceResponceHandler)(NSData *data, NSError *error);
static NSString* const youtubeURL = @"https://www.youtube.com/watch?v=%@";
static NSString* const dailymotionURL = @"https://www.dailymotion.com/embed/video/%@";
static NSString* const userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";
static NSString* const ytDummySignature = @"BC46474764CD5E86EBFECD43C5692A50528C66A2F45.A3A45BF375942288144D567BC990BD0A09483A1111";
@interface ExtractVideoURLManager()
{
JSContext *_jsContext;
}
@end
@implementation ExtractVideoURLManager
- (id)init {
if (self = [super init]) {
_jsContext = [JSContext new];
}
return self;
}
- (void)youtube:(NSString *)vid handler:(ExtractURLHandler)handler {
__weak typeof(self)weakSelf = self;
NSString *urlString = [NSString stringWithFormat:youtubeURL, vid];
//Fetching
[self fetchSource:[NSURL URLWithString:urlString] responseHandler:^(NSData *data, NSError *error){
//Error
if (error) {
handler(nil, error);
return;
}
//Extract Info
id infoObj = [weakSelf yt_ExtractInfo:data];
//Error
if (!infoObj) {
handler(nil, [NSError errorWithDomain:@"" code:400 userInfo:nil]);
return;
}
//PlayerJS URL
NSString *playerJS_URLString = [weakSelf nullToString:[infoObj valueForKeyPath:@"config.assets.js"]];
playerJS_URLString = [NSString stringWithFormat:@"http:%@", playerJS_URLString];
//Fetching
[weakSelf fetchSource:[NSURL URLWithString:playerJS_URLString] responseHandler:^(NSData *data2, NSError *error2){
//Error
if (error2) {
handler(nil, error2);
return;
}
NSString *decryptionFuncName = [weakSelf yt_ExtractDecryptionFuncName:data2];
//Error
if (!decryptionFuncName) {
handler(nil, [NSError errorWithDomain:@"" code:400 userInfo:nil]);
return;
}
NSString *decryptionFunc = [weakSelf yt_ExtractDecryptionFunc:data2 funcName:decryptionFuncName];
//Error
if (!decryptionFunc) {
handler(nil, [NSError errorWithDomain:@"" code:400 userInfo:nil]);
return;
}
[_jsContext evaluateScript:decryptionFunc];
NSString *decryptionFuncProperty = [weakSelf yt_ValidateDecryptionFunc:data2 funcName:decryptionFuncName];
//Error
if (!decryptionFuncProperty) {
handler(nil, [NSError errorWithDomain:@"" code:400 userInfo:nil]);
return;
}
[_jsContext evaluateScript:decryptionFuncProperty];
handler([weakSelf yt_CreateURLs:infoObj funcName:decryptionFuncName], nil);
return;
}];
}];
}
- (void)dailymotion:(NSString *)vid handler:(ExtractURLHandler)handler {
__weak typeof(self)weakSelf = self;
NSString *urlString = [NSString stringWithFormat:dailymotionURL, vid];
//Fetching
[self fetchSource:[NSURL URLWithString:urlString] responseHandler:^(NSData *data, NSError *error){
//Error
if (error) {
handler(nil, error);
return;
}
//Extract Info
id infoObj = [self dm_ExtractInfo:data];
//Error
if (!infoObj) {
handler(nil, [NSError errorWithDomain:@"" code:400 userInfo:nil]);
return;
}
handler([weakSelf dm_CreateURLs:infoObj], nil);
return;
}];
}
/////////////////////////////////////////////
// Youtube
/////////////////////////////////////////////
// 動画情報を取得
- (id)yt_ExtractInfo:(NSData *)data {
NSString *regPtn = @"<script>(.*);ytplayer.load";
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regPtn
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSTextCheckingResult *result = [reg firstMatchInString:str
options:(NSMatchingOptions)0
range:NSMakeRange(0, str.length)];
NSString *infoObjStr = (result.numberOfRanges > 1) ? [str substringWithRange:[result rangeAtIndex:1]] : nil;
if (!infoObjStr) {
return nil;
}
[_jsContext evaluateScript:infoObjStr];
JSValue *infoObjValue = _jsContext[@"ytplayer"];
id infoObj = [infoObjValue toObject];
return infoObj;
}
// 復号化する関数名を取得
- (NSString *)yt_ExtractDecryptionFuncName:(NSData *)data {
NSString *regPtn = @"set\\([\"']signature[\"']\\s*,\\s*(.*)\\((.*)\\)\\)";
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regPtn
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSTextCheckingResult *result = [reg firstMatchInString:str
options:(NSMatchingOptions)0
range:NSMakeRange(0, str.length)];
NSString *funcName = (result.numberOfRanges > 1) ? [str substringWithRange:[result rangeAtIndex:1]] : nil;
return funcName;
}
// 復号化する関数文字列を取得
- (NSString *)yt_ExtractDecryptionFunc:(NSData *)data funcName:(NSString *)funcName {
NSString *regPtn = @"function %@\\((.*)\\)\\s*\\{(.*)\\};";
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *regStr = [NSString stringWithFormat:regPtn, funcName];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regStr
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSTextCheckingResult *result = [reg firstMatchInString:str
options:(NSMatchingOptions)0
range:NSMakeRange(0, str.length)];
NSString *funcStr = (result.numberOfRanges > 1) ? [str substringWithRange:[result rangeAtIndex:0]] : nil;
return funcStr;
}
// 復号化する関数を有効にする
- (NSString *)yt_ValidateDecryptionFunc:(NSData *)data funcName:(NSString *)funcName {
[_jsContext evaluateScript:[NSString stringWithFormat:@"%@('%@')", funcName, ytDummySignature]];
NSString *exception = [[_jsContext exception] toString];
// 関数内で使用されているプロパティも有効にする
NSString *funcName2 = [exception stringByReplacingOccurrencesOfString:@"ReferenceError: Can't find variable: " withString:@""];
NSString *regPtn = @"var %@\\s*=\\s*\\{(.*)\\};";
NSString *regStr = [NSString stringWithFormat:regPtn, funcName2];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regStr
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSTextCheckingResult *result = [reg firstMatchInString:str
options:(NSMatchingOptions)0
range:NSMakeRange(0, str.length)];
NSString *varStr = (result.numberOfRanges > 1) ? [str substringWithRange:[result rangeAtIndex:0]] : nil;
return varStr;
}
// URLを作成する
- (NSDictionary *)yt_CreateURLs:(id)infoObj funcName:(NSString *)funcName {
NSMutableDictionary *urls = [NSMutableDictionary dictionary];
NSString *stream_map = [self nullToString:[infoObj valueForKeyPath:@"config.args.url_encoded_fmt_stream_map"]];
NSArray *stream_map_ary = [stream_map componentsSeparatedByString:@","];
for (NSString *stream in stream_map_ary) {
NSString *itag = @"";
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (NSString *q in [stream componentsSeparatedByString:@"&"]) {
NSArray *q_ary = [q componentsSeparatedByString:@"="];
if ([q_ary[0] isEqualToString:@"itag"]) {
itag = q_ary[1];
}
if ([q_ary[0] isEqualToString:@"url"] || [q_ary[0] isEqualToString:@"type"]) {
dic[q_ary[0]] = [q_ary[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
dic[q_ary[0]] = q_ary[1];
}
}
if (dic[@"s"] != nil) {
NSString *decrypted_s = [[_jsContext evaluateScript:[NSString stringWithFormat:@"%@('%@')", funcName, dic[@"s"]]] toString];
dic[@"url"] = [dic[@"url"] stringByAppendingFormat:@"&signature=%@", decrypted_s];
}
urls[itag] = dic;
}
return [NSDictionary dictionaryWithDictionary:urls];
}
/////////////////////////////////////////////
// Dailymotion
/////////////////////////////////////////////
// 動画情報を取得
- (id)dm_ExtractInfo:(NSData *)data {
NSString *regPtn = @"var info\\s*=\\s*\\{(.*)\\}";
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regPtn
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSTextCheckingResult *result = [reg firstMatchInString:str
options:(NSMatchingOptions)0
range:NSMakeRange(0, str.length)];
NSString *infoObjStr = (result.numberOfRanges > 1) ? [str substringWithRange:[result rangeAtIndex:0]] : nil;
if (!infoObjStr) {
return nil;
}
[_jsContext evaluateScript:infoObjStr];
JSValue *infoObjValue = _jsContext[@"info"];
id infoObj = [infoObjValue toObject];
return infoObj;
}
// URLを作成する
- (NSDictionary *)dm_CreateURLs:(id)infoObj {
NSMutableDictionary *urls = [NSMutableDictionary dictionary];
urls[@"stream_h264_hd1080_url"] = [self nullToString:[infoObj objectForKey:@"stream_h264_hd1080_url"]];
urls[@"stream_h264_hd_url"] = [self nullToString:[infoObj objectForKey:@"stream_h264_hd_url"]];
urls[@"stream_h264_hq_url"] = [self nullToString:[infoObj objectForKey:@"stream_h264_hq_url"]];
urls[@"stream_h264_ld_url"] = [self nullToString:[infoObj objectForKey:@"stream_h264_ld_url"]];
urls[@"stream_h264_url"] = [self nullToString:[infoObj objectForKey:@"stream_h264_url"]];
urls[@"stream_hls_url"] = [self nullToString:[infoObj objectForKey:@"stream_hls_url"]];
return [NSDictionary dictionaryWithDictionary:urls];
}
/////////////////////////////////////////////
// 共通
/////////////////////////////////////////////
- (void)fetchSource:(NSURL *)url responseHandler:(FetchSourceResponceHandler)handler {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (handler) {
handler(data, error);
}
}];
[task resume];
}
- (NSString *)nullToString:(NSString *)data {
return (![data isEqual:[NSNull null]] ? data : @"");
}