From bfad3f92f2b8fdd327c962807828ece3b585fe45 Mon Sep 17 00:00:00 2001 From: dstark5 <136567021+dstark5@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:44:53 +0530 Subject: [PATCH] Fixed 'stuck in solve captcha screen' --- lib/services/annas_archieve.dart | 59 ++++++++++++++------------------ lib/services/download_file.dart | 11 +++--- lib/ui/about_page.dart | 2 +- lib/ui/book_info_page.dart | 26 ++++++++------ lib/ui/webview_page.dart | 30 +++++++++------- pubspec.yaml | 2 +- 6 files changed, 67 insertions(+), 63 deletions(-) diff --git a/lib/services/annas_archieve.dart b/lib/services/annas_archieve.dart index 8ab65b8..e7173a4 100644 --- a/lib/services/annas_archieve.dart +++ b/lib/services/annas_archieve.dart @@ -162,52 +162,43 @@ class AnnasArchieve { Future _bookInfoParser(resData, url) async { var document = parse(resData.toString()); var main = document.querySelector('main[class="main"]'); - var ul = main?.querySelectorAll('ul[class="list-inside mb-4 ml-1"]'); + var ul = main?.querySelectorAll('ul[class="list-inside mb-4 ml-1"]>li>a'); + var externalUrlAnchorTags = main + ?.querySelector( + 'ul[class="list-inside mb-4 ml-1 js-show-external hidden"]') + ?.querySelectorAll('li>a'); // List mirrors = []; - - // if (ul != null) { - // var anchorTags = []; - - // for (var e in ul) { - // anchorTags.insertAll(0, e.querySelectorAll('a')); - // } - - // for (var element in anchorTags) { - // if (element.attributes['href'] != null && - // element.attributes['href']!.startsWith('/slow_download') && - // element.attributes['href']!.endsWith('/2')) { - // String? url = - // await _getMirrorLink('$baseUrl${element.attributes['href']!}'); - // if (url != null && url.isNotEmpty) { - // mirrors.add(url); - // } - // } else if (element.attributes['href']!.startsWith('https://')) { - // if (element.attributes['href'] != null && - // element.attributes['href'].contains('ipfs') == true) { - // mirrors.add(element.attributes['href']!); - // } - // } - // } - // } String? mirror; var anchorTags = []; if (ul != null) { - for (var e in ul) { - anchorTags.insertAll(0, e.querySelectorAll('a')); + for (var element in ul) { + if (element.attributes['href'] != null && + element.attributes['href']!.startsWith('/slow_download') && + element.attributes['href']!.endsWith('/2')) { + mirror = '$baseUrl${element.attributes['href']}'; + } } } - for (var element in anchorTags) { - if (element.attributes['href'] != null && - element.attributes['href']!.startsWith('/slow_download') && - element.attributes['href']!.endsWith('/2')) { - mirror = '$baseUrl${element.attributes['href']}'; + if (mirror == null) { + if (externalUrlAnchorTags != null) { + for (var e in externalUrlAnchorTags) { + if (e.attributes['href'] != null) { + anchorTags.add(e.attributes['href']); + } + } + } + + for (var element in anchorTags) { + if (element.startsWith('/ipfs_downloads')) { + mirror = '$baseUrl$element'; + } } } - // print(mirrors); + // print(mirror); var data = { 'title': main?.querySelector('div[class="text-3xl font-bold"]')?.text, diff --git a/lib/services/download_file.dart b/lib/services/download_file.dart index f698c75..8c1ff00 100644 --- a/lib/services/download_file.dart +++ b/lib/services/download_file.dart @@ -35,17 +35,20 @@ List _reorderMirrors(List mirrors) { Future _getAliveMirror(List mirrors) async { Dio dio = Dio(); + const timeOut = 15; + if (mirrors.length == 1) { + await Future.delayed(const Duration(seconds: 2)); + return mirrors[0]; + } for (var url in mirrors) { try { final response = await dio.head(url, - options: Options(receiveTimeout: const Duration(seconds: 25))); + options: Options(receiveTimeout: const Duration(seconds: timeOut))); if (response.statusCode == 200) { dio.close(); return url; } - } catch (_) { - // print("timeOut"); - } + } catch (_) {} } return null; } diff --git a/lib/ui/about_page.dart b/lib/ui/about_page.dart index 8fb3e7f..975e5db 100644 --- a/lib/ui/about_page.dart +++ b/lib/ui/about_page.dart @@ -13,7 +13,7 @@ class AboutPage extends StatelessWidget { @override Widget build(BuildContext context) { - const version = "1.0.10"; + const version = "1.0.11"; return Scaffold( appBar: AppBar( diff --git a/lib/ui/book_info_page.dart b/lib/ui/book_info_page.dart index 1401f3e..7758227 100644 --- a/lib/ui/book_info_page.dart +++ b/lib/ui/book_info_page.dart @@ -223,14 +223,20 @@ class _ActionButtonWidgetState extends ConsumerState { ), ), onPressed: () async { - final result = await Navigator.push(context, - MaterialPageRoute(builder: (BuildContext context) { - return Webview(url: widget.data.mirror ?? ''); - })); + if (widget.data.mirror != null && + widget.data.mirror != '') { + final result = await Navigator.push(context, + MaterialPageRoute(builder: (BuildContext context) { + return Webview(url: widget.data.mirror ?? ''); + })); - if (result != null) { - widget.data.mirror = result; - await downloadFileWidget(ref, context, widget.data); + if (result != null) { + await downloadFileWidget( + ref, context, widget.data, result); + } + } else { + showSnackBar( + context: context, message: 'No mirrors available!'); } }, child: const Text('Add To My Library'), @@ -253,16 +259,14 @@ class _ActionButtonWidgetState extends ConsumerState { } } -Future downloadFileWidget( - WidgetRef ref, BuildContext context, BookInfoData data) async { +Future downloadFileWidget(WidgetRef ref, BuildContext context, + BookInfoData data, List mirrors) async { showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return _ShowDialog(title: data.title); }); - - List mirrors = [data.mirror!]; // print(mirrors); downloadFile( mirrors: mirrors, diff --git a/lib/ui/webview_page.dart b/lib/ui/webview_page.dart index 36158bf..e83ca90 100644 --- a/lib/ui/webview_page.dart +++ b/lib/ui/webview_page.dart @@ -51,21 +51,27 @@ class _WebviewState extends ConsumerState { }, onLoadStart: (controller, url) {}, onLoadStop: (controller, url) async { - String query = - """var paragraphTag=document.querySelector('p[class="mb-4 text-xl font-bold"]');var anchorTagHref=paragraphTag.querySelector('a').href;var url=()=>{return anchorTagHref};url();"""; - String? mirrorLink = await webViewController - ?.evaluateJavascript(source: query); - // final ipfsUrl = widget.url - // .replaceAll("slow_download", "ipfs_downloads") - // .replaceAll("/0/2", ""); + List bookDownloadLinks = []; + if (url.toString().contains("slow_download")) { + String query = + """var paragraphTag=document.querySelector('p[class="mb-4 text-xl font-bold"]');var anchorTagHref=paragraphTag.querySelector('a').href;var url=()=>{return anchorTagHref};url();"""; + String? mirrorLink = await webViewController + ?.evaluateJavascript(source: query); + if (mirrorLink != null) { + bookDownloadLinks.add(mirrorLink); + } + } else { + String query = + """var ipfsLinkTags=document.querySelectorAll('ul>li>a');var ipfsLinks=[];var getIpfsLinks=()=>{ipfsLinkTags.forEach(e=>{ipfsLinks.push(e.href)});return ipfsLinks};getIpfsLinks();"""; + List mirrorLinks = await webViewController + ?.evaluateJavascript(source: query); + bookDownloadLinks = mirrorLinks.cast(); + } - // await webViewController?.loadUrl( - // urlRequest: URLRequest( - // url: WebUri('https://example.com/new-page'))); - if (mirrorLink != null) { + if (bookDownloadLinks.isNotEmpty) { Future.delayed(const Duration(milliseconds: 70), () { // ignore: use_build_context_synchronously - Navigator.pop(context, mirrorLink); + Navigator.pop(context, bookDownloadLinks); }); } }, diff --git a/pubspec.yaml b/pubspec.yaml index b1fcaab..446405c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.10+13 +version: 1.0.11+14 environment: sdk: ">=3.3.0 <4.0.0"