Skip to content

Commit

Permalink
fix: upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Oct 28, 2023
1 parent 45c650b commit 2a0dc0c
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 67 deletions.
6 changes: 3 additions & 3 deletions EhTagClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ static Client()

private const string LAST_UPDATE = "EhTagClient.LastUpdate";

private static readonly Regex reg = new Regex(@"<a href=""https://repo\.e-hentai\.org/tools\.php\?act=taggroup&amp;mastertag=(\d+)"">([^<]+)</a>", RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex _Reg = new Regex(@"<a href=""https://repo\.e-hentai\.org/tools/taggroup\?mastertag=(\d+)"">([^<]+)</a>", RegexOptions.Singleline | RegexOptions.Compiled);

private static readonly string DbUri = "https://repo.e-hentai.org/tools.php?act=taggroup&show={0}";
private static readonly string DbUri = "https://repo.e-hentai.org/tools/taggroup?show={0}";

public static DateTimeOffset LastUpdate
{
Expand Down Expand Up @@ -86,7 +86,7 @@ await Task.Run(async () =>

private static async Task updateDbAsync(TagDb db, string html, CancellationToken token)
{
var matches = reg.Matches(html);
var matches = _Reg.Matches(html);
var toAdd = new List<TagRecord>(matches.Count);
foreach (var item in matches.Cast<Match>())
{
Expand Down
3 changes: 0 additions & 3 deletions ExClient/Api/ImageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected override void CheckResponseOverride(ApiRequest request)
throw new ArgumentException("Key mismatch");
}

#pragma warning disable IDE0051 // 删除未使用的私有成员
#pragma warning disable IDE1006 // 命名样式
[JsonProperty] internal int p { get; set; }
[JsonProperty] internal string s { get; set; }
Expand All @@ -44,11 +43,9 @@ protected override void CheckResponseOverride(ApiRequest request)
[JsonProperty] internal string i3 { get; set; }
[JsonProperty] internal string i5 { get; set; }
[JsonProperty] internal string i6 { get; set; }
[JsonProperty] internal string i7 { get; set; }
[JsonProperty] internal int si { get; set; }
[JsonProperty] internal string x { get; set; }
[JsonProperty] internal string y { get; set; }
#pragma warning restore IDE0051 // 删除未使用的私有成员
#pragma warning restore IDE1006 // 命名样式
}
}
34 changes: 24 additions & 10 deletions ExClient/Galleries/Commenting/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,36 @@ private Comment(CommentCollection owner, int id, HtmlNode commentNode)

private static readonly HttpClient _TransClient = new HttpClient();

private static async Task<string> _TranslateAsync(string source, string targetLangCode, CancellationToken token)
{
if (Uri.TryCreate(source, UriKind.Absolute, out var urlContent) && (urlContent.Scheme == "http" || urlContent.Scheme == "https"))
{
// Do not translate url
return source;
}
var uri = $"https://clients5.google.com/translate_a/t?client=dict-chrome-ex"
+ $"&sl=auto&tl={Uri.EscapeDataString(targetLangCode)}&q={Uri.EscapeDataString(source)}";
var transTask = _TransClient.GetStringAsync(new(uri));
var transRetHtml = await transTask.AsTask(token);
var obj = JsonConvert.DeserializeObject<List<string>[]>(transRetHtml);
var first = obj[0];
first.RemoveAt(first.Count - 1);
return string.Concat(first);
}

public IAsyncOperation<HtmlNode> TranslateAsync(string targetLangCode)
{
return AsyncInfo.Run(async token =>
{
var node = HtmlNode.CreateNode(Content.OuterHtml);
foreach (var item in node.Descendants("#text"))
var textNodes = node.Descendants("#text").ToList();
var tasks = textNodes.Select(async node =>
{
var data = item.GetInnerText();
var uri = $"https://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8"
+ $"&sl=auto&tl={targetLangCode}&q={Uri.EscapeDataString(data)}";
var transRetHtml = await _TransClient.GetStringAsync(new Uri(uri));
var obj = JsonConvert.DeserializeObject<JArray>(transRetHtml);
var objarr = (JArray)obj[0];
var translated = string.Concat(objarr.Select(a => a[0].ToString()));
item.InnerHtml = HtmlEntity.Entitize(translated);
}
var data = node.GetInnerText();
var translated = await _TranslateAsync(data, targetLangCode, token);
node.InnerHtml = HtmlEntity.Entitize(translated);
});
await Task.WhenAll(tasks);
TranslatedContent = node;
return node;
});
Expand Down
15 changes: 7 additions & 8 deletions ExClient/Galleries/Gallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,18 @@ async Task<HtmlDocument> getDoc(int imageIndex, CancellationToken cancellationTo
return doc;
}

var rows = doc.GetElementbyId("gdo2").Elements("div", "ths").Last().GetInnerText();
rows = rows.Substring(0, rows.IndexOf(' '));
var rowCount = int.Parse(rows);
PageSize = rowCount * 5;
if (doc.GetElementbyId("gdo4").Elements("div", "ths").Last().InnerText != "Large")
var content = doc.GetElementbyId("gdt");
if(content.Element("div", "gdts") is not null)
{
// 切换到大图模式
await Client.Current.HttpClient.GetAsync(new Uri("/?inline_set=ts_l", UriKind.Relative));
doc = await getDoc(imageIndex, cancellationToken, true);
return await getDoc(imageIndex, cancellationToken, true);
}
else if (pageIndex != imageIndex / _PageSize)
var items = content.Elements("div", "gdtl").ToList();
PageSize = items.Count;
if (pageIndex != imageIndex / _PageSize)
{
doc = await getDoc(imageIndex, cancellationToken, true);
return await getDoc(imageIndex, cancellationToken, true);
}
return doc;
}
Expand Down
21 changes: 9 additions & 12 deletions ExClient/Galleries/GalleryImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ async Task loadFromApi()
doc.LoadHtml(res.i3);
analyzeI3Node(doc.DocumentNode);
doc.LoadHtml(res.i6);
analyzeI6Node(doc.DocumentNode);
doc.LoadHtml(res.i7);
analyzeI7Node(doc.DocumentNode);
analyzeI6Node(doc.DocumentNode);
}
catch (ArgumentException)
{
Expand All @@ -98,8 +96,7 @@ async Task loadFromHtml()

var doc = await Client.Current.HttpClient.GetDocumentAsync(loadPageUri).AsTask(token);
analyzeI3Node(doc.GetElementbyId("i3"));
analyzeI6Node(doc.GetElementbyId("i6"));
analyzeI7Node(doc.GetElementbyId("i7"));
analyzeI6Node(doc.GetElementbyId("i6"));
Owner.ShowKey = doc.DocumentNode.Descendants("script").Select(n =>
{
var match = _ShowKeyMatcher.Match(n.GetInnerText());
Expand All @@ -124,13 +121,13 @@ void analyzeI3Node(HtmlNode i3)
}
void analyzeI6Node(HtmlNode i6)
{
var hashNode = i6.Element("a");
ImageHash = SHA1Value.Parse(_HashMatcher.Match(hashNode.GetAttribute("href", "")).Groups[1].Value);
}
void analyzeI7Node(HtmlNode i7)
{
var origNode = i7.Element("a");
originalImageUri = origNode?.GetAttribute("href", default(Uri));
var links = i6.Descendants("a").Select(a => a.GetAttribute("href", "")).ToList();

var hashLink = links.First(l => l.Contains("f_shash="));
ImageHash = SHA1Value.Parse(_HashMatcher.Match(hashLink).Groups[1].Value);

var originalLink = links.FirstOrDefault(l => l.Contains("fullimg"));
originalImageUri = originalLink.IsNullOrEmpty() ? null : new Uri(originalLink);
}
}

Expand Down
21 changes: 8 additions & 13 deletions ExClient/Services/TorrentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static IAsyncOperation<ReadOnlyCollection<TorrentInfo>> FetchTorrnetsAsyn

public readonly struct TorrentInfo : IEquatable<TorrentInfo>
{
private static readonly Regex _InfoMatcher = new Regex(@"\s+Posted:\s([-\d:\s]+)\s+Size:\s([\d\.]+\s+[KMG]?B)\s+Seeds:\s(\d+)\s+Peers:\s(\d+)\s+Downloads:\s(\d+)\s+Uploader:\s+(.+)\s+", RegexOptions.Compiled);
private static readonly Regex _InfoMatcher = new Regex(@"\s+Posted:\s([-\d:\s]+)\s+Size:\s([\d\.]+\s+[KMG]?i?B)\s+Seeds:\s(\d+)\s+Peers:\s(\d+)\s+Downloads:\s(\d+)\s+Uploader:\s+(.+)\s+", RegexOptions.Compiled);
private static readonly Regex _UrlMatcher = new Regex(@"document\.location='([^']+?)'", RegexOptions.Compiled);

internal static IAsyncOperation<ReadOnlyCollection<TorrentInfo>> FetchAsync(GalleryInfo galleryInfo)
Expand Down Expand Up @@ -73,19 +73,14 @@ long parseSize(string sizeStr)
{
var s = sizeStr.Split(' ');
var value = double.Parse(s[0]);
switch (s[1])
return s[1] switch
{
case "B":
return (long)value;
case "KB":
return (long)(value * (1 << 10));
case "MB":
return (long)(value * (1 << 20));
case "GB":
return (long)(value * (1 << 30));
default:
return 0;
}
"B" => (long)value,
"KB" or "KiB" => (long)(value * (1 << 10)),
"MB" or "MiB" => (long)(value * (1 << 20)),
"GB" or "GiB" => (long)(value * (1 << 30)),
_ => 0,
};
}
}).AsAsyncOperation();
}
Expand Down
15 changes: 7 additions & 8 deletions ExClient/Status/TaggingRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
using HtmlAgilityPack;

using System;
using System.Collections.Generic;
using System.Linq;

namespace ExClient.Status
{
public readonly struct TaggingRecord : IEquatable<TaggingRecord>
{
internal TaggingRecord(HtmlNode trNode)
internal TaggingRecord(GalleryInfo gallery, List<HtmlNode> tdNodes)
{
var td = trNode.Elements("td").ToList();
Tag = Tag.Parse(td[1].GetInnerText());
Score = int.Parse(td[2].GetInnerText());
var uri = td[3].Element("a").GetAttribute("href", default(Uri));
GalleryInfo = GalleryInfo.Parse(uri);
Timestamp = DateTimeOffset.Parse(td[4].GetInnerText(), null, System.Globalization.DateTimeStyles.AssumeUniversal);
}
GalleryInfo = gallery;
Timestamp = DateTimeOffset.Parse(tdNodes[0].GetInnerText(), null, System.Globalization.DateTimeStyles.AssumeUniversal);
Score = int.Parse(tdNodes[1].GetInnerText());
Tag = Tag.Parse(tdNodes[2].GetInnerText());
}

public Tag Tag { get; }

Expand Down
30 changes: 24 additions & 6 deletions ExClient/Status/TaggingStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using HtmlAgilityPack;
using ExClient.Api;

using HtmlAgilityPack;

using Opportunity.MvvmUniverse;
using Opportunity.MvvmUniverse.Collections;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -34,10 +37,9 @@ public IAsyncAction RefreshAsync()
var uid = Client.Current.UserId;
if (uid < 0)
throw new InvalidOperationException("Hasn't log in");
var getPage = Client.Current.HttpClient.GetDocumentAsync(new Uri($"https://e-hentai.org/tools.php?act=taglist&uid={uid}"));
var getPage = Client.Current.HttpClient.GetDocumentAsync(new Uri($"https://repo.e-hentai.org/tools.php?act=taglist&uid={uid}"));
token.Register(getPage.Cancel);
var page = await getPage;
var body = page.DocumentNode.Element("html").Element("body");

var tagstats = page.GetElementbyId("tagstats")?.Elements("tr")?.ToArray();
if (tagstats != null && tagstats.Length == 3)
Expand All @@ -56,9 +58,25 @@ public IAsyncAction RefreshAsync()
}
}

var table = body.Element("table");
if (table != null)
records.Update(table.Elements("tr").Skip(1).Select(item => new TaggingRecord(item)).ToList());
var usertaglist = page.GetElementbyId("usertaglist");
if (usertaglist != null)
{
var list = new List<TaggingRecord>();
var gallary = default(GalleryInfo);
foreach (var row in usertaglist.Elements("tr"))
{
var cells = row.Elements("td").ToList();
if (cells.Count == 1)
{
gallary = GalleryInfo.Parse(cells[0].Descendants("a").Last().GetAttribute("href", default(Uri)));
}
else
{
list.Add(new TaggingRecord(gallary, cells));
}
}
records.Update(list);
}
else if (tagstats != null)
records.Clear();
OnPropertyChanged(default(string));
Expand Down
2 changes: 1 addition & 1 deletion ExClient/Status/UserStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public IAsyncAction ResetImageUsageAsync()
{
return AsyncInfo.Run(async token =>
{
var p = Client.Current.HttpClient.PostAsync(infoUri, new KeyValuePair<string, string>("act", "limits"));
var p = Client.Current.HttpClient.PostAsync(infoUri, new KeyValuePair<string, string>("reset_imagelimit", "reset_imagelimit"));
token.Register(p.Cancel);
var r = await p;
var html = await r.Content.ReadAsStringAsync();
Expand Down
4 changes: 4 additions & 0 deletions ExClient/Tagging/Namespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ private static readonly Dictionary<string, Namespace> parsingDic

["X"] = Namespace.Mixed,
["Mixed"] = Namespace.Mixed,

["T"] = Namespace.Temp,
["Temp"] = Namespace.Temp,
["Temporary"] = Namespace.Temp,
};

private static readonly Dictionary<Namespace, string> _SearchDic
Expand Down
3 changes: 1 addition & 2 deletions ExClient/Tagging/TagCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ private void _InitOrReset(IEnumerable<(Tag tag, TagState ts)> items)
{
var rawData = items.OrderBy(t => t.tag.Namespace)
// put low-power tags to the end
.ThenByDescending(t => t.ts & TagState.NormalPower)
.ThenBy(t => t.tag.Content)
.ThenByDescending(t => t.ts.GetPowerState())
.ToList();
var data = new Tag[rawData.Count];
var state = new TagState[rawData.Count];
Expand Down
2 changes: 1 addition & 1 deletion ExClient/Tagging/TagState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum TagState
/// <summary>
/// Mod-power &gt;= 100.
/// </summary>
HighPower = 0b0111_0000_0000
HighPower = 0b0111_0000_0000,
}

public static class TagStateExtension
Expand Down

0 comments on commit 2a0dc0c

Please sign in to comment.