話(huà)不多說(shuō)直接狠鸳,上代碼
比如優(yōu)酷鏈接:http://v.youku.com/v_show/id_XNzA3NjE4MTEy.html
Model
@interface VideoModel : NSObject
@property (nonatomic, copy) NSString * srcUrl;
@property (nonatomic, copy) NSString * videoUrl;
@property (nonatomic, copy) NSString * streamType;
@property (nonatomic, copy) NSString * hdtv;
@property (nonatomic, assign) NSInteger size;
@property (nonatomic, assign) NSInteger videoMilliseconds;
@end
VC
typedef void (^completeBlock)(NSArray * array);
@property (nonatomic, strong) NSMutableDictionary * cookDic;
@property (nonatomic, strong) NSMutableArray * videoTSArr;
self.videoTSArr = [NSMutableArray array];
NSString * vid = [self getVid:self.self.model.url];
NSString * utid = [self getUtid];
NSTimeInterval client_ts = [NSDate date].timeIntervalSince1970;
NSString * playApi = [NSString stringWithFormat:@"https://ups.youku.com/ups/get.json?vid=%@&ccode=0521&client_ip=192.168.1.1&utid=%@&client_ts=%ld",vid, utid, (long)client_ts];
[self getVideoMessage:playApi complete:^(NSArray *array) {
VideoModel * video = [array firstObject];
playerManager.assetURL = [NSURL URLWithString:video.videoUrl];
}];
- (NSString *)getVid:(NSString *)url
{
NSString * string = @"";
NSArray * strArr = [url componentsSeparatedByString:@"id_"];
if (strArr.count > 1)
{
NSArray * neArray = [[strArr lastObject] componentsSeparatedByString:@".html"];
if (neArray.count > 1)
{
string = [neArray firstObject];
}
}
return string;
}
- (NSString *)getUtid
{
NSString * etgm = @"";
NSString * utidURLStr = @"https://log.mmstat.com/eg.js";
NSURL * url = [NSURL URLWithString:utidURLStr];
NSError * error;
NSStringEncoding encoding;
NSString * dataString = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];
NSArray * arr = [dataString componentsSeparatedByString:@";"];
for(NSString * str in arr)
{
if([str hasPrefix:@"goldlog.Etag"])
{
NSString * etag = [[str componentsSeparatedByString:@"="] lastObject];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:傍妒;()¥「」"蛉签、[]{}#%-*+=_\\|~<>$€^?'@#$%^&*()_+'\""];
etag = [etag stringByTrimmingCharactersInSet:set];
etgm = etag;
break;
}
}
return etgm;
}
- (NSMutableDictionary *)cookDic
{
if (!_cookDic)
{
_cookDic = [NSMutableDictionary dictionary];
[_cookDic setObject:[self getCookieValue:3] forKey:NSHTTPCookieValue];
[_cookDic setObject:[NSDate dateWithTimeIntervalSinceNow:60*60*24] forKey:NSHTTPCookieExpires];
[_cookDic setObject:@"http://www.youku.com" forKey:NSHTTPCookieName];
[_cookDic setObject:@"www.youku.com" forKey:NSHTTPCookieDomain];
[_cookDic setObject:@"0" forKey:NSHTTPCookieVersion];
[_cookDic setObject:@"www.youku.com" forKey:NSHTTPCookiePath];
}
return _cookDic;
}
- (NSString *)getCookieValue:(NSInteger)length
{
NSString * source = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString * str = [[NSMutableString alloc] init];
for (NSInteger index = 0; index < length; index++)
{
NSInteger number = arc4random() % 52;
NSString * sourceStr = [source substringWithRange:NSMakeRange(number,1)];
[str appendString:sourceStr];
}
NSInteger timeVal = [NSDate date].timeIntervalSince1970;
return [NSString stringWithFormat:@"%ld%@",(long)timeVal,str];
}
- (void)getVideoMessage:(NSString *)api complete:(completeBlock)complete
{
[self.videoTSArr removeAllObjects];
NSHTTPCookie * cookie = [[NSHTTPCookie alloc] initWithProperties:self.cookDic];
NSArray * cookies = [NSArray arrayWithObjects:cookie,nil];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[sessionManager.requestSerializer setValue:self.model.url forHTTPHeaderField:@"Referer"];
[sessionManager.requestSerializer setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
[sessionManager GET:api parameters:nil progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id responseObject) {
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSDictionary * videoDic = [dic objectForKey:@"data"];
if([videoDic objectForKey:@"error"] != NULL)
{
NSLog(@"%@",[[videoDic objectForKey:@"error"]objectForKey:@"note"]);
complete(@[]);
}
else
{
NSArray * streamArr = [videoDic objectForKey:@"stream"];
for(NSDictionary * item in streamArr)
{
if(![[item objectForKey:@"audio_lang"]isEqualToString:@"guoyu"])continue;
VideoModel * video = [VideoModel new];
video.srcUrl = api;
video.videoUrl = [item objectForKey:@"m3u8_url"];
video.size = [[item objectForKey:@"size"] integerValue];
video.streamType = [item objectForKey:@"stream_type"];
video.videoMilliseconds = [[item objectForKey:@"milliseconds_video"] integerValue];
video.hdtv = [self hdtv:[item objectForKey:@"stream_type"]];
[self.videoTSArr addObject:video];
}
if(self.videoTSArr.count <= 0)
{
for(NSDictionary * item in streamArr)
{
VideoModel * video = [VideoModel new];
video.srcUrl = api;
video.videoUrl = [item objectForKey:@"m3u8_url"];
video.size = [[item objectForKey:@"size"]integerValue];
video.streamType = [item objectForKey:@"stream_type"];
video.videoMilliseconds = [[item objectForKey:@"milliseconds_video"]integerValue];
video.hdtv = [self hdtv:[item objectForKey:@"stream_type"]];
[self.videoTSArr addObject:video];
}
}
complete([self.videoTSArr copy]);
}
}failure:^(NSURLSessionDataTask * _Nullable task,NSError * _Nonnull error) {
complete(@[]);
}];
}
- (NSString *)hdtv:(NSString *)type
{
NSString * ht = @"h1";
if([type isEqualToString:@"hd3"])
{
ht = @"1";
}
else if([type isEqualToString:@"hd2"])
{
ht = @"2";
}
else if([type isEqualToString:@"mp4hd"]||
[type isEqualToString:@"mp4"])
{
ht = @"3";
}
else if([type isEqualToString:@"mp4hd3"]||
[type isEqualToString:@"mp4hd2"]||
[type isEqualToString:@"flvhd"]||
[type isEqualToString:@"flv"])
{
ht = @"4";
}
else if([type isEqualToString:@"3gphd"])
{
ht = @"5";
}
else if([type isEqualToString:@"3gp"])
{
ht = @"6";
}
return ht;
}
PS:目前ccode是0521【我試出來(lái)的】