Skip to content

Commit

Permalink
Merge pull request #388 from vvbbnn00/main
Browse files Browse the repository at this point in the history
Fixed the parsing issue in exhentai source when Thumbnail Settings is set to Small or Auto
  • Loading branch information
chihchy authored Oct 28, 2024
2 parents 2d72097 + d98965f commit 2c4c68d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
6 changes: 4 additions & 2 deletions EhPanda/App/Tools/Clients/UIApplicationClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ComposableArchitecture

struct UIApplicationClient {
let openURL: @MainActor (URL) -> Void
let hideKeyboard: () -> Void
let hideKeyboard: @Sendable () async -> Void
let alternateIconName: () -> String?
let setAlternateIconName: @MainActor (String?) async -> Bool
let setUserInterfaceStyle: @MainActor (UIUserInterfaceStyle) -> Void
Expand All @@ -23,7 +23,9 @@ extension UIApplicationClient {
UIApplication.shared.open(url, options: [:])
},
hideKeyboard: {
UIApplication.shared.endEditing()
await MainActor.run {
UIApplication.shared.endEditing()
}
},
alternateIconName: {
UIApplication.shared.alternateIconName
Expand Down
1 change: 1 addition & 0 deletions EhPanda/App/Tools/Extensions/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extension Encodable {

// MARK: UIApplication
extension UIApplication {
@MainActor
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
Expand Down
56 changes: 50 additions & 6 deletions EhPanda/App/Tools/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ struct Parser {
case _ where singlePageCount <= 20:
return .large(rows: 4)

// gt200
case _ where singlePageCount <= 40:
return .large(rows: 8)

// gdtl
case _ where singlePageCount <= 50:
return .large(rows: 10)

Expand All @@ -340,6 +345,11 @@ struct Parser {
case _ where singlePageCount <= 40:
return .normal(rows: 4)

// gt100
case _ where singlePageCount <= 80:
return .normal(rows: 8)

// gdtm
case _ where singlePageCount <= 100:
return .normal(rows: 10)

Expand Down Expand Up @@ -640,11 +650,42 @@ struct Parser {

return previewURLs
}
func parseGT100PreviewURLs(node: XMLElement) -> [Int: URL] {
var previewURLs = [Int: URL]()

for link in node.xpath("//a") {
if let divNode = link.at_xpath(".//div[@title and @style]"),
let style = divNode["style"],
let rangeA = style.range(of: "width:"),
let rangeB = style.range(of: "px;height:"),
let rangeC = style.range(of: "px;background"),
let rangeD = style.range(of: "url("),
let rangeE = style.range(of: ") -"),
let rangeF = style[rangeE.upperBound...].range(of: "px "),
let urlString = style[rangeD.upperBound..<rangeE.lowerBound]
.replacingOccurrences(of: "'", with: "")
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: urlString),
let title = divNode["title"],
let index = parseGTX00IndexFromTitle(from: title)
{
let width = String(style[rangeA.upperBound..<rangeB.lowerBound])
let height = String(style[rangeB.upperBound..<rangeC.lowerBound])
let offset = String(style[rangeE.upperBound..<rangeF.lowerBound])

previewURLs[index] = URLUtil.normalPreviewURL(
plainURL: url, width: width,
height: height, offset: offset
)
}
}
return previewURLs
}
func parseGT200PreviewURLs(node: XMLElement) -> [Int: URL] {
var previewURLs = [Int: URL]()

for link in node.xpath("//a") {
if let divNode = link.at_xpath("div"),
if let divNode = link.at_xpath(".//div[@title and @style]"),
let style = divNode["style"],
let rangeA = style.range(of: "url("),
let rangeB = style.range(of: ")"),
Expand All @@ -653,7 +694,7 @@ struct Parser {
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: urlString),
let title = divNode["title"],
let index = parseGT200IndexFromTitle(from: title)
let index = parseGTX00IndexFromTitle(from: title)
{
previewURLs[index] = url
}
Expand All @@ -668,6 +709,7 @@ struct Parser {
return switch previewMode {
case "gdtl": parseLargePreviewURLs(node: gdtNode)
case "gdtm": parseNormalPreviewURLs(node: gdtNode)
case "gt100": parseGT100PreviewURLs(node: gdtNode)
case "gt200": parseGT200PreviewURLs(node: gdtNode)
default: .init()
}
Expand Down Expand Up @@ -745,7 +787,7 @@ struct Parser {
return comments
}

static func parseGT200IndexFromTitle(from title: String) -> Int? {
static func parseGTX00IndexFromTitle(from title: String) -> Int? {
// The probable format of page title is "Page [Number]: filename"
(
title
Expand All @@ -765,13 +807,13 @@ struct Parser {
let previewMode = try? parsePreviewMode(doc: doc)
else { throw AppError.parseFailed }

if previewMode == "gt200" {
if ["gt100", "gt200"].contains(previewMode) {
for aLink in gdtNode.xpath("a") {
guard let href = aLink["href"],
let thumbnailURL = URL(string: href),
let divNode = aLink.at_xpath("div"),
let divNode = aLink.at_xpath(".//div[@title and @style]"),
let title = divNode["title"],
let index = parseGT200IndexFromTitle(from: title)
let index = parseGTX00IndexFromTitle(from: title)
else { continue }

thumbnailURLs[index] = thumbnailURL
Expand Down Expand Up @@ -817,6 +859,8 @@ struct Parser {
return "gdtm"
} else if doc.at_xpath("//div [@class='gdtl']") != nil {
return "gdtl"
} else if doc.at_xpath("//div [@class='gt100']") != nil {
return "gt100"
} else if doc.at_xpath("//div [@class='gt200']") != nil {
return "gt200"
} else {
Expand Down
2 changes: 1 addition & 1 deletion EhPanda/App/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@
"eh_setting_view.title.ip_address_port" = "IP 地址:端口";
"eh_setting_view.description.ip_address_port" = "如果你本地安装了 H@H 客户端,本地 IP 与浏览网站的公共 IP 相同,一些路由器不支持回流导致无法访问到自己,你可以设置这里来解决。\n如果在同一台设备上访问网站和运行客户端,请使用本地回环地址 (127.0.0.1:端口号)。如果客户端在网络上的其它设备运行,请使用那台机器的内网 IP。某些浏览器的配置可能阻止外部网站访问本地网络,你必须将网站列入白名单才能工作。";

"eh_setting_view.section.title.original_images" = "Use original images instead of the resampled versions? Resampled images will still be used if you select a horizontal resolution different than \"Auto\" above and the image in question is wider, or if the original image is larger than 10 MiB (or 4 MiB for galleries older than one year).";
"eh_setting_view.section.title.original_images" = "是否使用原始图像而非重新采样的版本?如果您在上方选择的水平分辨率不是“自动”,并且所查看的图像更宽,或者原始图像大于10 MiB(对于超过一年的图库,则为4 MiB),那么仍将使用重新采样的图像。";
"eh_setting_view.title.use_original_images" = "显示原图";

"eh_setting_view.section.title.multi_page_viewer" = "多页查看器";
Expand Down
2 changes: 1 addition & 1 deletion EhPanda/View/Detail/Previews/PreviewsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct PreviewsView: View {
}
.onAppear {
if store.databaseLoadingState != .loading
&& store.previewURLs[index] == nil && (index - 1) % 20 == 0
&& store.previewURLs[index] == nil && (index - 1) % 10 == 0
{
store.send(.fetchPreviewURLs(index))
}
Expand Down
2 changes: 1 addition & 1 deletion EhPanda/View/Setting/EhSetting/EhSettingReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct EhSettingReducer {
return .none

case .setKeyboardHidden:
return .run(operation: { _ in uiApplicationClient.hideKeyboard() })
return .run(operation: { _ in await uiApplicationClient.hideKeyboard() })

case .setDefaultProfile(let profileSet):
return .run { _ in
Expand Down

0 comments on commit 2c4c68d

Please sign in to comment.