Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Harlan-H committed Jul 5, 2023
2 parents 5e6993d + 693be04 commit 3b02b14
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 61 deletions.
3 changes: 1 addition & 2 deletions M3u8Downloader_H.Core/DownloaderManagers/DownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,13 @@ public IDownloadManager WithTimeout(int second)
return this;
}


public async Task GetM3U8FileInfo(CancellationToken cancellationToken)
{
_m3U8FileInfoMananger.TimeOuts = _timeOut;
if (_m3UFileInfo is not null)
return;

_url = await _m3U8UriManager.GetM3u8UriAsync(_url, _timeOut , cancellationToken);
_url = await _m3U8UriManager.GetM3u8UriAsync(_url, 0 , cancellationToken);

M3UFileInfo m3UFileInfo = await _m3U8FileInfoMananger.GetM3u8FileInfo(_url, m3uContent, _headers, cancellationToken);

Expand Down
4 changes: 2 additions & 2 deletions M3u8Downloader_H.Core/M3u8Downloader_H.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Version>3.12.10.0</Version>
<Version>3.13.1.0</Version>
<Authors>Harlan</Authors>
<AssemblyVersion>3.12.10.0</AssemblyVersion>
<AssemblyVersion>3.13.1.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion M3u8Downloader_H.Core/M3u8UriManagers/IM3u8UriManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace M3u8Downloader_H.Core.M3u8UriManagers
{
internal interface IM3u8UriManager
{
Task<Uri> GetM3u8UriAsync(Uri uri,int timeout, CancellationToken cancellationToken);
Task<Uri> GetM3u8UriAsync(Uri uri,int reserve0, CancellationToken cancellationToken);
}
}
2 changes: 1 addition & 1 deletion M3u8Downloader_H.Core/M3u8UriManagers/M3u8UriManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace M3u8Downloader_H.Core.M3u8UriManagers
{
internal class M3u8UriManager : IM3u8UriManager
{
public Task<Uri> GetM3u8UriAsync(Uri uri, int timeout, CancellationToken cancellationToken)
public Task<Uri> GetM3u8UriAsync(Uri uri, int reserve0, CancellationToken cancellationToken)
{
return Task.FromResult(uri);
}
Expand Down
5 changes: 2 additions & 3 deletions M3u8Downloader_H.Core/M3u8UriManagers/PluginM3u8UriManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ public PluginM3u8UriManager(IM3u8UriProvider m3U8UriProvider,HttpClient httpClie
this.headers = headers;
}

public Task<Uri> GetM3u8UriAsync(Uri uri, int timeout, CancellationToken cancellationToken)
public Task<Uri> GetM3u8UriAsync(Uri uri, int reserve0, CancellationToken cancellationToken)
{
try
{
using CancellationTokenSource cancellationTokenSource = cancellationToken.CancelTimeOut(timeout);
return m3U8UriProvider.GetM3u8UriAsync(httpClient, uri, headers, cancellationTokenSource.Token);
return m3U8UriProvider.GetM3u8UriAsync(httpClient, uri, headers, cancellationToken);
}catch(OperationCanceledException) when(!cancellationToken.IsCancellationRequested)
{
throw new TimeoutException($"访问 {uri.OriginalString} 超时");
Expand Down
12 changes: 5 additions & 7 deletions M3u8Downloader_H.Core/M3uDownloaders/CryptM3uDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ public override async ValueTask Initialization(CancellationToken cancellationTok
if(m3UFileInfo.Key.Uri != null && m3UFileInfo.Key.BKey == null)
{
try
{
using var tokenSource = cancellationToken.CancelTimeOut(TimeOut);
{
byte[] data = m3UFileInfo.Key.Uri.IsFile
? await File.ReadAllBytesAsync(m3UFileInfo.Key.Uri.OriginalString, tokenSource.Token)
: await HttpClient.GetByteArrayAsync(m3UFileInfo.Key.Uri, Headers, tokenSource.Token);
? await File.ReadAllBytesAsync(m3UFileInfo.Key.Uri.OriginalString, cancellationToken)
: await HttpClient.GetByteArrayAsync(m3UFileInfo.Key.Uri, Headers, cancellationToken);

m3UFileInfo.Key.BKey = data.TryParseKey(m3UFileInfo.Key.Method);
}
Expand All @@ -50,9 +49,8 @@ public override async ValueTask Initialization(CancellationToken cancellationTok
}

protected override Stream DownloadAfter(Stream stream, string contentType, CancellationToken cancellationToken)
{
Stream Decryptstream = stream.AesDecrypt(m3UFileInfo.Key.BKey, m3UFileInfo.Key.IV);
return base.DownloadAfter(Decryptstream, contentType, cancellationToken);
{
return stream.AesDecrypt(m3UFileInfo.Key.BKey, m3UFileInfo.Key.IV);
}
}
}
29 changes: 13 additions & 16 deletions M3u8Downloader_H.Core/M3uDownloaders/M3u8Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,22 @@ private async Task DownloadCallBack(M3UFileInfo m3UFileInfo, string savePath, IE
protected async Task<bool> DownloadAsynInternal(Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, RangeHeaderValue? rangeHeaderValue, string mediaPath, bool skipRequestError, CancellationToken token)
{
bool IsSuccessful = false;
var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
cancellationTokenSource.CancelAfter(TimeOut);
for (int i = 0; i < RetryCount; i++)
{
try
{
using CancellationTokenSource cancellationToken = token.CancelTimeOut(TimeOut);
(Stream tmpstream, string contentType) = await HttpClient.GetResponseContentAsync(uri, headers, rangeHeaderValue, cancellationToken.Token);
using Stream stream = DownloadAfter(new HandleImageStream(tmpstream, DownloadRate), contentType, cancellationToken.Token);
(Stream tmpstream, string contentType) = await HttpClient.GetResponseContentAsync(uri, headers, rangeHeaderValue, cancellationTokenSource.Token);
using Stream stream = DownloadAfter(new HandleImageStream(tmpstream, DownloadRate), contentType, cancellationTokenSource.Token);

await WriteToFileAsync(mediaPath, stream, cancellationToken.Token);
await WriteToFileAsync(mediaPath, stream, cancellationTokenSource.Token);
IsSuccessful = true;
break;
}
catch (OperationCanceledException) when (!token.IsCancellationRequested)
{
await Task.Delay(2000, token);
await Task.Delay(2000, cancellationTokenSource.Token);
continue;
}
catch (AggregateException ex) when(ex.InnerException is not InvalidDataException)
Expand All @@ -150,17 +151,17 @@ protected async Task<bool> DownloadAsynInternal(Uri uri, IEnumerable<KeyValuePai
{
throw new CryptographicException("解密失败,请确认key,iv是否正确");
}
await Task.Delay(2000, token);
await Task.Delay(2000, cancellationTokenSource.Token);
continue;
}
catch (IOException)
{
await Task.Delay(2000, token);
await Task.Delay(2000, cancellationTokenSource.Token);
continue;
}
catch (HttpRequestException) when (skipRequestError)
{
await Task.Delay(2000, token);
await Task.Delay(2000, cancellationTokenSource.Token);
continue;
}
}
Expand All @@ -172,14 +173,10 @@ protected async Task<bool> DownloadAsynInternal(Uri uri, IEnumerable<KeyValuePai
//参数传入带类型的值 在其他操作上来判断是否要调用这个基类
protected virtual Stream DownloadAfter(Stream stream, string contentType, CancellationToken cancellationToken)
{
if (contentType.StartsWith("image", StringComparison.CurrentCultureIgnoreCase) || contentType.StartsWith("text", StringComparison.CurrentCultureIgnoreCase))
{
HandleImageStream handleImageStream =(HandleImageStream)stream;
Task t = handleImageStream.InitializePositionAsync(2000, cancellationToken);
t.Wait(cancellationToken);
return handleImageStream;
}
return stream;
HandleImageStream handleImageStream =(HandleImageStream)stream;
Task t = handleImageStream.InitializePositionAsync(2000, cancellationToken);
t.Wait(cancellationToken);
return handleImageStream;
}

protected static async Task WriteToFileAsync(string file, Stream stream, CancellationToken token)
Expand Down
3 changes: 1 addition & 2 deletions M3u8Downloader_H.Core/M3uDownloaders/PluginM3u8Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public PluginM3u8Downloader(IDownloadService downloadService, M3UFileInfo m3UFi

public override async ValueTask Initialization(CancellationToken cancellationToken)
{
using var tokenSource = cancellationToken.CancelTimeOut(TimeOut);
await _pluginDownload.Initialize(HttpClient,Headers, m3UFileInfo, tokenSource.Token);
await _pluginDownload.Initialize(HttpClient,Headers, m3UFileInfo, cancellationToken);
}

protected override Stream DownloadAfter(Stream stream, string contentType, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public M3UFileReaderManager(IM3uFileReader? M3UFileReader,HttpClient httpClient,
public async Task<M3UFileInfo> GetM3u8FileInfo(Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, bool isRetry, CancellationToken cancellationToken = default)
{
_ = isRetry;
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationTokenSource.CancelAfter(TimeOuts);
for (int i = 0; i < 5; i++)
{
try
{
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationTokenSource.CancelAfter(TimeOuts);
var m3u8FileInfo = await GetM3u8FileInfo(uri, headers, cancellationTokenSource.Token);
return m3u8FileInfo.MediaFiles != null && m3u8FileInfo.MediaFiles.Any()
? m3u8FileInfo
: throw new InvalidDataException($"'{uri.OriginalString}' 没有包含任何数据");
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(2000, cancellationToken);
await Task.Delay(2000, cancellationTokenSource.Token);
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions M3u8Downloader_H.M3U8/M3u8Downloader_H.M3U8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<AssemblyVersion>3.12.10.0</AssemblyVersion>
<Version>3.12.10.0</Version>
<AssemblyVersion>3.13.1.0</AssemblyVersion>
<Version>3.13.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Version>3.12.10.0</Version>
<Version>3.13.1.0</Version>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions M3u8Downloader_H/M3u8Downloader_H.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>M3u8Downloader_H</RootNamespace>
<Platforms>AnyCPU;x64</Platforms>
<AssemblyVersion>3.12.10.0</AssemblyVersion>
<Version>3.12.10.0</Version>
<AssemblyVersion>3.13.1.0</AssemblyVersion>
<Version>3.13.1.0</Version>
<PackageId>m3u8视频下载器</PackageId>
<Product>m3u8视频下载器</Product>
<ApplicationIcon>icon.ico</ApplicationIcon>
Expand Down
2 changes: 1 addition & 1 deletion M3u8Downloader_H/ViewModels/DownloadViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void OnStart()
soundService.PlaySuccess();
Status = DownloadStatus.Completed;
}
catch (OperationCanceledException)
catch (OperationCanceledException) when (cancellationTokenSource!.IsCancellationRequested)
{
Status = DownloadStatus.Canceled;
}
Expand Down
25 changes: 8 additions & 17 deletions M3u8Downloader_H/Views/AboutView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,18 @@
xmlns:cal="http://www.caliburnproject.org"
d:DataContext="{d:DesignInstance Type=local:AboutViewModel}"
mc:Ignorable="d"
Height="320"

Width="500"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto"/>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="本软件完全免费,且开源" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center" />
<StackPanel Grid.Row="1">
<TextBlock Text="本软件首发地址:" FontSize="18" HorizontalAlignment="Center" />
<Button Style="{StaticResource MaterialDesignFlatLightButton}"
cal:Message.Attach="OnExplore('https://www.52pojie.cn/thread-1622606-1-1.html')">
<Hyperlink >https://www.52pojie.cn/thread-1622606-1-1.html</Hyperlink>
</Button>
<TextBlock Text="帮助文档:" FontSize="16" HorizontalAlignment="Center" />
<Button Style="{StaticResource MaterialDesignFlatLightButton}"
cal:Message.Attach="OnExplore('http://note.youdao.com/noteshare?id=c6ba2fb478ad300b7095c7c951556fc6')">
<Hyperlink >http://note.youdao.com/noteshare?id=c6ba2fb478ad300b7095c7c951556fc6</Hyperlink>
</Button>
</StackPanel>

<StackPanel Grid.Row="2" >
<StackPanel Grid.Row="1" >
<TextBlock Text="本软件开源地址:" FontSize="18" HorizontalAlignment="Center"/>
<Button Style="{StaticResource MaterialDesignFlatLightButton}"
cal:Message.Attach="OnExplore('https://github.com/Harlan-H/M3u8Downloader_H')" >
Expand All @@ -42,9 +29,13 @@
cal:Message.Attach="OnExplore('https://github.com/Harlan-H/M3u8Downloader_H/wiki/')">
<Hyperlink >https://github.com/Harlan-H/M3u8Downloader_H/wiki/</Hyperlink>
</Button>
<Button Style="{StaticResource MaterialDesignFlatLightButton}"
cal:Message.Attach="OnExplore('http://note.youdao.com/noteshare?id=c6ba2fb478ad300b7095c7c951556fc6')">
<Hyperlink >http://note.youdao.com/noteshare?id=c6ba2fb478ad300b7095c7c951556fc6</Hyperlink>
</Button>
</StackPanel>
<Grid Grid.Row="3">
<TextBlock FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Copyright (c) 2022 Harlan" Margin="32 0 0 0" />
<Grid Grid.Row="2">
<TextBlock FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Copyright (c) 2023 Harlan" Margin="32 0 0 0" />

<Button x:Name="Close"
HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="关闭"
Expand Down
3 changes: 2 additions & 1 deletion M3u8Downloader_H/Views/MainWindowView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
Margin="5" Width="150" >
<materialDesign:PackIcon Kind="Download" Width="24" Height="24" />
</Button>
<ToggleButton Foreground="Blue" Margin="5" Cursor="Hand" HorizontalAlignment="Center" FontSize="13">
<ToggleButton Margin="5" Cursor="Hand" HorizontalAlignment="Center" FontSize="13">
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<TextBlock >
Expand All @@ -147,6 +147,7 @@
</ToggleButton.Template>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Foreground" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#af0000ff" />
Expand Down

0 comments on commit 3b02b14

Please sign in to comment.