Skip to content

Commit

Permalink
get image url from api #290
Browse files Browse the repository at this point in the history
  • Loading branch information
3003h committed Nov 2, 2023
1 parent d0e0b75 commit 0a84720
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 32 deletions.
3 changes: 2 additions & 1 deletion jsons/gallery_image.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"downloadProcess?": 1.0,
"errorInfo?": "",
"tempPath?": "",
"filename?": ""
"filename?": "",
"showKey?": ""
}
3 changes: 2 additions & 1 deletion lib/common/controller/download_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ class DownloadController extends GetxController {
}) async {
final String? _sourceId = changeSource ? sourceId : '';

final GalleryImage? _image = await fetchImageInfo(
final GalleryImage? _image = await fetchImageInfoByApi(
href,
refresh: changeSource,
sourceId: _sourceId,
Expand All @@ -832,6 +832,7 @@ class DownloadController extends GetxController {
imageHeight: _image.imageHeight,
originImageUrl: _image.originImageUrl,
filename: _image.filename,
showKey: _image.showKey,
);

return _imageCopyWith;
Expand Down
1 change: 1 addition & 0 deletions lib/common/parser/eh_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export 'mpv_parser.dart';
export 'mytags_parser.dart';
export 'profile_parser.dart';
export 'search_parser.dart';
export 'showpage_parser.dart';
export 'torrent_parser.dart';
export 'user_profile_parser.dart';
9 changes: 8 additions & 1 deletion lib/common/parser/image_page_parser.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:fehviewer/const/const.dart';
import 'package:fehviewer/models/index.dart';
import 'package:fehviewer/utils/logger.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' show parse;
import 'package:html_unescape/html_unescape.dart';
Expand Down Expand Up @@ -57,7 +58,12 @@ GalleryImage paraImage(String htmlText) {
originImageUrl =
'${htmlUnescape.convert(match!.group(1)!)}fullimg${htmlUnescape.convert(match.group(2)!)}';
}
// print('====================>$originImageUrl');
logger.t('========>$originImageUrl');

// showKey 用于api请求后续图片 script中
final String showKey =
RegExp(r'showkey="(.*?)"').firstMatch(htmlText)?.group(1) ?? '';
logger.t('showKey $showKey');

final GalleryImage _reImage = kDefGalleryImage.copyWith(
imageUrl: imageUrl,
Expand All @@ -69,6 +75,7 @@ GalleryImage paraImage(String htmlText) {
ser: ser,
originImageUrl: originImageUrl,
filename: filename,
showKey: showKey,
);

return _reImage;
Expand Down
74 changes: 74 additions & 0 deletions lib/common/parser/showpage_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'dart:convert';

import 'package:fehviewer/component/exception/error.dart';
import 'package:fehviewer/const/const.dart';
import 'package:fehviewer/models/index.dart';
import 'package:fehviewer/utils/logger.dart';
import 'package:html_unescape/html_unescape.dart';

GalleryImage paraShowPage(String jsonString) {
final HtmlUnescape htmlUnescape = HtmlUnescape();
final jsonMap = jsonDecode(jsonString) as Map<String, dynamic>;

final RegExp regImageUrl = RegExp(r'<img[^>]*src="([^"]+)" style');
final String imageUrl =
regImageUrl.firstMatch('${jsonMap['i3']}')?.group(1) ?? '';

// throw EhError(type: EhErrorType.image509);

if (imageUrl.endsWith('/509.gif') || imageUrl.endsWith('/509s.gif')) {
throw EhError(type: EhErrorType.image509);
}

logger.d('largeImageUrl $imageUrl');

final RegExpMatch? _xy = RegExp(r'(\S+)\s+::\s+(\d+)\s+x\s+(\d+)(\s+::)?')
.firstMatch('${jsonMap['i']}');

final String? filename = _xy != null ? _xy.group(1)?.trim() : null;

// 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']}');

if (width == null || height == null) {
throw EhError(type: EhErrorType.parse, error: 'width or height is null');
}

final RegExp urlRegExp =
RegExp(r'https?://e[-x]hentai.org/g/([0-9]+)/([0-9a-z]+)/?');

final RegExpMatch? urlRult = urlRegExp.firstMatch('${jsonMap['i5']}');
final gid = urlRult?.group(1) ?? '';
final token = urlRult?.group(2) ?? '';

final int ser = int.parse('${jsonMap['p']}');

// 原图链接
final regExpOriginImageUrl = RegExp(r'<a href="([^"]+)fullimg([^"]+)">');
final match = regExpOriginImageUrl.firstMatch('${jsonMap['i6']}');
String? originImageUrl;
if (match?.groupCount == 2) {
originImageUrl =
'${htmlUnescape.convert(match!.group(1)!)}fullimg${htmlUnescape.convert(match.group(2)!)}';
}
logger.d('======>>>> originImageUrl: $originImageUrl');

final String _sourceId =
RegExp(r"nl\('(.*?)'\)").firstMatch('${jsonMap['i6']}')?.group(1) ?? '';

final GalleryImage _reImage = kDefGalleryImage.copyWith(
imageUrl: imageUrl,
sourceId: _sourceId,
imageWidth: width,
imageHeight: height,
gid: gid,
token: token,
ser: ser,
originImageUrl: originImageUrl,
filename: filename,
);

return _reImage;
}
2 changes: 2 additions & 0 deletions lib/component/exception/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ enum EhErrorType {
image509,

imageHide,

parse,
}

class EhError implements Exception {
Expand Down
19 changes: 13 additions & 6 deletions lib/models/gallery_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class GalleryImage {
this.errorInfo,
this.tempPath,
this.filename,
this.showKey,
});

final bool? largeThumb;
Expand Down Expand Up @@ -63,6 +64,7 @@ class GalleryImage {
final String? errorInfo;
final String? tempPath;
final String? filename;
final String? showKey;

factory GalleryImage.fromJson(Map<String,dynamic> json) => GalleryImage(
largeThumb: json['largeThumb'] != null ? json['largeThumb'] as bool : null,
Expand Down Expand Up @@ -92,7 +94,8 @@ class GalleryImage {
downloadProcess: json['downloadProcess'] != null ? json['downloadProcess'] as double : null,
errorInfo: json['errorInfo'] != null ? json['errorInfo'] as String : null,
tempPath: json['tempPath'] != null ? json['tempPath'] as String : null,
filename: json['filename'] != null ? json['filename'] as String : null
filename: json['filename'] != null ? json['filename'] as String : null,
showKey: json['showKey'] != null ? json['showKey'] as String : null
);

Map<String, dynamic> toJson() => {
Expand Down Expand Up @@ -123,7 +126,8 @@ class GalleryImage {
'downloadProcess': downloadProcess,
'errorInfo': errorInfo,
'tempPath': tempPath,
'filename': filename
'filename': filename,
'showKey': showKey
};

GalleryImage clone() => GalleryImage(
Expand Down Expand Up @@ -154,7 +158,8 @@ class GalleryImage {
downloadProcess: downloadProcess,
errorInfo: errorInfo,
tempPath: tempPath,
filename: filename
filename: filename,
showKey: showKey
);


Expand Down Expand Up @@ -186,7 +191,8 @@ class GalleryImage {
double? downloadProcess,
String? errorInfo,
String? tempPath,
String? filename
String? filename,
String? showKey
}) => GalleryImage(
largeThumb: largeThumb ?? this.largeThumb,
completeCache: completeCache ?? this.completeCache,
Expand Down Expand Up @@ -216,12 +222,13 @@ class GalleryImage {
errorInfo: errorInfo ?? this.errorInfo,
tempPath: tempPath ?? this.tempPath,
filename: filename ?? this.filename,
showKey: showKey ?? this.showKey,
);

@override
bool operator ==(Object other) => identical(this, other)
|| other is GalleryImage && largeThumb == other.largeThumb && completeCache == other.completeCache && startPrecache == other.startPrecache && ser == other.ser && href == other.href && imageUrl == other.imageUrl && originImageUrl == other.originImageUrl && thumbUrl == other.thumbUrl && thumbHeight == other.thumbHeight && thumbWidth == other.thumbWidth && imageHeight == other.imageHeight && imageWidth == other.imageWidth && oriHeight == other.oriHeight && oriWidth == other.oriWidth && offSet == other.offSet && sourceId == other.sourceId && completeHeight == other.completeHeight && gid == other.gid && token == other.token && completeDownload == other.completeDownload && filePath == other.filePath && changeSource == other.changeSource && hide == other.hide && checkHide == other.checkHide && downloadProcess == other.downloadProcess && errorInfo == other.errorInfo && tempPath == other.tempPath && filename == other.filename;
|| other is GalleryImage && largeThumb == other.largeThumb && completeCache == other.completeCache && startPrecache == other.startPrecache && ser == other.ser && href == other.href && imageUrl == other.imageUrl && originImageUrl == other.originImageUrl && thumbUrl == other.thumbUrl && thumbHeight == other.thumbHeight && thumbWidth == other.thumbWidth && imageHeight == other.imageHeight && imageWidth == other.imageWidth && oriHeight == other.oriHeight && oriWidth == other.oriWidth && offSet == other.offSet && sourceId == other.sourceId && completeHeight == other.completeHeight && gid == other.gid && token == other.token && completeDownload == other.completeDownload && filePath == other.filePath && changeSource == other.changeSource && hide == other.hide && checkHide == other.checkHide && downloadProcess == other.downloadProcess && errorInfo == other.errorInfo && tempPath == other.tempPath && filename == other.filename && showKey == other.showKey;

@override
int get hashCode => largeThumb.hashCode ^ completeCache.hashCode ^ startPrecache.hashCode ^ ser.hashCode ^ href.hashCode ^ imageUrl.hashCode ^ originImageUrl.hashCode ^ thumbUrl.hashCode ^ thumbHeight.hashCode ^ thumbWidth.hashCode ^ imageHeight.hashCode ^ imageWidth.hashCode ^ oriHeight.hashCode ^ oriWidth.hashCode ^ offSet.hashCode ^ sourceId.hashCode ^ completeHeight.hashCode ^ gid.hashCode ^ token.hashCode ^ completeDownload.hashCode ^ filePath.hashCode ^ changeSource.hashCode ^ hide.hashCode ^ checkHide.hashCode ^ downloadProcess.hashCode ^ errorInfo.hashCode ^ tempPath.hashCode ^ filename.hashCode;
int get hashCode => largeThumb.hashCode ^ completeCache.hashCode ^ startPrecache.hashCode ^ ser.hashCode ^ href.hashCode ^ imageUrl.hashCode ^ originImageUrl.hashCode ^ thumbUrl.hashCode ^ thumbHeight.hashCode ^ thumbWidth.hashCode ^ imageHeight.hashCode ^ imageWidth.hashCode ^ oriHeight.hashCode ^ oriWidth.hashCode ^ offSet.hashCode ^ sourceId.hashCode ^ completeHeight.hashCode ^ gid.hashCode ^ token.hashCode ^ completeDownload.hashCode ^ filePath.hashCode ^ changeSource.hashCode ^ hide.hashCode ^ checkHide.hashCode ^ downloadProcess.hashCode ^ errorInfo.hashCode ^ tempPath.hashCode ^ filename.hashCode ^ showKey.hashCode;
}
3 changes: 2 additions & 1 deletion lib/network/app_dio/http_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class GalleryMpvImageHttpTransformer extends HttpTransformer {
FutureOr<DioHttpResponse<GalleryImage>> parse(
Response<dynamic> response) async {
final html = response.data as String;
final mpvPage = await compute(parserMpvPage, html);
// final mpvPage = await compute(parserMpvPage, html);
final mpvPage = parserMpvPage(html);

if (mpvPage.mpvkey == null || mpvPage.mpvkey!.isEmpty) {
return DioHttpResponse<GalleryImage>.failure(
Expand Down
60 changes: 58 additions & 2 deletions lib/network/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,60 @@ Future<GalleryProvider?> getGalleryDetail({
}
}

Future<GalleryImage?> fetchImageInfo(
Future<GalleryImage?> fetchImageInfoByApi(
String href, {
String? showKey,
bool refresh = false,
String? sourceId,
CancelToken? cancelToken,
}) async {
// 如果 showKey 为空,直接使用常规请求,解析html
if (showKey == null || showKey.isEmpty) {
logger.d('fetchImageInfoByApi: showKey is null');
final _image = await _fetchImageInfo(
href,
refresh: refresh,
sourceId: sourceId,
cancelToken: cancelToken,
);
return _image;
}

logger.d(
'fetchImageInfoByApi: href $href, refresh $refresh, sourceId $sourceId');

String mpvSer = '1';
final isMpv = regGalleryMpvPageUrl.hasMatch(href);
if (isMpv) {
mpvSer = regGalleryMpvPageUrl.firstMatch(href)?.group(3) ?? '1';
}

final RegExp regExp =
RegExp(r'https://e[-x]hentai.org/s/([0-9a-z]+)/(\d+)-(\d+)');
final RegExpMatch? regRult = regExp.firstMatch(href);
final int gid = int.parse(regRult?.group(2) ?? '0');
final String imgkey = regRult?.group(1) ?? '';
final int page = int.parse(regRult?.group(3) ?? '0');

final Map<String, Object> reqMap = {
'method': 'showpage',
'gid': gid,
'page': page,
'imgkey': imgkey,
'showkey': showKey,
};

// const url = '/api.php';
// DioHttpClient dioHttpClient = DioHttpClient(dioConfig: globalDioConfig);
final String reqJsonStr = jsonEncode(reqMap);

// 请求api
final response = await postEhApi(reqJsonStr, forceRefresh: refresh);

return paraShowPage(response);
}

Future<GalleryImage?> _fetchImageInfo(
String href, {
bool refresh = false,
String? sourceId,
Expand Down Expand Up @@ -968,7 +1021,10 @@ Future<String> postEhApi(
bool forceRefresh = true,
}) async {
const String url = '/api.php';
DioHttpClient dioHttpClient = DioHttpClient(dioConfig: globalDioConfig);
DioHttpClient dioHttpClient = DioHttpClient(
dioConfig: globalDioConfig.copyWith(
contentType: Headers.jsonContentType,
));
DioHttpResponse httpResponse = await dioHttpClient.post(
url,
data: data,
Expand Down
30 changes: 25 additions & 5 deletions lib/pages/gallery/controller/gallery_page_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,16 @@ class GalleryPageController extends GetxController
gState.images.indexWhere((GalleryImage element) => element.ser == ser);
if (_index != null && _index >= 0) {
final image = imageCallback(gState.images[_index]);
// logger.d('${image.toJson()}');

// update showKey
if (image.showKey != null &&
image.showKey != gState.galleryProvider?.showKey) {
logger.d('update showKey ${image.showKey}');
gState.galleryProvider = gState.galleryProvider?.copyWith(
showKey: image.showKey,
);
}

gState.images[_index] = image;
return image;
}
Expand Down Expand Up @@ -537,6 +546,7 @@ class GalleryPageController extends GetxController
bool changeSource = false,
bool refresh = false,
}) async {
logger.d('fetchAndParserImageInfo $itemSer');
try {
/// 当前缩略图对象
final GalleryImage? _curImages = gState.imageMap[itemSer];
Expand All @@ -557,6 +567,8 @@ class GalleryPageController extends GetxController
final String? _sourceId =
changeSource ? gState.imageMap[itemSer]?.sourceId : '';

final _showKey = gState.galleryProvider?.showKey;

logger.t(
'ser:$itemSer ,href: ${gState.imageMap[itemSer]?.href} , _sourceId: $_sourceId');

Expand All @@ -568,15 +580,22 @@ class GalleryPageController extends GetxController
path: gState.imageMap[itemSer]?.href ?? '');
}

// 加载当前页信息
final GalleryImage? _image = await fetchImageInfo(
// 请求当前页信息
final GalleryImage? _image = await fetchImageInfoByApi(
gState.imageMap[itemSer]?.href ?? '',
sourceId: _sourceId,
refresh: changeSource || refresh,
debugLabel: 'fetchAndParserImageInfo 加载当前页信息',
showKey: _showKey,
);

logger.t('fetch _image ${_image?.toJson()}');
logger.d('fetch _image ${_image?.toJson()}');

if (_image?.showKey != null && _image?.showKey != _showKey) {
logger.d('update showKey ${_image?.showKey}');
gState.galleryProvider = gState.galleryProvider?.copyWith(
showKey: _image?.showKey,
);
}

// 换源加载
if (changeSource) {
Expand All @@ -598,6 +617,7 @@ class GalleryPageController extends GetxController
errorInfo: '',
tempPath: '',
completeCache: false,
showKey: _image.showKey,
);

return uptImageBySer(ser: itemSer, imageCallback: (image) => __image);
Expand Down
Loading

0 comments on commit 0a84720

Please sign in to comment.