下载功能 完成一半

This commit is contained in:
皓月 2018-10-06 23:24:02 +08:00
parent 4e627d2801
commit 0caf890b51
11 changed files with 5463 additions and 20 deletions

View File

@ -64,6 +64,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="HttpHelper.cs" />
<Compile Include="StartOtherProgarm.cs" /> <Compile Include="StartOtherProgarm.cs" />
<Compile Include="StaticClass.cs" /> <Compile Include="StaticClass.cs" />
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">

View File

@ -75,6 +75,7 @@ namespace HaoYue.CQTVShow.Hub
HtmlWeb htmlweb = new HtmlWeb(); HtmlWeb htmlweb = new HtmlWeb();
HtmlDocument htmldoc = htmlweb.Load(url); HtmlDocument htmldoc = htmlweb.Load(url);
string scriptstr = htmldoc.DocumentNode.SelectNodes("//div[@class='main']/script")[0].InnerHtml; string scriptstr = htmldoc.DocumentNode.SelectNodes("//div[@class='main']/script")[0].InnerHtml;
string[] strArr = scriptstr.Split('\n'); string[] strArr = scriptstr.Split('\n');
string vurl_1 = strArr[1].Substring(strArr[1].IndexOf("\"") + 1); string vurl_1 = strArr[1].Substring(strArr[1].IndexOf("\"") + 1);
@ -88,6 +89,10 @@ namespace HaoYue.CQTVShow.Hub
var list = CheckM3u8File(si.PlayListM3u8Url); 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(); var normalm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b3000")).FirstOrDefault();
if (normalm3u8 != null) if (normalm3u8 != null)
{ {
@ -104,16 +109,12 @@ namespace HaoYue.CQTVShow.Hub
} }
private static List<ModelOfM3u8> CheckM3u8File(string url) public static List<ModelOfM3u8> CheckM3u8File(string url)
{ {
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.Default); StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
//AddLog("开始解析m3u8文件"); //AddLog("开始解析m3u8文件");
var ALLList = new List<ModelOfM3u8>(); var ALLList = new List<ModelOfM3u8>();
//string FileText = File.ReadAllText(FilePath); //string FileText = File.ReadAllText(FilePath);

View File

@ -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
{
/// <summary>
/// Http方式下载文件
/// </summary>
/// <param name="url">http地址</param>
/// <param name="localfile">本地文件</param>
/// <returns></returns>
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;
}
}
}
}

View File

