Skip to content

Commit

Permalink
Adapt to the new thumbnail DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
3003h committed Oct 29, 2024
1 parent 5ba5991 commit 62efa16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
55 changes: 33 additions & 22 deletions lib/common/parser/gallery_detail_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ List<GalleryImage> parseGalleryImage(Document document) {
final List<GalleryImage> galleryImages = [];

// 小图 #gdt > div.gdtm
List<Element> picLsit = document.querySelectorAll('#gdt > div.gdtm');
if (picLsit.isNotEmpty) {
for (final Element pic in picLsit) {
List<Element> 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 =
Expand All @@ -398,18 +398,18 @@ List<GalleryImage> 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() ?? '';
Expand All @@ -425,28 +425,39 @@ List<GalleryImage> 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) ?? '';

Expand All @@ -455,9 +466,9 @@ List<GalleryImage> 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;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/parser/image_page_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions lib/common/parser/showpage_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 62efa16

Please sign in to comment.