m3u8解析到临时数据

This commit is contained in:
皓月 2018-10-06 13:33:14 +08:00
parent 05f567279e
commit 920e63996e
3 changed files with 105 additions and 2 deletions

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Text; using System.Text;
using HtmlAgilityPack; using HtmlAgilityPack;
using System.IO;
using System.Net;
namespace HaoYue.CQTVShow.Hub namespace HaoYue.CQTVShow.Hub
{ {
@ -61,5 +63,74 @@ namespace HaoYue.CQTVShow.Hub
return ""; return "";
} }
public static ShowInfo GetShowInfo(string url)
{
HtmlWeb htmlweb = new HtmlWeb();
HtmlDocument htmldoc = htmlweb.Load(url);
string scriptstr = htmldoc.DocumentNode.SelectNodes("//div[@class='main']/script")[0].InnerHtml;
string[] strArr = scriptstr.Split('\n');
string vurl_1 = strArr[1].Substring(strArr[1].IndexOf("\"") + 1);
string vurl = vurl_1.Substring(0, vurl_1.LastIndexOf('\"'));
string url_1 = strArr[6].Substring(strArr[6].IndexOf("\'") + 1);
string url_2 = url_1.Substring(0, url_1.LastIndexOf('\''));
ShowInfo si = new ShowInfo();
si.PlayListM3u8Url = url_2.Replace("' + vurl + '", vurl);
var list = CheckM3u8File(si.PlayListM3u8Url);
var normalm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b3000")).FirstOrDefault();
if (normalm3u8 != null)
{
si.PlayNormalM3u8Url = si.PlayListM3u8Url.Substring(0, si.PlayListM3u8Url.LastIndexOf("/")+1) + normalm3u8.Path;
}
var hdm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b5000")).FirstOrDefault();
if (hdm3u8 != null)
{
si.PlayNormalM3u8Url = si.PlayListM3u8Url.Substring(0, si.PlayListM3u8Url.LastIndexOf("/")+1) + normalm3u8.Path;
}
return si;
}
private static List<ModelOfM3u8> CheckM3u8File(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
//AddLog("开始解析m3u8文件");
var ALLList = new List<ModelOfM3u8>();
//string FileText = File.ReadAllText(FilePath);
string FileText = sr.ReadToEnd();
int i = 0;
foreach (string s in FileText.Split('\n'))
{
if (s.Trim().Length > 0 && s.Trim().Substring(0, 1) != "#")//去空格后长度不为1 且不是注释
{
int filetype = 0;
if (s.Substring(s.LastIndexOf(".")).Contains("m3u8"))
{
filetype = 1;
}
ALLList.Add(new ModelOfM3u8() { Path = s, filetype = filetype, State = 0, Index = ++i });
}
}
//AddLog("解析完毕,共" + ALLList.Where(w => w.filetype == 0).Count() + "个ts视频切片,共" + ALLList.Where(w => w.filetype == 1).Count() + "个m3u8配置文件");
return ALLList;
}
} }
} }

View File

@ -116,6 +116,11 @@ namespace HaoYue.CQTVShow.Hub
{ {
//获取选择的节目 //获取选择的节目
int SelectedIndex = this.ListBoxShow.SelectedIndex; int SelectedIndex = this.ListBoxShow.SelectedIndex;
//大概是重新装载列表的时候也会出发选择事件、然而这时候选择的Index会为-1 执行则会错误
if (SelectedIndex < 0)
return;
ThreadPool.QueueUserWorkItem((obj) => ThreadPool.QueueUserWorkItem((obj) =>
{ {
this.Image_SelectedShow.Dispatcher.BeginInvoke(new myintsetter(SetShowSelected), SelectedIndex); this.Image_SelectedShow.Dispatcher.BeginInvoke(new myintsetter(SetShowSelected), SelectedIndex);
@ -182,7 +187,8 @@ namespace HaoYue.CQTVShow.Hub
/// <param name="e"></param> /// <param name="e"></param>
private void PlayMedia(object sender, RoutedEventArgs e) private void PlayMedia(object sender, RoutedEventArgs e)
{ {
System.Diagnostics.Process.Start(StaticClass.PlayUrl); //System.Diagnostics.Process.Start(StaticClass.PlayUrl);
StaticClass.StaticShowInfo = HtmlDoIt.GetShowInfo(StaticClass.PlayUrl);
} }
} }
} }

View File

@ -9,6 +9,7 @@ namespace HaoYue.CQTVShow.Hub
{ {
public static List<StaticShow> StaticShowList = new List<StaticShow>(); public static List<StaticShow> StaticShowList = new List<StaticShow>();
public static string PlayUrl = ""; public static string PlayUrl = "";
public static ShowInfo StaticShowInfo = new ShowInfo();
} }
@ -22,4 +23,29 @@ namespace HaoYue.CQTVShow.Hub
public string Time { get; set; } public string Time { get; set; }
public string ImageSrc { get; set; } public string ImageSrc { get; set; }
} }
public class ShowInfo
{
public string Title { get; set; }
public string Info { get; set; }
public string PlayList { get; set; }
public string PlayListM3u8Url { get; set; }
public string PlayNormalM3u8Url { get; set; }
public string PlayHDM3u8Url { get; set; }
}
public class ModelOfM3u8
{
public string Path { get; set; }
/// <summary>
/// [0] 视频格式 [1]m3u8文件
/// </summary>
public int filetype { get; set; }
/// <summary>
/// [0]未处理[1]在下中[2]已下载[3]处理失败
/// </summary>
public int State { get; set; }
public int Index { get; set; }
}
} }