diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HaoYue.CQTVShow.Hub.csproj b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HaoYue.CQTVShow.Hub.csproj index 21282bd..b6c5b4d 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HaoYue.CQTVShow.Hub.csproj +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HaoYue.CQTVShow.Hub.csproj @@ -64,6 +64,7 @@ MSBuild:Compile Designer + diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs index 53c36e1..92ea37b 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs @@ -75,6 +75,7 @@ namespace HaoYue.CQTVShow.Hub 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); @@ -88,6 +89,10 @@ namespace HaoYue.CQTVShow.Hub var list = CheckM3u8File(si.PlayListM3u8Url); + si.Title = htmldoc.DocumentNode.SelectNodes("//div[@class='v_message']/p[1]/span")[0].InnerHtml; + + si.Info = htmldoc.DocumentNode.SelectNodes("//div[@class='v_message']/p[3]/span")[0].InnerHtml; + var normalm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b3000")).FirstOrDefault(); if (normalm3u8 != null) { @@ -104,16 +109,12 @@ namespace HaoYue.CQTVShow.Hub } - private static List CheckM3u8File(string url) + public static List 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(); //string FileText = File.ReadAllText(FilePath); diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HttpHelper.cs b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HttpHelper.cs new file mode 100644 index 0000000..e35fb71 --- /dev/null +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HttpHelper.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYue.CQTVShow.Hub +{ + public static class HttpHelper + { + /// + /// Http方式下载文件 + /// + /// http地址 + /// 本地文件 + /// + public static bool Download(string url, string localfile) + { + bool flag = false; + long startPosition = 0; // 上次下载的文件起始位置 + FileStream writeStream; // 写入本地文件流对象 + + long remoteFileLength = GetHttpLength(url);// 取得远程文件长度 + System.Console.WriteLine("remoteFileLength=" + remoteFileLength); + if (remoteFileLength == 745) + { + System.Console.WriteLine("远程文件不存在."); + return false; + } + + // 判断要下载的文件夹是否存在 + if (File.Exists(localfile)) + { + + writeStream = File.OpenWrite(localfile); // 存在则打开要下载的文件 + startPosition = writeStream.Length; // 获取已经下载的长度 + + if (startPosition >= remoteFileLength) + { + System.Console.WriteLine("本地文件长度" + startPosition + "已经大于等于远程文件长度" + remoteFileLength); + writeStream.Close(); + + return false; + } + else + { + writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位 + } + } + else + { + writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存创建一个文件 + startPosition = 0; + } + + + try + { + HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接 + + if (startPosition > 0) + { + myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置 + } + + + Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流 + + + byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容 + int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次 + + long currPostion = startPosition; + + while (contentSize > 0)// 如果读取长度大于零则继续读 + { + currPostion += contentSize; + int percent = (int)(currPostion * 100 / remoteFileLength); + System.Console.WriteLine("percent=" + percent + "%"); + writeStream.Write(btArray, 0, contentSize);// 写入本地文件 + contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取 + } + + //关闭流 + writeStream.Close(); + readStream.Close(); + + flag = true; //返回true下载成功 + } + catch (Exception) + { + writeStream.Close(); + flag = false; //返回false下载失败 + } + + return flag; + } + + // 从文件头得到远程文件的长度 + private static long GetHttpLength(string url) + { + long length = 0; + + try + { + HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接 + HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); + + if (rsp.StatusCode == HttpStatusCode.OK) + { + length = rsp.ContentLength;// 从文件头得到远程文件的长度 + } + + rsp.Close(); + return length; + } + catch (Exception e) + { + return length; + } + + } + } +} diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml index 7989794..ea66339 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml @@ -17,6 +17,7 @@ Height="503" Width="409" VerticalAlignment="Top" Margin="6,47,0,0" SelectionChanged="ListBoxSelectionChanged" + Background="#EEEEEE" > @@ -33,7 +34,7 @@ - + @@ -44,24 +45,35 @@ -