From f1379bb9734ac014ad81be80cfe387f31eaf840c Mon Sep 17 00:00:00 2001 From: TaYaKi71751 Date: Tue, 26 Dec 2023 21:32:58 +0900 Subject: [PATCH] Add duckduckgo for ehash --- lib/component/eh/eh_headers.dart | 25 +++++++++++++++++++++++-- lib/component/hentai.dart | 19 +++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/component/eh/eh_headers.dart b/lib/component/eh/eh_headers.dart index c39bffea0..672c0f99e 100644 --- a/lib/component/eh/eh_headers.dart +++ b/lib/component/eh/eh_headers.dart @@ -5,6 +5,7 @@ import 'package:get/get.dart'; import 'package:html/parser.dart'; import 'package:http/http.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:violet/log/log.dart'; import 'package:violet/network/wrapper.dart' as http; class EHSession { @@ -119,7 +120,7 @@ class EHSession { } static Future getEHashById(String id) async { if(id.isEmpty) throw Error(); - var ehash; + String? ehash; await Future.forEach(['e-hentai.org','exhentai.org'],(host) async { if(ehash != null) return; try { @@ -132,6 +133,26 @@ class EHSession { return; } }); - return ehash ?? ''; + if(ehash != null) return ehash ?? ''; + if(ehash == null){ + final search_res = await http.post( + "https://lite.duckduckgo.com/lite/", + body: 'q=${('https://e-hentai.org/g/${id}/').replaceAll(':', '%3A').replaceAll('/', '%2F')}&kl=&df=', + headers: { + 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0', + 'Content-Type': 'application/x-www-form-urlencoded', + } + ); + final search_html = search_res.body; + final found_url = parse(search_html) + .querySelector('[href*="/g/${id}/"]') + ?.attributes['href']; + ehash = found_url?.split('/').lastWhere((element) => element.isNotEmpty); + } + if(ehash != null){ + return ehash ?? ''; + } + Logger.warning('[getEHashById] Could not found hash of ${id}'); + throw Error(); } } diff --git a/lib/component/hentai.dart b/lib/component/hentai.dart index b4351db1d..f9671c2c6 100644 --- a/lib/component/hentai.dart +++ b/lib/component/hentai.dart @@ -233,12 +233,16 @@ class HentaiManager { final v4 = ScriptManager.enableV4; for (int i = 0; i < route.length; i++) { + var ehash; try { switch (route[i]) { case 'EHentai': - if (qr.ehash() != null) { + if (qr.ehash() == null) { + ehash = await EHSession.getEHashById('${qr.id()}'); + } + if (qr.ehash() != null || ehash != null) { var html = await EHSession.requestString( - 'https://e-hentai.org/g/${qr.id()}/${qr.ehash()}/?p=0&inline_set=ts_m'); + 'https://e-hentai.org/g/${qr.id()}/${(qr.ehash() ?? ehash)}/?p=0&inline_set=ts_m'); var article = EHParser.parseArticleData(html); return EHentaiImageProvider( count: article.length, @@ -246,15 +250,18 @@ class HentaiManager { pagesUrl: List.generate( (article.length / 40).ceil(), (index) => - 'https://e-hentai.org/g/${qr.id()}/${qr.ehash()}/?p=$index'), + 'https://e-hentai.org/g/${qr.id()}/${(qr.ehash() ?? ehash)}/?p=$index'), isEHentai: true, ); } break; case 'ExHentai': - if (qr.ehash() != null) { + if (qr.ehash() == null) { + ehash = await EHSession.getEHashById('${qr.id()}'); + } + if (qr.ehash() != null || ehash != null) { var html = await EHSession.requestString( - 'https://exhentai.org/g/${qr.id()}/${qr.ehash()}/?p=0&inline_set=ts_m'); + 'https://exhentai.org/g/${qr.id()}/${(qr.ehash() ?? ehash)}/?p=0&inline_set=ts_m'); var article = EHParser.parseArticleData(html); return EHentaiImageProvider( count: article.length, @@ -262,7 +269,7 @@ class HentaiManager { pagesUrl: List.generate( (article.length / 40).ceil(), (index) => - 'https://exhentai.org/g/${qr.id()}/${qr.ehash()}/?p=$index'), + 'https://exhentai.org/g/${qr.id()}/${(qr.ehash() ?? ehash)}/?p=$index'), isEHentai: false, ); }