Skip to content

Commit

Permalink
9.4.6 支持b站搜索,美化图片显示,支持gelbooru 的artist,copyright等字段
Browse files Browse the repository at this point in the history
  • Loading branch information
xplusky committed Apr 24, 2020
1 parent 0821e49 commit 2e68ea8
Show file tree
Hide file tree
Showing 29 changed files with 571 additions and 509 deletions.
77 changes: 30 additions & 47 deletions MoeLoaderP.Core/DownloadItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using ImageMagick;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
Expand All @@ -7,8 +9,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ImageMagick;
using Newtonsoft.Json;

namespace MoeLoaderP.Core
{
Expand Down Expand Up @@ -52,26 +52,8 @@ public string DownloadStatusIconText
{
get
{
switch (Status)
{
case DownloadStatusEnum.WaitForDownload:
return "";
case DownloadStatusEnum.Success:
return "";
case DownloadStatusEnum.Cancel:
return "";
case DownloadStatusEnum.IsExist:
return "";
case DownloadStatusEnum.Failed:
return "";
case DownloadStatusEnum.Downloading:
return "";
case DownloadStatusEnum.Skip:
return "";
case DownloadStatusEnum.Stop:
return WebUtility.HtmlDecode("");
}
return null;
var strings = new[] {"", "", "", WebUtility.HtmlDecode(""), "", "", ""};
return strings[(int) Status];
}
}

Expand Down Expand Up @@ -105,7 +87,7 @@ public DownloadItem(Settings set, dynamic bitimg, MoeItem item, int subindex = 0
CurrentMoeItem = item;
SubIndex = subindex;
OriginFileName = Path.GetFileName(item.DownloadUrlInfo.Url);
OriginFileName = Path.GetFileNameWithoutExtension(item.DownloadUrlInfo.Url);
OriginFileNameWithoutExt = Path.GetFileNameWithoutExtension(item.DownloadUrlInfo.Url).ToDecodedUrl();
var father = subindex == 0 ? null : fatheritem;
GenFileNameWithoutExt(father);
GenLocalFileFullPath(father);
Expand All @@ -120,19 +102,22 @@ public async Task DownloadFileAsync()
if (SubItems.Count > 0)
{
Status = DownloadStatusEnum.Downloading;
var b = Set.DownloadFirstSeveralCount < SubItems.Count && Set.IsDownloadFirstSeveral;
var count = b ? Set.DownloadFirstSeveralCount : SubItems.Count;
for (var i = 0; i < SubItems.Count; i++)
{

StatusText = $"正在下载 {i+1} / {count} 张";
if (i < Set.DownloadFirstSeveralCount || Set.IsDownloadFirstSeveral == false)
{
var item = SubItems[i];
await item.DownloadFileAsync();
}

var count = Set.DownloadFirstSeveralCount < SubItems.Count ? Set.DownloadFirstSeveralCount : SubItems.Count;

Progress = (i + 1d) / count * 100d;
}
Status = DownloadStatusEnum.Success;
StatusText = "下载完成";
StatusText = $"{count} 张下载完成";
}
else
{
Expand All @@ -158,7 +143,7 @@ public async Task DownloadFileAsync()
else
{
Status = DownloadStatusEnum.Skip;
StatusText = "跳过";
StatusText = "已存在,跳过";
return;
}

Expand All @@ -176,19 +161,18 @@ public async Task DownloadFileAsync()
if (File.Exists(LocalFileFullPath))
{
Status = DownloadStatusEnum.Skip;
StatusText = "跳过";
StatusText = "已存在,跳过";
return;
}
Status = DownloadStatusEnum.Downloading;
var data = await net.Client.GetAsync(url.Url, token);
var bytes = await data.Content.ReadAsByteArrayAsync();

var dir = Path.GetDirectoryName(LocalFileFullPath);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
using (var fs = new FileStream(LocalFileFullPath, FileMode.Create))
{
await fs.WriteAsync(bytes, 0, bytes.Length, token);

}

if (CurrentMoeItem.ExtraFile != null)
Expand All @@ -197,7 +181,7 @@ public async Task DownloadFileAsync()
File.WriteAllText(path, CurrentMoeItem.ExtraFile.Content);
}

if (url.IsPixivGifZip && CurrentMoeItem.ExtraFile!=null)
if (url.IsPixivGifZip && CurrentMoeItem.ExtraFile != null)
{
dynamic json = JsonConvert.DeserializeObject(CurrentMoeItem.ExtraFile.Content);
var list = json.body.frames;
Expand Down Expand Up @@ -237,9 +221,9 @@ await Task.Run(() =>
}
}

public void ConvertPixivZipToGif(Stream stream,dynamic frames,FileInfo fi)
public void ConvertPixivZipToGif(Stream stream, dynamic frames, FileInfo fi)
{
var delayList= new List<int>();
var delayList = new List<int>();
using (var images = new MagickImageCollection())
{
foreach (var frame in frames)
Expand Down Expand Up @@ -289,7 +273,7 @@ public void GenLocalFileFullPath(MoeItem father = null)
public void GenFileNameWithoutExt(MoeItem father = null)
{
var img = father ?? CurrentMoeItem;

var format = Set.SaveFileNameFormat;
if (format.IsNaN())
{
Expand All @@ -302,7 +286,7 @@ public void GenFileNameWithoutExt(MoeItem father = null)
LocalFileShortNameWithoutExt = SubIndex > 0 ? $"{sb} p{SubIndex}" : $"{sb}";
}

public string FormatText(string format,MoeItem img,bool isFolder=false)
public string FormatText(string format, MoeItem img, bool isFolder = false)
{
var sb = new StringBuilder(format);
sb.Replace("%site", img.Site.ShortName);
Expand All @@ -326,29 +310,28 @@ public string FormatText(string format,MoeItem img,bool isFolder=false)
sb.Replace("%copyright", img.Copyright ?? "no-copyright");
foreach (var c in Path.GetInvalidFileNameChars())
{
if(c == '\\' && isFolder) continue;
if (c == '\\' && isFolder) continue;
sb.Replace($"{c}", "");
}

return sb.ToString();
}


public string AutoRenameFullPath()
{
var oldf = LocalFileFullPath;
var ext = Path.GetExtension(oldf);
var dir = Path.GetDirectoryName(oldf);
var file = Path.GetFileNameWithoutExtension(oldf);
var oldF = LocalFileFullPath;
var ext = Path.GetExtension(oldF);
var dir = Path.GetDirectoryName(oldF);
var file = Path.GetFileNameWithoutExtension(oldF);
var i = 2;
var newf = $"{dir}\\{file}{-i}{ext}";
while (File.Exists(newf))
var newF = $"{dir}\\{file}{-i}{ext}";
while (File.Exists(newF))
{
i++;
newf = $"{dir}{-i}{ext}";
newF = $"{dir}{-i}{ext}";
}

return newf;
return newF;
}

}
Expand All @@ -357,6 +340,6 @@ public class DownloadItems : ObservableCollection<DownloadItem> { }

public enum DownloadStatusEnum
{
Success, Failed, Cancel, IsExist, Stop, Downloading, WaitForDownload, Skip
Success, Failed, Cancel, Stop, Downloading, WaitForDownload, Skip
}
}
24 changes: 8 additions & 16 deletions MoeLoaderP.Core/Extend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ public static string ToPairsString(this Pairs pairs)
var query = string.Empty;
var i = 0;
if (pairs == null) return query;
foreach (var para in pairs.Where(para => !string.IsNullOrEmpty(para.Value)))
foreach (var para in pairs.Where(para => !para.Value.IsNaN()))
{
query += string.Format("{2}{0}={1}", para.Key, para.Value, i > 0 ? "&" : "?");
query += $"{(i > 0 ? "&" : "?")}{para.Key}={para.Value}";
i++;
}

return query;
}
public static dynamic CheckListNull(dynamic dyObj) => dyObj ?? new List<dynamic>();

public static bool IsNaN(this string text)
{
return string.IsNullOrWhiteSpace(text);
}
public static bool IsNaN(this string text) => string.IsNullOrWhiteSpace(text);

public static DateTime? ToDateTime(this string dateTime)
{
if (string.IsNullOrWhiteSpace(dateTime)) return null;
if (dateTime.IsNaN()) return null;
var timeInt = dateTime.ToLong();
if (timeInt != 0)
{
Expand All @@ -70,19 +68,13 @@ public static long ToLong(this string idStr)
return id;
}

public static string ToEncodedUrl(this string orgstr)
{
return HttpUtility.UrlEncode(orgstr, Encoding.UTF8);
}
public static string ToEncodedUrl(this string orgStr) => HttpUtility.UrlEncode(orgStr, Encoding.UTF8);

public static string ToDecodedUrl(this string orgstr)
{
return HttpUtility.UrlDecode(orgstr, Encoding.UTF8);
}
public static string ToDecodedUrl(this string orgStr) => HttpUtility.UrlDecode(orgStr, Encoding.UTF8);

public static void GoUrl(this string url)
{
if (string.IsNullOrWhiteSpace(url)) return;
if (url.IsNaN()) return;
try
{
Process.Start(url);
Expand Down
19 changes: 15 additions & 4 deletions MoeLoaderP.Core/MoeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MoeItem : BindingObject

public string DateString
{
get => string.IsNullOrWhiteSpace(_dateString) ? Date?.ToString("G", new CultureInfo("zh-CN")) : _dateString;
get => _dateString.IsNaN() ? Date?.ToString("G", new CultureInfo("zh-CN")) : _dateString;
set => _dateString = value;
}

Expand All @@ -35,7 +35,16 @@ public string DateString
/// 上传用户ID
/// </summary>
public string UploaderId { get; set; }
public double Score { get; set; }

public double Score
{
get => _score;
set
{
_score = value; OnPropertyChanged(nameof(Score));
}
}

public int Rank { get; set; }
public bool TipHighLight { get; set; }

Expand Down Expand Up @@ -112,6 +121,7 @@ public string ResolutionText
private int _imageCount;
private string _dateString;
private string _tip;
private double _score;

public int ImagesCount
{
Expand Down Expand Up @@ -198,8 +208,9 @@ public UrlInfo(int priority, string url, string referer = null)

public string GetFileExtFromUrl()
{
if (string.IsNullOrEmpty(Url)) return null;
var type = Path.GetExtension(Url).Replace(".", "").ToUpper();
if (Url.IsNaN()) return null;
var type = Path.GetExtension(Url)?.Replace(".", "").ToUpper();
if (type == null) return null;
if (type.Contains("?"))
{
type = type.Split('?')[0];
Expand Down
10 changes: 5 additions & 5 deletions MoeLoaderP.Core/NetDocker.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using HtmlAgilityPack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using HtmlAgilityPack;
using Newtonsoft.Json;

namespace MoeLoaderP.Core
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public NetDocker(Settings settings, string cookieurl = null)

public void SetReferer(string rfurl)
{
if (string.IsNullOrWhiteSpace(rfurl)) return;
if (rfurl.IsNaN()) return;
Client.DefaultRequestHeaders.Referrer = new Uri(rfurl);
}

Expand Down Expand Up @@ -153,7 +153,7 @@ public IWebProxy Proxy
}
return xml;
}

}


Expand Down
Loading

0 comments on commit 2e68ea8

Please sign in to comment.