Silverlight官网提供了好多的视频,也提供了下载地址,然则一个一个打开网页下载,470多个视频急需多长时间? 既是我们都是程序员,当然要找个办法批量下载。 这是我找到的地址: [资料下载]sl批量下载URL.rar 现下来说说何以下载,以次2种步骤解决问题 最开始的见地:
Silverlight官网提供了好多的视频,也提供了下载地址,然则一个一个打开网页下载,470多个视频亟需多长时间?
既是我们都是程序员,当然要找个办法批量下载。
这是我找到的地址:
[资料下载] sl批量下载URL.rar
现时来说说何以下载,以次2种步骤解决问题
最开始的见地:
一、“爬”网页
既是网页提供下载,那只要“爬”每个SL视频网页,其后用正则解析,天然便OK了。
第一打开SL视频的网页:http://silverlight.net/learn/videos/all/
这边展示了全部SL视频,右键点击,查看网页源代码,看到全部视频的地址都是这么的
< a href ="/learn/videos/all/http-request-with-httpwebrequest" >....
现时就该上程序,找到全部的地址了
Regex reg = new Regex( " " );
var match = reg.Match(html);
while (match.Success)
{
// anchors.Add(match.Value);
anchors.Add( " http://silverlight.net " + match.Groups[ 一 ].Value);
match = match.NextMatch();
}
这是我找到的全部联接地址
http://silverlight.net/learn/videos/all/Basic-Animation-Silverlight-三
http://silverlight.net/learn/videos/all/RichTextArea-Part-二
http://silverlight.net/learn/videos/all/Duplex-Services-in-Silverlight-三
http://silverlight.net/learn/videos/all/Change-Styles-Runtime-Silverlight-三
http://silverlight.net/learn/videos/all/Use-Isolated-Storage-SL三-Out-of-Browser
http://silverlight.net/learn/videos/all/Out-Of-Stream-Data-Access
http://silverlight.net/learn/videos/all/Access-Web-Camera-Microphone
http://silverlight.net/learn/videos/all/BiDi-Right-to-Left
http://silverlight.net/learn/videos/all/Right-Click-Mouse-Events
http://silverlight.net/learn/videos/all/Building-Custom-Bitrate-Meter
http://silverlight.net/learn/videos/all/Creating-Custom-Timeline-Markers
http://silverlight.net/learn/videos/all/Hosting-HTML-Content
http://silverlight.net/learn/videos/all/Using-the-ViewBox-Control
http://silverlight.net/learn/videos/all/Accessing-Global-Clipboard
http://silverlight.net/learn/videos/all/Notification-API
http://silverlight.net/learn/videos/all/MouseWheel-API
....
既是找到了SL视频的详细页地址,然后便是对每个页面“爬网”了,这边我们采取异步步骤,提高效率
public static ManualResetEvent _allDone = new ManualResetEvent( false );
public static void Download()
{
var url = "" ;
using (StreamReader reader = new StreamReader( " all-silverlight-vedio-detail-url " ))
{
url = reader.ReadToEnd();
}
var urlArray = url.Split( new string [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
_totalFiles = urlArray.Length;
for ( int i = 零 ; i < _totalFiles; i ++ )
{
var request = HttpWebRequest.Create(urlArray[i]);
request.BeginGetResponse(ResponseCallback, request);
}
_allDone.WaitOne();
}
static int _totalFiles;
static int _filesFlag;
private static void ResponseCallback(IAsyncResult asynchronousResult)
{
var request = (WebRequest)asynchronousResult.AsyncState;
var response = request.EndGetResponse(asynchronousResult);
var stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
var name = GetFileNameFromUri(request.RequestUri.AbsolutePath);
var html = reader.ReadToEnd();
using (StreamWriter writer = new StreamWriter( " D://silverlight-vedios// " + name))
writer.Write(html);
}
_filesFlag ++ ;
if (_filesFlag == _totalFiles)
_allDone.Set();
}
private static string GetFileNameFromUri( string p)
{
var i = p.LastIndexOf( ' / ' );
var s = p.Substring(i + 一 );
return s;
}
下载完全部视频网页以后,快要开始对网页进展分析,提取全部WMV视频,代码略
之上步骤貌似很圆满了,可是我们依然要采取第二种步骤,
如其大伙儿试过以后就会晓得,微软付出的下载视频因特网址,很多是失灵的链接
视频470多个,而找到的视频下载地址唯有380多个
二、从微软提供的web service下载
思路:SL播放器肯定是通用的,想想微软也不可能为每个视频做个播放器
既是播放器通用,那肯定有地方获取要播发的视频地址
思路有了,那我们即将从SL播放器动手。
打开随意一个SL视频网页,其后查看源代码,我们可以看到这么一段
< object id ="slMediaPlayer" style ="width:400px;height:338px" autoupdate ="true" data ="data:application/x-silverlight-二," type ="application/x-silverlight-二" >
< param name ="MinRuntimeVersion" value ="3.0.40624.0" />
< param name ="source" value =" /ClientBin/mediaplayer/MSCommunities.MediaPlayer.xap " />
< param name ="InitParams" value ="videoid=34056,username=Anonymous" />
< a href ="http://go.microsoft.com/fwlink/?LinkId=149156 " style ="text-decoration:none" >
< img src ="http://i一.silverlight.net/resources/images/content/misc/Install-Silverlight-400x338-VideoSize.png?cdn_id=20091118_三 " alt ="Please install Silverlight or click download to watch video locally." />
上边我上色的地方,乃是SL的地址,前边加上因特网址:http://silverlight.net/ /ClientBin/mediaplayer/MSCommunities.MediaPlayer.xap
下载以后,将XAP的后缀改成ZIP,其后打开,可以看到以次资料
写过SL程序的人,一看到这些资料应当立马就明白了,他引述的是WCF服务!!
2个主要的资料,急需我们去见见
一、WCF配置文件:ServiceReferences.ClientConfig
二、播放器DLL资料:MSCommunities.MediaPlayer.dll
打开ServiceReferences.ClientConfig,我们可以一眼看到摘引的service地址
< configuration >
< system .serviceModel >
< bindings >
< basicHttpBinding >
< binding name ="BasicHttpBinding_MediaPlayer" maxBufferSize ="2147483647"
maxReceivedMessageSize ="2147483647" >
< security mode ="None" >
< transport >
< extendedProtectionPolicy policyEnforcement ="Never" />
< client >
< endpoint address ="http://www.silverlight.net/services/mediaplayer.svc "
binding ="basicHttpBinding" bindingConfiguration ="BasicHttpBinding_MediaPlayer"
contract ="Services.Silverlight.MediaPlayer" name ="BasicHttpBinding_MediaPlayer" />
WCF地址:http://www.silverlight.net/services/mediaplayer.svc
我们将这段地址COPY,其后在浏览器中打开,没加密,会打开,GREAT!
其后我们建立一个工程,将这段地址以Service Reference的形式引来,工程门类不限
引出Service以后,我们可以看到几个十分有用的种,此中我们会用到的2个:
MediaPlayerClient (获取VEDIO便靠他了)
VedioInfo (视频对象种,我们会用到它的VideoSourceUri属性)
现下开始分析MediaPlayerClient,打开来看到里头有个步骤GetVideo,参数是ID和USERNAME,回来值是VedioInfo太好了,步骤现成的!
可是ID,和USERNAME,我们到哪里去找呢?
实则对SL程序相熟的人应当已经晓得何以操作了,直接看网页,下部的稿件是对不熟悉SL的人讲的。
还记得我上边谈到的这个MSCommunities.MediaPlayer.dll 吗
微软的播放器就用它来获取地址的,那这个DLL里边肯定有获取地址的步骤,何以找出来呢,上Reflector!
用Reflector打开这个DLL
解释停:
MSCommunities.MediaPlayer :播放器的有关种
MSCommunities.MediaPlayer.Services.Silverlight: SL播放器引述上头的WCF地址的署理种
天然,SL播放器也会用到MediaPlayerClient这个种,打开这个种,看到这个步骤
public void GetVideoAsync( string id, string userName);
由于SL引述WCF只好用异步步骤
好了,现时我们只要找出SL播放器何以运用这个步骤,便晓得何以调用了
最容易的步骤,导出整个DLL,其后搜寻GetVideoAsync这个步骤,相信聪明的你肯定能找出这个步骤在哪儿调用的
最后,我们找出这个步骤的调征地方,在MSCommunities.MediaPlayer 定名空间停的Page 种中
private void InitGetVideoAsync()
{
this .m_service.GetVideoAsync(Application.Current.Resources[ " VideoID " ].ToString(), Application.Current.Resources[ " UserName " ].ToString());
}
找出了!
它是从这2个地方获取ID和USERNAME的
Application.Current.Resources[" VideoID " ] //获取ID
Application.Current.Resources[" UserName " ] //获取USERNAME
最后,我们只要知道程序在哪儿加载这2个信息的就行了
熟习SL的人肯定一下子就能想到是在APP里边加载的
不熟悉的人可以搜寻,Application.Current.Resources是ResourceDictionary门类,加载自然会用到Add步骤
好了,来见见APP种的Application_Startup步骤做了什么
private void Application_Startup( object sender, StartupEventArgs e)
{
Application.Current.Resources.Add( " VideoID " , e.InitParams[ " videoid " ]);
....
}
诠释:InitParams,获取作为 Silverlight 插件的 HTML 初始化的部分传接的初始化参数。
即,从网页的< param name ="InitParams" 获取参数
好了,我们再回过火见见最开始的,嵌入SL的一部分
< object id ="slMediaPlayer" style ="width:400px;height:338px" autoupdate ="true" data ="data:application/x-silverlight-二," type ="application/x-silverlight-二" >
< param name ="MinRuntimeVersion" value ="3.0.40624.0" />
< param name ="source" value ="/ClientBin/mediaplayer/MSCommunities.MediaPlayer.xap" />
< param name ="InitParams" value ="videoid=34056,username=Anonymous" />
< a href ="http://go.microsoft.com/fwlink/?LinkId=149156" style ="text-decoration:none" >
< img src ="http://i一.silverlight.net/resources/images/content/misc/Install-Silverlight-400x338-VideoSize.png?cdn_id=20091118_三" alt ="Please install Silverlight or click download to watch video locally." />
现下见见< param name ="InitParams" value ="videoid=34056,username=Anonymous" />
看到了吧,vedioid 和 username都在这里了
剩余的事儿乃是利用我们下载的每个视频网页,找到每个视频的ID,其后用MediaPlayerClient下载即可
本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception