From c1a04734a35c49be5327dfb097674522e06ceae7 Mon Sep 17 00:00:00 2001 From: Vincent Wang <376787823@qq.com> Date: Wed, 23 Oct 2024 20:53:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BF=A1=E6=81=AF=E6=8D=95=E6=8D=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeiboAlbumDownloader/App.xaml.cs | 16 +++++++++++ WeiboAlbumDownloader/GlobalVar.cs | 12 +++++++++ WeiboAlbumDownloader/MainWindow.xaml | 1 + WeiboAlbumDownloader/MainWindow.xaml.cs | 36 ++++++++++++++++++++----- 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 WeiboAlbumDownloader/GlobalVar.cs diff --git a/WeiboAlbumDownloader/App.xaml.cs b/WeiboAlbumDownloader/App.xaml.cs index dac92cf..fd2c3fc 100644 --- a/WeiboAlbumDownloader/App.xaml.cs +++ b/WeiboAlbumDownloader/App.xaml.cs @@ -4,6 +4,7 @@ using System.Configuration; using System.Data; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; @@ -32,10 +33,25 @@ public App() void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { + SentrySdk.AddBreadcrumb( + message: $"weibo.com/u/{GlobalVar.gId},uid: {GlobalVar.gId},DataSource: {GlobalVar.gDataSource},Page:{GlobalVar.gPage},SinceId: {GlobalVar.gSinceId}", + category: "error", + level: BreadcrumbLevel.Error + ); SentrySdk.CaptureException(e.Exception); // If you want to avoid the application from crashing: e.Handled = true; } + + private void OnStartup(object sender, StartupEventArgs e) + { + SentrySdk.ConfigureScope(scope => + { + scope.SetTag("AppName", Assembly.GetExecutingAssembly().GetName().Name!); + scope.SetTag("DeviceName", Environment.MachineName); + }); + } + } } diff --git a/WeiboAlbumDownloader/GlobalVar.cs b/WeiboAlbumDownloader/GlobalVar.cs new file mode 100644 index 0000000..f2612ef --- /dev/null +++ b/WeiboAlbumDownloader/GlobalVar.cs @@ -0,0 +1,12 @@ +using WeiboAlbumDownloader.Enums; + +namespace WeiboAlbumDownloader +{ + public class GlobalVar + { + public static string gId = ""; + public static long gSinceId; + public static int gPage; + public static WeiboDataSource gDataSource; + } +} diff --git a/WeiboAlbumDownloader/MainWindow.xaml b/WeiboAlbumDownloader/MainWindow.xaml index bed20f9..8c4112d 100644 --- a/WeiboAlbumDownloader/MainWindow.xaml +++ b/WeiboAlbumDownloader/MainWindow.xaml @@ -55,6 +55,7 @@ Width="140" Height="32" Margin="8,0" + KeyUp="TextBox_WeiboId_KeyUp" ToolTip="填写uid单用户下载或者uidList.txt进行批量下载" Watermark="uid" /> /// com1是根据uid获取相册id,https://photo.weibo.com/albums/get_all?uid=10000000000&page=1;根据uid和相册id以及相册type获取图片列表,https://photo.weibo.com/photos/get_all?uid=10000000000&album_id=3959362334782071&page=1&type=3 @@ -116,6 +116,16 @@ private async Task Start(string userId) cancellationTokenSource?.Cancel(); cancellationTokenSource = new CancellationTokenSource(); + if (string.IsNullOrEmpty(userId)) + { + AppendLog("uid呢?u i d呢?请填写uid再下载。参考上面说明获取uid", MessageEnum.Error); + return; + } + + //用于Sentry崩溃收集 + GlobalVar.gId = userId; + GlobalVar.gDataSource = dataSource; + //读取用户列表和设置 InitData(); @@ -162,6 +172,8 @@ await Task.Run(async () => if (isSkip) break; + GlobalVar.gPage = page; + string albumUrl = $"https://photo.weibo.com/photos/get_all?uid={userId}&album_id={item.album_id}&count={countPerPage}&page={page}&type={item.type}"; var photos = await HttpHelper.GetAsync(albumUrl, dataSource, cookie!); if (photos != null && photos.data?.photo_list != null && photos.data?.photo_list.Count > 0) @@ -193,7 +205,7 @@ await Task.Run(async () => var invalidChar = System.IO.Path.GetInvalidFileNameChars(); var newCaption = invalidChar.Aggregate(photo.caption_render, (o, r) => (o.Replace(r.ToString(), string.Empty))); var fileName = downloadFolder + userId + "//" + item.caption + "//" - + timestamp.ToString("yyyy-MM-dd HH_mm_ss") + "-" + photoCount + newCaption + ".jpg"; + + timestamp.ToString("yyyy-MM-dd HH_mm_ss") + newCaption + "_" + photoCount + ".jpg"; Debug.WriteLine(fileName); if (File.Exists(fileName)) { @@ -278,7 +290,7 @@ await Task.Run(async () => var photos = await HttpHelper.GetAsync(albumUrl, dataSource, cookie!); if (photos != null && photos.PhotoListData2?.PhotoListItem2 != null && photos.PhotoListData2?.PhotoListItem2.Count > 0) { - sinceId = photos.PhotoListData2.SinceId; + GlobalVar.gSinceId = sinceId = photos.PhotoListData2.SinceId; foreach (var photo in photos.PhotoListData2?.PhotoListItem2!) { if (cancellationTokenSource.IsCancellationRequested) @@ -347,6 +359,8 @@ await Task.Run(async () => if (isSkip) break; + GlobalVar.gPage = page; + //filter,0-全部;1-原创;2-图片 //https://weibo.cn/xxxxxxxxxxxxx?page=2 //https://weibo.cn/xxxxxxxxxxxxx/profile?page=2 @@ -543,7 +557,7 @@ await Task.Run(async () => var invalidChar = Path.GetInvalidFileNameChars(); var newCaption = invalidChar.Aggregate(weiboContent, (o, r) => (o.Replace(r.ToString(), string.Empty))); var fileName = downloadFolder + userId + "//" + "微博配图" + "//" - + timestamp.ToString("yyyy-MM-dd HH_mm_ss") + "-" + photoCount + newCaption; + + timestamp.ToString("yyyy-MM-dd HH_mm_ss") + newCaption + "_" + photoCount; //后缀名区分图片/视频 if (picType == PicEnum.Video) fileName += ".mp4"; @@ -618,7 +632,7 @@ await Task.Run(async () => var res = await HttpHelper.GetAsync(url, dataSource, cookie!); if (res != null && res?.Ok == 1 && res?.Data != null && res?.Data?.Cards != null && res?.Data?.Cards?.Count > 0) { - sinceId = (long)res?.Data?.CardlistInfo?.SinceId!; + GlobalVar.gSinceId = sinceId = (long)res?.Data?.CardlistInfo?.SinceId!; AppendLog($"获取到{res?.Data?.CardlistInfo?.Total}条数据,正在下载第{page}页", MessageEnum.Info); //获取用户资料 @@ -890,7 +904,7 @@ await Task.Run(async () => //单个用户结束下载 - string info = $"{nickName} {userId}{nickName}于{DateTime.Now.ToString("HH:mm:ss")}结束下载,程序版本V{currentVersion}"; + string info = $"{nickName} {userId}{nickName}于{DateTime.Now.ToString("HH:mm:ss")}结束下载,程序版本V{currentVersion}"; await PushPlusHelper.SendMessage(settings?.PushPlusToken!, "微博相册下载", info); SentrySdk.CaptureMessage(info); @@ -905,7 +919,9 @@ await Task.Run(async () => } catch (Exception ex) { - AppendLog("遇到错误" + ex.ToString() + ",请稍后重试。", MessageEnum.Error); + string msg = $"遇到错误,uid: {GlobalVar.gId},DataSource: {dataSource},Page:{GlobalVar.gPage},SinceId: {GlobalVar.gSinceId}," + ex.ToString() + ",请稍后重试。"; + AppendLog(msg, MessageEnum.Error); + SentrySdk.CaptureMessage(msg, SentryLevel.Error); } } @@ -1112,5 +1128,11 @@ private void GetCookie(object sender, RoutedEventArgs e) // 程序结束时,手动关闭浏览器 driver.Quit(); } + + private async void TextBox_WeiboId_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) + { + if (e.Key == System.Windows.Input.Key.Enter) + await Start(TextBox_WeiboId.Text.Trim()); + } } }