@ -17,6 +17,7 @@
Height="503" Width="409" Height="503" Width="409"
VerticalAlignment="Top" Margin="6,47,0,0" VerticalAlignment="Top" Margin="6,47,0,0"
SelectionChanged="ListBoxSelectionChanged" SelectionChanged="ListBoxSelectionChanged"
Background="#EEEEEE"
> >
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
@ -33,7 +34,7 @@
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
</ListBox> </ListBox>
<TabControl x:Name="TabMeun" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="505" Margin="7,2,0,0" SelectionChanged="MeunTabSelect"> <TabControl x:Name="TabMeun" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="741" Margin="7,2,0,0" SelectionChanged="MeunTabSelect">
<TabItem Header="生活麻辣烫"> <TabItem Header="生活麻辣烫">
<Grid Background="#FFE5E5E5"/> <Grid Background="#FFE5E5E5"/>
</TabItem> </TabItem>
@ -44,24 +45,35 @@
<Grid Background="#FFE5E5E5"/> <Grid Background="#FFE5E5E5"/>
</TabItem> </TabItem>
</TabControl> </TabControl>
<Label x:Name="LabelState" Content="状态" HorizontalAlignment="Left" Margin="4,586,0,0" VerticalAlignment="Top"/> <Label x:Name="LabelState" Content="状态" HorizontalAlignment="Left" Margin="6,584,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.235,0.36"/>
<Image x:Name="Image_SelectedShow" HorizontalAlignment="Left" MaxHeight="300" MaxWidth="300" Margin="450,40,0,0" VerticalAlignment="Top" /> <Image x:Name="Image_SelectedShow" HorizontalAlignment="Left" MaxHeight="300" MaxWidth="300" Margin="450,48,0,0" VerticalAlignment="Top" />
<Label x:Name="Label_SelectedName" Content="-" HorizontalAlignment="Left" Margin="450,310,0,0" VerticalAlignment="Top" <Label x:Name="Label_SelectedName" Content="-" HorizontalAlignment="Left" Margin="450,310,0,0" VerticalAlignment="Top"
FontSize="20" FontWeight="Bold"/> FontSize="20" FontWeight="Bold"/>
<Label x:Name="Label_SelectedTime" Content="-" HorizontalAlignment="Left" Margin="450,350,0,0" VerticalAlignment="Top" <Label x:Name="Label_SelectedTime" Content="-" HorizontalAlignment="Left" Margin="450,350,0,0" VerticalAlignment="Top"
FontSize="18" FontWeight="Bold"/> FontSize="18" FontWeight="Bold"/>
<Button x:Name="BtnPlay" Content="播 放" HorizontalAlignment="Left" Margin="459,400,0,0" VerticalAlignment="Top" Width="75" <Button x:Name="BtnPlay" Content="播 放" HorizontalAlignment="Left" Margin="450,400,0,0" VerticalAlignment="Top" Width="95"
FontSize="18" FontWeight="Bold" Click="PlayMedia"/> FontSize="18" FontWeight="Bold" Click="PlayMedia"/>
<Button x:Name="BtnDown" Content="下 载" HorizontalAlignment="Left" Margin="550,400,0,0" VerticalAlignment="Top" Width="75" <Button x:Name="BtnDown" Content="下 载" HorizontalAlignment="Left" Margin="560,400,0,0" VerticalAlignment="Top" Width="95"
FontSize="18" FontWeight="Bold" Click="DownLoadClicked"/> FontSize="18" FontWeight="Bold" Click="DownLoadClicked"/>
<TextBox x:Name="TextPageIndex" IsEnabled="False" HorizontalAlignment="Left" Height="27" Margin="77,555,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="19"/> <TextBox x:Name="TextPageIndex" IsEnabled="False" HorizontalAlignment="Left" Height="27" Margin="77,555,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="27"/>
<Button x:Name="Btn_UpPage" Content="上一页" IsEnabled="False" HorizontalAlignment="Left" Margin="17,555,0,0" VerticalAlignment="Top" Width="52" Height="26" <Button x:Name="Btn_UpPage" Content="上一页" IsEnabled="False" HorizontalAlignment="Left" Margin="17,555,0,0" VerticalAlignment="Top" Width="52" Height="26"
Click="UpPage"/> Click="UpPage"/>
<Button x:Name="Btn_NextPage" Content="下一页" HorizontalAlignment="Left" Margin="104,555,0,0" VerticalAlignment="Top" Width="52" Height="26" <Button x:Name="Btn_NextPage" Content="下一页" HorizontalAlignment="Left" Margin="112,555,0,0" VerticalAlignment="Top" Width="52" Height="26"
Click="NextPage" Click="NextPage"
/> />
<Label Content="Powered by axibug.com" HorizontalAlignment="Left" Margin="600,579,0,0" VerticalAlignment="Top" /> <Label Content="节目来源及版权归视界网/重庆网络广播电视台所有" HorizontalAlignment="Left" Margin="316,584,0,0" VerticalAlignment="Top" />
<Label Content="Powered by axibug.com" HorizontalAlignment="Left" Margin="600,584,0,0" VerticalAlignment="Top" />
<Grid HorizontalAlignment="Left" Height="68" Margin="450,482,0,0" VerticalAlignment="Top" Width="298" Background="#EEEEEE">
<Label Content="下载任务" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label Content="任务名称:" HorizontalAlignment="Left" Margin="0,21,0,0" VerticalAlignment="Top"/>
<Label x:Name="LabelDownloadMediaTitle" Content="-" HorizontalAlignment="Left" Margin="62,21,0,0" VerticalAlignment="Top"/>
<Label Content="下载进度:" HorizontalAlignment="Left" Margin="0,44,0,0" VerticalAlignment="Top"/>
<ProgressBar x:Name="MyDownLoadProgressBar" HorizontalAlignment="Left" Height="10" Margin="67,51,0,0" VerticalAlignment="Top" Width="191"/>
<Label Content="打开目录" HorizontalAlignment="Left" Margin="238,24,0,0" VerticalAlignment="Top" FontSize="9" />
<Label x:Name="MyDownLoadProgressText" Content="-%" HorizontalAlignment="Left" Margin="260,44,0,0" VerticalAlignment="Top" FontSize="10" FontWeight="Bold"/>
<Button Content="取消下载" IsEnabled="False" HorizontalAlignment="Left" Margin="213,3,0,0" VerticalAlignment="Top" Width="75" Height="10"/>
</Grid>
</Grid> </Grid>
</controls:MetroWindow> </controls:MetroWindow>

View File

