From 191329fdf00100b24f0ed5d6b6ca059b8549dbcd Mon Sep 17 00:00:00 2001 From: Nlick47 <2247717951@qq.com> Date: Sun, 24 Nov 2024 03:54:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=94=B9=E8=BF=9BGetVideoSections?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BiliApi/Video/Models/UgcEpisode.cs | 2 + DownKyi/Services/VideoInfoService.cs | 197 ++++++++---------- 2 files changed, 88 insertions(+), 111 deletions(-) diff --git a/DownKyi.Core/BiliApi/Video/Models/UgcEpisode.cs b/DownKyi.Core/BiliApi/Video/Models/UgcEpisode.cs index 9015de2..56e68a5 100644 --- a/DownKyi.Core/BiliApi/Video/Models/UgcEpisode.cs +++ b/DownKyi.Core/BiliApi/Video/Models/UgcEpisode.cs @@ -14,5 +14,7 @@ public class UgcEpisode : BaseModel [JsonProperty("attribute")] public int Attribute { get; set; } [JsonProperty("arc")] public UgcArc Arc { get; set; } [JsonProperty("page")] public VideoPage Page { get; set; } + + [JsonProperty("pages")] public List Pages { get; set; } [JsonProperty("bvid")] public string Bvid { get; set; } } \ No newline at end of file diff --git a/DownKyi/Services/VideoInfoService.cs b/DownKyi/Services/VideoInfoService.cs index 0c27bb7..10d17da 100644 --- a/DownKyi/Services/VideoInfoService.cs +++ b/DownKyi/Services/VideoInfoService.cs @@ -137,7 +137,7 @@ public class VideoInfoService : IInfoService /// public List? GetVideoSections(bool noUgc = false) { - if (_videoView == null) + if (_videoView == null || _videoView.UgcSeason?.Sections == null || _videoView.UgcSeason.Sections.Count == 0) { return null; } @@ -147,75 +147,12 @@ public class VideoInfoService : IInfoService // 不需要ugc内容 if (noUgc) { - videoSections.Add(new VideoSection - { - Id = 0, - Title = "default", - IsSelected = true, - VideoPages = GetVideoPages() - }); - + videoSections.Add(CreateDefaultVideoSection()); return videoSections; } - if (_videoView.UgcSeason == null) - { - return null; - } - - if (_videoView.UgcSeason.Sections == null) - { - return null; - } - - if (_videoView.UgcSeason.Sections.Count == 0) - { - return null; - } - - if (_videoView.UgcSeason.Sections.Any(x => x.Type == 1)) - { - if (_videoView.Pages.Count != 1) - { - var eps = _videoView.UgcSeason.Sections.First().Episodes; - var order = 0; - - foreach (var ep in eps) - { - int cp = 1; - var result = VideoInfo.VideoPagelist(aid: ep.Aid); - var pages = new List(); - foreach (var p in result) - { - order++; - pages.Add(new() - { - Cid = p.Cid, - EpisodeId = -1, - Order = order, - Name = p.Part, - Duration = "N/A", - Owner = _videoView.Owner, - FirstFrame = p.FirstFrame, - Avid = ep.Aid, - Bvid = ep.Bvid, - Page = cp++, - }); - } - - videoSections.Add(new() - { - Id = ep.Id, - Title = ep.Title, - VideoPages = pages, - }); - } - videoSections[0].IsSelected = true; - return videoSections; - } - - } - + var timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); + var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); foreach (var section in _videoView.UgcSeason.Sections) { @@ -223,60 +160,98 @@ public class VideoInfoService : IInfoService var order = 0; foreach (var episode in section.Episodes) { - order++; - var page = new VideoPage + if (episode.Pages?.Count > 1) { - Avid = episode.Aid, - Bvid = episode.Bvid, - Cid = episode.Cid, - EpisodeId = -1, - FirstFrame = episode.Arc.Pic, - Order = order, - Name = episode.Title, - Duration = "N/A", - // UP主信息 - Owner = _videoView.Owner, - Page = episode.Page.Page - }; - - if (page.Owner == null) - { - page.Owner = new VideoOwner - { - Name = "", - Face = "", - Mid = -1, - }; + var videoSection = CreateVideoSectionFromEpisode(section, episode, startTime, timeFormat); + videoSections.Add(videoSection); + } + else + { + pages.Add(GenerateVideoPage(episode, ++order, startTime, timeFormat)); } - - // 文件命名中的时间格式 - var timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); - // 视频发布时间 - var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 - var dateTime = startTime.AddSeconds(episode.Arc.Ctime); - page.PublishTime = dateTime.ToString(timeFormat); - // 这里的发布时间有问题, - // 如果是合集,也会执行这里, - // 但是发布时间是入口视频的,不是所有视频的 - // TODO 修复 - - pages.Add(page); } - - var videoSection = new VideoSection + if (pages.Count > 0) { - Id = section.Id, - Title = section.Title, - VideoPages = pages - }; - videoSections.Add(videoSection); + var videoSection = new VideoSection + { + Id = section.Id, + Title = section.Title, + VideoPages = pages + }; + videoSections.Add(videoSection); + } } - videoSections[0].IsSelected = true; + if (videoSections.Count > 0) + { + videoSections[0].IsSelected = true; + } return videoSections; } + private VideoSection CreateDefaultVideoSection() + { + return new VideoSection + { + Id = 0, + Title = "default", + IsSelected = true, + VideoPages = GetVideoPages() + }; + } + + private VideoSection CreateVideoSectionFromEpisode(UgcSection section, UgcEpisode episode, DateTime startTime, string timeFormat) + { + var videoSection = new VideoSection + { + Id = section.Id, + Title = episode.Title, + VideoPages = new List() + }; + var order = 1; + foreach (var p in episode.Pages) + { + var dateTime = startTime.AddSeconds(episode.Arc.Ctime); + videoSection.VideoPages.Add(new VideoPage + { + Avid = episode.Aid, + Bvid = episode.Bvid, + Cid = p.Cid, + EpisodeId = -1, + FirstFrame = episode.Arc.Pic, + Order = order ++, + Name = p.Part, + Duration = "N/A", + Owner = _videoView.Owner, + Page = p.Page, + PublishTime = dateTime.ToString(timeFormat) + }); + } + + return videoSection; + } + + private VideoPage GenerateVideoPage(UgcEpisode episode, int order, DateTime startTime, string timeFormat) + { + var page = new VideoPage + { + Avid = episode.Aid, + Bvid = episode.Bvid, + Cid = episode.Cid, + EpisodeId = -1, + FirstFrame = episode.Arc.Pic, + Order = order, + Name = episode.Title, + Duration = "N/A", + Owner = _videoView.Owner ?? new VideoOwner { Name = "", Face = "", Mid = -1 }, + Page = episode.Page.Page + }; + var dateTime = startTime.AddSeconds(episode.Arc.Ctime); + page.PublishTime = dateTime.ToString(timeFormat); + return page; + } + /// /// 获取视频流的信息,从VideoPage返回 ///