Skip to content

Commit

Permalink
增加联网获取最新版本提示
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzheng committed Apr 19, 2024
1 parent ce104a1 commit 5b2ab9e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 45 deletions.
38 changes: 38 additions & 0 deletions WeiboAlbumDownloader/Helpers/GithubHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WeiboAlbumDownloader.Helpers
{
public class GithubHelper
{
public static async Task<string?> GetLatestVersion()
{
try
{
HttpClient client = new HttpClient();
var resp = await client.GetAsync("https://github.com/hupo376787/WeiboAlbumDownloader/tags");
var body = await resp.Content.ReadAsStringAsync();
string pattern = "tags.*zip";
//匹配多个
//MatchCollection list = Regex.Matches(body, pattern);
//匹配第一个
Match match = Regex.Match(body, pattern);
if (match.Success)
{
var version = match.Value.Replace("tags/", "").Replace(".zip", "");
return version;
}

return null;

}
catch (Exception ex)
{
return ex.ToString();
}
}
}
}
44 changes: 0 additions & 44 deletions WeiboAlbumDownloader/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,50 +101,6 @@
ImageSource="/weibo.ico"
Stretch="Fill" />
</Border.Background>
<Border.Style>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Width" Value="60" />
<Setter Property="Height" Value="40" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect
x:Name="OSE"
BlurRadius="24"
Direction="0"
Opacity="1"
RenderingBias="Quality"
ShadowDepth="0"
Color="#FFBF00">
<Storyboard.TargetProperty>
BlurRadius
</Storyboard.TargetProperty>
</DropShadowEffect>
</Setter.Value>
</Setter>
</Style>
</Border.Style>
<Border.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
AutoReverse="True"
BeginTime="00:00:00"
RepeatBehavior="Forever"
Storyboard.TargetProperty="(FrameworkElement.Effect).(DropShadowEffect.BlurRadius)"
From="30"
To="100"
Duration="00:00:1.500" />
<ColorAnimationUsingKeyFrames
AutoReverse="True"
RepeatBehavior="Forever"
Storyboard.TargetProperty="(FrameworkElement.Effect).(DropShadowEffect.Color)">
<EasingColorKeyFrame KeyTime="0:0:1.500" Value="#FFBF00" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
<TextBlock
x:Name="TextBlock_UID"
Expand Down
31 changes: 30 additions & 1 deletion WeiboAlbumDownloader/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -42,6 +43,8 @@ public partial class MainWindow : MicaWindow
public MainWindow()
{
InitializeComponent();

GetVersion();
AppendLog("数据源选择weibo.com的时候,程序是从微博相册采集数据,包括微博配图和头像相册等,但是没有视频;数据源选择weibo.cn的时候,程序是从微博时间流中采集数据,包括微博配图以及发布的视频。", MessageEnum.Info);

InitData();
Expand Down Expand Up @@ -77,6 +80,32 @@ private void StopDownLoad(object sender, RoutedEventArgs e)
cancellationTokenSource.Cancel();
}

private async Task GetVersion()
{
double currentVersion = 1.4;
AppendLog($"当前程序版本V{currentVersion}");

var latestVersionString = await GithubHelper.GetLatestVersion();
if (string.IsNullOrEmpty(latestVersionString))
{
AppendLog("从https://github.com/hupo376787/WeiboAlbumDownloader/releases获取最新版失败,请检查网络或稍后再试", MessageEnum.Warning);
}
else
{
var res = double.TryParse(latestVersionString, out double latestVersion);
//获取成功
if (res)
{
if(latestVersion > currentVersion)
AppendLog($"Github最新版为V{latestVersion}。请从https://github.com/hupo376787/WeiboAlbumDownloader/releases获取最新版", MessageEnum.Success);
}
else
{
AppendLog("从https://github.com/hupo376787/WeiboAlbumDownloader/releases获取最新版失败,请检查网络或稍后再试", MessageEnum.Warning);
}
}
}

private async Task Start()
{
try
Expand Down Expand Up @@ -579,7 +608,7 @@ await Task.Run(async () =>
}
}

private void AppendLog(string text, MessageEnum messageEnum)
private void AppendLog(string text, MessageEnum messageEnum = MessageEnum.Info)
{
string msg = $"{DateTime.Now} {text}";
Debug.WriteLine(msg);
Expand Down

0 comments on commit 5b2ab9e

Please sign in to comment.