@ -31,10 +31,12 @@ namespace HaoYue.CQTVShow.Hub
InitializeComponent(); InitializeComponent();
SetShowList(0); SetShowList(0);
this.BtnPlay.IsEnabled = false; this.BtnPlay.IsEnabled = false;
this.ShowMessageAsync("欢迎使用\n皓月云-重庆本土节目播放器\nver 0.1.0 预览版", "本软件目的是给重庆本土电视节目爱好者,\n一个便捷的节目选择器,便捷的观看体验\n节目来源和版权均归 重庆网络广播电视台 所有\n\n请在节目列表选择要观看的栏目", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" }); this.BtnDown.IsEnabled = false;
this.ShowMessageAsync("欢迎使用\n皓月云-重庆本土节目播放器\nver 0.1.0 预览版", "免责声明:本软件仅作为便于重庆卫视观众的节目选择器功能的民间软件工具作品、意在便捷的观看体验之用。\n\n本软件作者也是重庆广电忠实粉丝,本软件作品仅帮助用户做便捷节目选择菜单排列,本身不提供任何数据服务。\n节目来源和版权均归 重庆网络广播电视台 所有\n\n使用本软件代表您认可以上说法\n\n---- 90后15年来重庆时尚频道老粉丝 皓月", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" });
} }
int frist = 1; int frist = 1;
string mypath = Environment.CurrentDirectory;
private void MeunTabSelect(object sender, SelectionChangedEventArgs e) private void MeunTabSelect(object sender, SelectionChangedEventArgs e)
{ {
@ -83,6 +85,7 @@ namespace HaoYue.CQTVShow.Hub
public delegate void myboolsetter(bool emmmmmmbool); public delegate void myboolsetter(bool emmmmmmbool);
public delegate void myintsetter(int num); public delegate void myintsetter(int num);
public delegate void mystringsetter(string str); public delegate void mystringsetter(string str);
public delegate void mydoublesetter(double value);
public void SetListBoxShow() public void SetListBoxShow()
{ {
@ -111,15 +114,41 @@ namespace HaoYue.CQTVShow.Hub
this.ListBoxShow.SelectedIndex = index; this.ListBoxShow.SelectedIndex = index;
} }
public void SetProgressBar(double value)
{
this.MyDownLoadProgressBar.Value = value;
this.MyDownLoadProgressText.Content = ((int)value).ToString() + "%";
}
public void SetLabelDownloadMediaTitle(string value)
{
this.LabelDownloadMediaTitle.Content = value;
}
/// <summary>
/// 根据节目列表选择 显示详情
/// </summary>
/// <param name="index"></param>
public void SetShowSelected(int index) public void SetShowSelected(int index)
{ {
//BitmapImage image = new BitmapImage(new Uri(str, UriKind.Absolute)); ThreadPool.QueueUserWorkItem((obj) =>
{
this.Image_SelectedShow.Dispatcher.BeginInvoke(new myintsetter(SetPlayeInfoForSelectIndex), index);
}, null);
}
public void SetPlayeInfoForSelectIndex(int index)
{
byte[] btyarray = GetImageFromResponse(StaticClass.StaticShowList[index].ImageSrc, null); byte[] btyarray = GetImageFromResponse(StaticClass.StaticShowList[index].ImageSrc, null);
MemoryStream ms = new MemoryStream(btyarray); MemoryStream ms = new MemoryStream(btyarray);
this.Image_SelectedShow.Source = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.Default); this.Image_SelectedShow.Source = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.Default);
this.Label_SelectedName.Content = StaticClass.StaticShowList[index].Title; this.Label_SelectedName.Content = StaticClass.StaticShowList[index].Title;
this.Label_SelectedTime.Content = StaticClass.StaticShowList[index].Time; this.Label_SelectedTime.Content = StaticClass.StaticShowList[index].Time;
//赋值到临时需解析的播放页面Url
StaticClass.PlayUrl = StaticClass.StaticShowList[index].ToPath; StaticClass.PlayUrl = StaticClass.StaticShowList[index].ToPath;
//装载到选中静态类
StaticClass.StaticShowInfo = HtmlDoIt.GetShowInfo(StaticClass.PlayUrl);
this.BtnPlay.IsEnabled = true; this.BtnPlay.IsEnabled = true;
this.BtnDown.IsEnabled = true; this.BtnDown.IsEnabled = true;
} }
@ -139,6 +168,8 @@ namespace HaoYue.CQTVShow.Hub
if (SelectedIndex < 0) if (SelectedIndex < 0)
return; 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);
@ -206,7 +237,7 @@ namespace HaoYue.CQTVShow.Hub
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); //StaticClass.StaticShowInfo = HtmlDoIt.GetShowInfo(StaticClass.PlayUrl);
string playm3u8url; string playm3u8url;
if (StaticClass.StaticShowInfo.PlayHDM3u8Url != null) if (StaticClass.StaticShowInfo.PlayHDM3u8Url != null)
@ -218,7 +249,7 @@ namespace HaoYue.CQTVShow.Hub
this.ShowMessageAsync("错误", "视频资源有误", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" }); this.ShowMessageAsync("错误", "视频资源有误", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" });
Process myprocess = new Process(); Process myprocess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(Environment.CurrentDirectory + @"\\PotPlayer\\PotPlayerMini.exe", StaticClass.StaticShowInfo.PlayHDM3u8Url); ProcessStartInfo startInfo = new ProcessStartInfo(mypath + @"\\PotPlayer\\PotPlayerMini.exe", StaticClass.StaticShowInfo.PlayHDM3u8Url);
myprocess.StartInfo = startInfo; myprocess.StartInfo = startInfo;
myprocess.StartInfo.UseShellExecute = false; myprocess.StartInfo.UseShellExecute = false;
myprocess.Start(); myprocess.Start();
@ -240,7 +271,81 @@ namespace HaoYue.CQTVShow.Hub
private void DownLoadClicked(object sender, RoutedEventArgs e) private void DownLoadClicked(object sender, RoutedEventArgs e)
{ {
this.ShowMessageAsync("喵喵喵", "下载功能暂时没有做呢", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" }); //this.ShowMessageAsync("喵喵喵", "下载功能暂时没有做呢", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" });
string SiteDir = StaticClass.PlayUrl.Substring(0, StaticClass.PlayUrl.LastIndexOf('/')) + "/";
ThreadPool.QueueUserWorkItem((obj) =>
{
//this.LabelState.Dispatcher.BeginInvoke(new mystringsetter(SetLabelState), "开始解析m3u8文件");
var list = HtmlDoIt.CheckM3u8File(StaticClass.StaticShowInfo.PlayHDM3u8Url);
//创建工作目录
string ThisTaskDir = mypath + "\\Download\\" + DateTime.Now.ToString("yyyyMMdd-hhmmss.FFF") + "\\";
if (!Directory.Exists(ThisTaskDir))
{
Directory.CreateDirectory(ThisTaskDir);
//AddLog("创建媒体任务目录 " + ThisTaskDir);
}
this.LabelDownloadMediaTitle.Dispatcher.BeginInvoke(new mystringsetter(SetLabelDownloadMediaTitle), StaticClass.StaticShowInfo.Title);
//如果队列中还有未进行的任务则进行
while (list.Where(w => w.State == 0).Count() > 0)
{
var taskinfo = list.Where(w => w.State == 0).FirstOrDefault();
if (taskinfo != null)
{
list[list.IndexOf(taskinfo)].State = 1;
//AddLog("开始下载文件" + taskinfo.Path);
HttpHelper.Download(SiteDir + taskinfo.Path, ThisTaskDir + taskinfo.Index.ToString("000000") + ".ts");
////更新进度条
this.MyDownLoadProgressBar.Dispatcher.BeginInvoke(new mydoublesetter(SetProgressBar),
((double)list.Where(w => w.State != 0).Count() / (double)list.Count()) * 100.0f
);
//AddLog("" + taskinfo.Path + " 下载完毕");
}
}
CommanderStr(ThisTaskDir);
//AddLog("进程结束");
}, null);
}
public void CommanderStr(string FileItemDir)
{
string c = @"copy /b " + FileItemDir + "\\*.ts " + FileItemDir + "\\final.ts";
Cmd(c);
}
/// <summary>
/// 执行Cmd命令
/// </summary>
private void Cmd(string c)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardInput.WriteLine(c);
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine("exit");
StreamReader reader = process.StandardOutput;//截取输出流
process.WaitForExit();
}
catch
{ }
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
DAUMPLAYLIST
playname=http://sjvodcdn.cbg.cn:1935/app_1/_definst_/smil:getnew/sobeyget/vod/2018/10/05/9712fe7e4408426399c8cc9a065690d3/1538749629_4698.smil/chunklist_b500000.m3u8
playtime=134618
topindex=0
1*file*http://sjvodcdn.cbg.cn:1935/app_1/_definst_/smil:getnew/sobeyget/vod/2018/10/05/9712fe7e4408426399c8cc9a065690d3/1538749629_4698.smil/chunklist_b500000.m3u8
1*played*0
1*duration2*2859455
1*start*134618