思路和前面解析其它视频网站的差不多,都是一层层的拨开隐藏的东西,获取到真正的视频地址。
1)需要准备视频网址。
2)一些验证码解析视频固定的参数
3)从网址的地址中获取需要的参数
4)解析视频的链接地址
一)准备参数(一些需要验证的参数)
self.scUrl= url;
self.MD5_KEY = @"d5fb4bd9d50c4be6948c97edd7254b0e";
self.SRC = @"76f90cbd92f94a2e925d83e8ccd22cb7";
NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
NSString * tvid = [[[[[self matchString:htmlString toRegexString:@"data-(?:player|shareplattrigger)-tvid\\s*=\\s*[\\'\"](\\d+)"] firstObject] componentsSeparatedByString:@"="] lastObject] substringFromIndex:1];
NSString * videoId = [[[[[self matchString:htmlString toRegexString:@"data-(?:player|shareplattrigger)-videoid\\s*=\\s*[\\'\"]([a-f\\d]+)"] firstObject] componentsSeparatedByString:@"="] lastObject] substringFromIndex:1];
2)上步中用到了利用正则表达式去获取到想要的信息
-(NSArray*)matchString:(NSString*)string toRegexString:(NSString*)regexStr{
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [stringlength])];
//match: 所有匹配到的字符,根据() 包含级
NSMutableArray *array = [NSMutableArray array];
for(NSTextCheckingResult * match in matches) {
for(inti =0; i < [match numberOfRanges]; i++) {
//以正则中的(),划分成不同的匹配部分
NSString * component = [string substringWithRange:[match rangeAtIndex:i]];
[array addObject:component];
}
}
return array;
)
二)拼接需要的解析视频的地址
1)拼接地址
-(NSString*)rawDaTa:(NSString*)tvid videoId:(NSString*)videoId{
NSString* raw =@"";
NSString * time =[NSString stringWithFormat:@"%ld",(long)[NSDate date].timeIntervalSince1970];
NSString* pa = [NSString stringWithFormat:@"%@%@%@",time,self.MD5_KEY,tvid];
NSString* sc = [XJMD5 MD5ForLower32Bate:pa];
self.api = @"http://cache.m.iqiyi.com/jp/tmts/%@/%@/?tvid=%@&vid=%@&src=%@&sc=%@&t=%@";
raw = [NSString stringWithFormat:self.api,tvid,videoId,tvid,videoId,self.SRC,sc,time];
return raw;
}
2)获取到地址后,就能直接去进行网络请求,然后解析反回来的json数据,获取到视频真正的地址
-(void)getVideoData:(NSString*)url{
AFHTTPSessionManager * mn = [AFHTTPSessionManager manager];
mn.responseSerializer = [AFHTTPResponseSerializer serializer];
[mn GET:url parameters:nil progress:^(NSProgress *_Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
NSString * str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSString * reStr = [str stringByReplacingOccurrencesOfString:@"var tvInfoJs="withString:@""];
NSData * data = [reStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if(![[dic objectForKey:@"code"] isEqualToString:@"A00000"])//error {
self.anLyBlock([NSMutableArray array]);
}
else {
NSArray * ary = [[dic objectForKey:@"data"] objectForKey:@"vidl"];
NSMutableArray * videoAry = [[NSMutableArray alloc] init];
//这里获取第一个视频的地址
NSDictionary * videoDic = [ary firstObject];
VideoModel * video = [VideoModel new];
video.videoUrl = [videoDic objectForKey:@"m3utx"];
video.srcUrl =self.scUrl;
[videoAry addObject:video];
if(self.anLyBlock)
self.anLyBlock(videoAry);
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
if(self.anLyBlock)
self.anLyBlock([NSMutableArray array]);
}];
}
上面的解析方法中,可能会有一些参数会被爱奇艺修改掉。如果解析不成功,看看是否是参数的问题。
申明:这只是自己的一些研究而已,请不要用于商业用途。如果影响到你的利益,请联系我。