tudou(土豆)、youku(优酷)API(有相应的dll [C#])

相信网上有不少的相关介绍;

先说一下怎么获取tudou的视频:

先给一个官方API,注:需要登录:

登录后去到我的应用,没的当然要创建了,主要是为了拿appkey

imageimageimage

我归纳一下:

土豆的视频分三类(普通、豆单、剧集),可以从他们提供的API里可清晰知道;

image

开发文档栏有具体说明;

普通视频的URL是这样的:www.tudou.com/programs/view/xSGVQG5Vi_M/或者www.tudou.com/programs/view/M9Ke35fBnDk/?tid=-1&aid=12667&pid=41010111&oid=100173&isNielson=0

而它的AP要求是这样的

image

主要是图中的itemCode;很幸运:普通的视频URL里就可以知道这个itemcode,聪明的你一看就知道了,就是跟在/view/__ 后面的,如上面URL里的xSGVQG5Vi_MM9Ke35fBnDk,这两个都是itemcode;

为什么上面说幸运呢,因为豆单的和剧集的就没那么好运了!在2011-06-01(呃。。。今天是6.1哦~~)为止,我还没有看到能够直接通过它的API直接拿到相关的信息;(豆单与剧集是列表形式的视频);

看下列表视频的URL:www.tudou.com/playlist/p/l12302700.html或者http://www.tudou.com/playlist/p/a66454i82719708.html或者http://www.tudou.com/playlist/p/a66506.html

说到这里,他们的API估计还没做好,因为我发现点问题,看图:

 

image

看绿色的框框,他们提供的API里一个是写着playlistid,而示例则是playlistCode,重点是如果API是不包括playlistCode的话根本是访问不通的,会提示

4001 : Required String parameter 'playlistCode' is not present

image

那这个playlistCode怎么拿到呢??(注:是最原始的URL,就是上面给的那些URL例子,因为在视频里的分享我们是可以拿到playlistCode,后面我会讲一下)

后来我查看了所有的API都没办法,因为最基本的需要就是playlistCode;

最后实在没办法了,就用了最原始的方法(分析其网页原码了,然后用正则匹配出来),后来看了原码后发现都提供了itemcode了,所以我就直接拿这个itemcode挺不是更好,也不用要拿playlistCode后还要定位某个视频(因为是列表啊);

image

拿到这个itemCode后就可以利用普通视频的API来拿相关信息了,测试结果还是挺理想的,当然这样就会向tudou请求两次了;

请求它的API时format选择了JSON,结果:

 View Code

 打开
{"multiResult":{"results":[{"description":"test","tags":"test","itemId":1000232,"channelId":1,"mediaType":"视频","totalTime":20100,"pubDate":"2006-04-27","title":"test","picUrl":"http://i1.tdimg.com/001/000/232/p.jpg","itemUrl":"http://www.tudou.com/programs/view/yg8CVootoAc/","itemCode":"yg8CVootoAc","ownerName":"yumihhh","ownerNickname":"yumihhh","outerPlayerUrl":"http://www.tudou.com/v/yg8CVootoAc/v.swf","ownerId":262809,"picChoiceUrl":["http://image2.tudou.com/data/imgs/i/001/000/232/m15.jpg","http://image2.tudou.com/data/imgs/i/001/000/232/m30.jpg"],"secret":false

上面提到的分享拿playlistCode

image

如果有人能知道列表视频的直接API,请告诉我,谢谢~~

youku的其实差不多

如果想通过youku官方提供的API就不可行了,因为我看了也是要APPKEY,关键是要合作方的,没免费注册~~或许我没看到吧,反正找不到;

不过我知道一个入口是:

http://v.youku.com/player/getPlayList/VideoIDS/XMjcxOTg0NTcy

注意这个XMjcxOTg0NTcy

youku的分类:

普通:http://v.youku.com/v_show/id_XMjcxOTU3OTk2.html

列表:http://v.youku.com/v_playlist/f6164011o1p0.html

列表的我还是通过分析其网页的原码拿这个VideoId;

image

贴个youku的代码

 YouKu Code

打开 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace DCC.UI.Common
{
    
public class YouKuApiHelper
    {
        
private string videoApiUrl = "http://v.youku.com/player/getPlayList/VideoIDS/{0}";
        
private string _url;
        
public YouKuApiHelper(string url)
        {
            
this._url = url;
        }

        
private bool GetVideoId(ref string videoId)
        {
            Regex regShow 
= new Regex(@"^http:\/\/v\.youku\.com\/v_show\/id_([\w\d+_-]*)\.html|^http:\/\/v\.youku\.com\/v_show\/id_([\w\d+_-]*)=\.html", RegexOptions.IgnoreCase);
            Match mShow 
= regShow.Match(_url);
            
if (mShow.Success)
            {
                videoId 
= mShow.Groups[1].Value;//Groups[0]為整個匹配對象(這裡是_url),Groups[1]才是第一個括號裡匹配的
                return true;
            }
            
else
            {
                Regex regList 
= new Regex(@"^http:\/\/v\.youku\.com\/v_playlist", RegexOptions.IgnoreCase);
                
if (regList.IsMatch(_url))
                {
                    
try
                    {
                        WebClient webClient 
= new WebClient();
                        
string youkuHtml = Encoding.ASCII.GetString(webClient.DownloadData(_url));
                        Regex regHtml 
= new Regex(@"(?is)var\s+(videoId2)+?\s*=\s*'([\w-]+)'\s*;", RegexOptions.IgnoreCase);
                        Match mHtml 
= regHtml.Match(youkuHtml);
                        
if (mHtml.Success)
                        {
                            videoId 
= mHtml.Groups[2].Value;
                            
return true;
                        }
                    }
                    
catch (Exception ex)
                    {
                        
return false;
                    }
                }
            }
            
return false;
        }
        
/// <summary>
        
/// 根據youku的視頻URL分析再請求其API獲取視頻信息
        
/// </summary>
        
/// <returns></returns>
        public string GetVideoInfo()
        {
            
try
            {
                
string videoId = string.Empty;
                
if (GetVideoId(ref videoId))
                {
                    WebClient webClient 
= new WebClient();
                    
string videoInfo = Encoding.ASCII.GetString(webClient.DownloadData(string.Format(videoApiUrl, videoId)));
                    
return videoInfo;
                }
                
else
                { 
return "error"; }
            }
            
catch (Exception ex)
            {
                
return "error";
            }
        }
    }

 

tudou Api源码下载(VS2010)
posted @ 2011-06-01 14:12  BePast  阅读(9692)  评论(10编辑  收藏  举报