Created
March 2, 2015 08:45
-
-
Save edward-hsu-1994/1444b6eb10631e19aae0 to your computer and use it in GitHub Desktop.
GetDailymationMedia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Net; | |
using System.Collections; | |
using Alex.Data; | |
using Alex.Syntax; | |
using Alex.Data.Json; | |
namespace Alex.Social.MediaGet.Parsers { | |
public class DailymationParser : Parser { | |
public override MediaInfo[] GetMediaInfos(Uri Uri) { | |
if(!IsMatch(Uri))//檢查網址是否符合規範 | |
throw new UriNotMatchException(Uri);//如果不符合網址規範返回例外 | |
string EmbedUrl = string.Format("http://www.dailymotion.com/embed/video/{0}",GetMediaId(Uri)); | |
JsonObject Info = GetMediaJson(EmbedUrl);//取得Json物件 | |
string AUDIO = Info["stream_audio_url"] as string;//取出影片真實位置 | |
string H264_HD1080 = Info["stream_h264_hd1080_url"] as string; | |
string H264_HD = Info["stream_h264_hd_url"] as string; | |
string H264_HQ = Info["stream_h264_hq_url"] as string; | |
string H264_LD = Info["stream_h264_ld_url"] as string; | |
string H264 = Info["stream_h264_url"] as string; | |
ArrayList result = new ArrayList(); | |
MediaInfo Templet = new MediaInfo();//資料樣板 | |
Templet.Parser = ParserEnum.Dailymation; | |
Templet.SourceUrl = Uri; | |
Templet.Name = Info["title"] as string; | |
Templet.Attributes.AddWithValue("author" ,Info["owner.username"]);//使用者帳號 | |
Templet.Attributes.Add(Info.GetWithKey("duration"));//影片長度 | |
Templet.Attributes.AddWithValue("thumb" ,Info["thumbnail_url"]);//縮圖網址 | |
Templet.Attributes.Add(Info.GetWithKey("language"));//語系 | |
if(AUDIO != null) { | |
MediaInfo audio = Templet.Clone(); | |
audio.Type = MediaType.Audio; | |
audio.RealUrl = new Uri[] { new Uri(AUDIO) }; | |
audio.Attributes.AddWithValue("type" ,"mp3"); | |
result.Add(audio); | |
} | |
if(H264_HD1080 != null) { | |
MediaInfo hd1080 = Templet.Clone(); | |
hd1080.Type = MediaType.Video; | |
hd1080.RealUrl = new Uri[] { new Uri(H264_HD1080) }; | |
hd1080.Attributes.AddWithValue("quality" ,"hd1080"); | |
hd1080.Attributes.AddWithValue("size" ,GetSize(H264_HD1080)); | |
hd1080.Attributes.AddWithValue("type" ,"mp4"); | |
result.Add(hd1080); | |
} | |
if(H264_HD != null) { | |
MediaInfo hd = Templet.Clone(); | |
hd.Type = MediaType.Video; | |
hd.RealUrl = new Uri[] { new Uri(H264_HD) }; | |
hd.Attributes.AddWithValue("quality" ,"hd"); | |
hd.Attributes.AddWithValue("size" ,GetSize(H264_HD)); | |
hd.Attributes.AddWithValue("type" ,"mp4"); | |
result.Add(hd); | |
} | |
if(H264_HQ != null) { | |
MediaInfo hq = Templet.Clone(); | |
hq.Type = MediaType.Video; | |
hq.RealUrl = new Uri[] { new Uri(H264_HQ) }; | |
hq.Attributes.AddWithValue("quality" ,"hq"); | |
hq.Attributes.AddWithValue("size" ,GetSize(H264_HQ)); | |
hq.Attributes.AddWithValue("type" ,"mp4"); | |
result.Add(hq); | |
} | |
if(H264_LD != null) { | |
MediaInfo ld = Templet.Clone(); | |
ld.Type = MediaType.Video; | |
ld.RealUrl = new Uri[] { new Uri(H264_LD) }; | |
ld.Attributes.AddWithValue("quality" ,"ld"); | |
ld.Attributes.AddWithValue("size" ,GetSize(H264_LD)); | |
ld.Attributes.AddWithValue("type" ,"mp4"); | |
result.Add(ld); | |
} | |
if(H264 != null) { | |
MediaInfo def = Templet.Clone(); | |
def.Type = MediaType.Video; | |
def.RealUrl = new Uri[] { new Uri(H264) }; | |
def.Attributes.AddWithValue("quality" ,"default"); | |
def.Attributes.AddWithValue("size" ,GetSize(H264)); | |
def.Attributes.AddWithValue("type" ,"mp4"); | |
result.Add(def); | |
} | |
return result.ToArray<MediaInfo>();//回傳結果 | |
} | |
private string GetMediaId(Uri Uri) {//取得影片ID | |
string result = Uri.Segments.Last<string>(); | |
result = result.Substring(0 ,result.IndexOf("_")); | |
return result; | |
} | |
private JsonObject GetMediaJson(string Url) {//取得影片JSON物件 | |
WebClient browser = new WebClient(); | |
byte[] data = browser.DownloadData(Url); | |
string html = Encoding.UTF8.GetString(data); | |
html = html.InnerString("var info =" ,"fields ="); | |
html = html.Substring(0 ,html.LastIndexOf(",")); | |
JsonObject obj = JsonObject.Parse(html); | |
return obj; | |
} | |
private string GetSize(string RealUrl) {//從真實位址網址中取得SIZE | |
return RealUrl.InnerString("-" ,"/"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment