From 62efa16f0d05305199d548b0cf4e7f26fa5dff7c Mon Sep 17 00:00:00 2001 From: 3003h Date: Tue, 29 Oct 2024 12:21:17 +0800 Subject: [PATCH] Adapt to the new thumbnail DOM --- lib/common/parser/gallery_detail_parser.dart | 55 ++++++++++++-------- lib/common/parser/image_page_parser.dart | 2 +- lib/common/parser/showpage_parser.dart | 4 +- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/common/parser/gallery_detail_parser.dart b/lib/common/parser/gallery_detail_parser.dart index cbf8bfc11..94ab37c2d 100644 --- a/lib/common/parser/gallery_detail_parser.dart +++ b/lib/common/parser/gallery_detail_parser.dart @@ -376,9 +376,9 @@ List parseGalleryImage(Document document) { final List galleryImages = []; // 小图 #gdt > div.gdtm - List picLsit = document.querySelectorAll('#gdt > div.gdtm'); - if (picLsit.isNotEmpty) { - for (final Element pic in picLsit) { + List picList = document.querySelectorAll('#gdt > div.gdtm'); + if (picList.isNotEmpty) { + for (final Element pic in picList) { final String picHref = pic.querySelector('a')?.attributes['href'] ?? ''; final String style = pic.querySelector('div')?.attributes['style'] ?? ''; final String picSrcUrl = @@ -398,18 +398,18 @@ List parseGalleryImage(Document document) { largeThumb: false, href: picHref, thumbUrl: picSrcUrl, - thumbHeight: double.parse(height), - thumbWidth: double.parse(width), - offSet: double.parse(offSet), + thumbHeight: double.tryParse(height) ?? 0, + thumbWidth: double.tryParse(width) ?? 0, + offSet: double.tryParse(offSet) ?? 0, )); } return galleryImages; } // 大图 #gdt > div.gdtl - picLsit = document.querySelectorAll('#gdt > div.gdtl'); - if (picLsit.isNotEmpty) { - for (final Element pic in picLsit) { + picList = document.querySelectorAll('#gdt > div.gdtl'); + if (picList.isNotEmpty) { + for (final Element pic in picList) { final String picHref = pic.querySelector('a')?.attributes['href'] ?? ''; final Element? imgElem = pic.querySelector('img'); final String picSer = imgElem?.attributes['alt']?.trim() ?? ''; @@ -425,28 +425,39 @@ List parseGalleryImage(Document document) { largeThumb: true, href: picHref, thumbUrl: picSrcUrl, - oriWidth: double.parse(width), - oriHeight: double.parse(height), + oriWidth: double.tryParse(width) ?? 0, + oriHeight: double.tryParse(height) ?? 0, )); } return galleryImages; } // 里站 #gdt > a - picLsit = document.querySelectorAll('#gdt > a'); - if (picLsit.isNotEmpty) { - for (final Element pic in picLsit) { + // 新版缩略图dom, 统一了大小缩略图, 小图不再需要单独的分割处理 + picList = document.querySelectorAll('#gdt > a'); + if (picList.isNotEmpty) { + for (final Element pic in picList) { final String picHref = pic.attributes['href'] ?? ''; - final String style = pic.querySelector('div')?.attributes['style'] ?? ''; + + // 对 label 不为空设置的处理 + final divElm = pic.querySelector('div'); + final childrenElms = divElm?.children; + logger.d('>>>> childrenElms count: ${childrenElms?.length}'); + final hasChildren = childrenElms?.isNotEmpty ?? false; + final destDivElm = hasChildren ? childrenElms![0] : divElm; + final String style = destDivElm?.attributes['style'] ?? ''; + logger.d('>>>> style: $style'); + final String picSrcUrl = RegExp(r'url\((.+)\)').firstMatch(style)?.group(1) ?? ''; final String height = - RegExp(r'height:(\d+)?px').firstMatch(style)?.group(1) ?? ''; + RegExp(r'height:(\d+)?px').firstMatch(style)?.group(1) ?? '0'; final String width = - RegExp(r'width:(\d+)?px').firstMatch(style)?.group(1) ?? ''; + RegExp(r'width:(\d+)?px').firstMatch(style)?.group(1) ?? '0'; final String offSet = - RegExp(r'\) -(\d+)?px ').firstMatch(style)?.group(1) ?? ''; - final String title = pic.querySelector('div')?.attributes['title'] ?? ''; + RegExp(r'\) -(\d+)?px ').firstMatch(style)?.group(1) ?? '0'; + + final String title = destDivElm?.attributes['title'] ?? ''; final String picSer = RegExp(r'Page (\d+):').firstMatch(title)?.group(1) ?? ''; @@ -455,9 +466,9 @@ List parseGalleryImage(Document document) { largeThumb: false, href: picHref, thumbUrl: picSrcUrl, - thumbHeight: double.parse(height), - thumbWidth: double.parse(width), - offSet: double.parse(offSet), + thumbHeight: double.tryParse(height) ?? 0, + thumbWidth: double.tryParse(width) ?? 0, + offSet: double.tryParse(offSet) ?? 0, )); } return galleryImages; diff --git a/lib/common/parser/image_page_parser.dart b/lib/common/parser/image_page_parser.dart index d2fa7978a..59461632e 100644 --- a/lib/common/parser/image_page_parser.dart +++ b/lib/common/parser/image_page_parser.dart @@ -24,7 +24,7 @@ GalleryImage paraImage(String htmlText) { final Element? elmI2 = document.querySelector('#i2 > div:nth-child(1)'); final RegExpMatch? _xy = RegExp(r'(\S+)\s+::\s+(\d+)\s+x\s+(\d+)(\s+::)?') .firstMatch(elmI2?.text ?? ''); - final String? filename = _xy != null ? _xy.group(1)?.trim() : null; + final String? filename = _xy?.group(1)?.trim(); final double? width = _xy != null ? double.parse(_xy.group(2)!) : null; final double? height = _xy != null ? double.parse(_xy.group(3)!) : null; diff --git a/lib/common/parser/showpage_parser.dart b/lib/common/parser/showpage_parser.dart index 926e1e719..4abde3ccc 100644 --- a/lib/common/parser/showpage_parser.dart +++ b/lib/common/parser/showpage_parser.dart @@ -24,8 +24,8 @@ GalleryImage paraShowPage(String jsonString) { // final double? width = _xy != null ? double.parse(_xy.group(2)!) : null; // final double? height = _xy != null ? double.parse(_xy.group(3)!) : null; - final double? width = double.parse('${jsonMap['x']}'); - final double? height = double.parse('${jsonMap['y']}'); + final double? width = double.tryParse('${jsonMap['x']}'); + final double? height = double.tryParse('${jsonMap['y']}'); if (width == null || height == null) { throw EhError(type: EhErrorType.parse, error: 'width or height is null');