mirror of
https://github.com/dstark5/Openlib.git
synced 2025-07-03 13:06:47 +08:00
Fixed trending page request connection error
This commit is contained in:
@ -8,9 +8,31 @@ class TrendingBookData {
|
||||
TrendingBookData({this.title, this.thumbnail});
|
||||
}
|
||||
|
||||
class OpenLibrary {
|
||||
String url = "https://openlibrary.org/trending/daily";
|
||||
abstract class TrendingBooksImpl {
|
||||
String url = '';
|
||||
int timeOutDuration = 20;
|
||||
List<TrendingBookData> _parser(dynamic data);
|
||||
|
||||
Future<List<TrendingBookData>> trendingBooks() async {
|
||||
try {
|
||||
final dio = Dio();
|
||||
final response = await dio.get(url,
|
||||
options: Options(
|
||||
sendTimeout: Duration(seconds: timeOutDuration),
|
||||
receiveTimeout: Duration(seconds: timeOutDuration)));
|
||||
return _parser(response.data.toString());
|
||||
} on DioException catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OpenLibrary extends TrendingBooksImpl {
|
||||
OpenLibrary() {
|
||||
super.url = "https://openlibrary.org/trending/daily";
|
||||
}
|
||||
|
||||
@override
|
||||
List<TrendingBookData> _parser(data) {
|
||||
var document = parse(data.toString());
|
||||
var bookList = document.querySelectorAll('li[class="searchResultItem"]');
|
||||
@ -32,32 +54,33 @@ class OpenLibrary {
|
||||
return trendingBooks;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<TrendingBookData>> trendingBooks() async {
|
||||
try {
|
||||
final dio = Dio();
|
||||
const timeOutDuration = 5;
|
||||
final response = await dio.get(url,
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 20),
|
||||
receiveTimeout: const Duration(seconds: 20)));
|
||||
sendTimeout: const Duration(seconds: timeOutDuration),
|
||||
receiveTimeout: const Duration(seconds: timeOutDuration)));
|
||||
final response2 = await dio.get(
|
||||
"https://openlibrary.org/trending/daily?page=2",
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 20),
|
||||
receiveTimeout: const Duration(seconds: 20)));
|
||||
sendTimeout: const Duration(seconds: timeOutDuration),
|
||||
receiveTimeout: const Duration(seconds: timeOutDuration)));
|
||||
return _parser('${response.data.toString()}${response2.data.toString()}');
|
||||
} on DioException catch (e) {
|
||||
return [];
|
||||
// if (e.type == DioExceptionType.unknown) {
|
||||
// throw "socketException";
|
||||
// }
|
||||
// rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GoodReads {
|
||||
String url = "https://www.goodreads.com/shelf/show/trending";
|
||||
class GoodReads extends TrendingBooksImpl {
|
||||
GoodReads() {
|
||||
super.url = "https://www.goodreads.com/shelf/show/trending";
|
||||
}
|
||||
|
||||
@override
|
||||
List<TrendingBookData> _parser(data) {
|
||||
var document = parse(data.toString());
|
||||
var bookList = document.querySelectorAll('div[class="elementList"]');
|
||||
@ -85,29 +108,15 @@ class GoodReads {
|
||||
}
|
||||
return trendingBooks;
|
||||
}
|
||||
|
||||
Future<List<TrendingBookData>> trendingBooks() async {
|
||||
try {
|
||||
final dio = Dio();
|
||||
final response = await dio.get(url,
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 20),
|
||||
receiveTimeout: const Duration(seconds: 20)));
|
||||
return _parser(response.data.toString());
|
||||
} on DioException catch (e) {
|
||||
return [];
|
||||
// if (e.type == DioExceptionType.unknown) {
|
||||
// throw "socketException";
|
||||
// }
|
||||
// rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PenguinRandomHouse {
|
||||
String url =
|
||||
class PenguinRandomHouse extends TrendingBooksImpl {
|
||||
PenguinRandomHouse() {
|
||||
super.url =
|
||||
"https://www.penguinrandomhouse.com/ajaxc/categories/books/?from=0&to=50&contentId=&elClass=book&dataType=html&catFilter=best-sellers";
|
||||
}
|
||||
|
||||
@override
|
||||
List<TrendingBookData> _parser(data) {
|
||||
var document = parse(data.toString());
|
||||
var bookList = document.querySelectorAll('div[class="book"]');
|
||||
@ -134,21 +143,34 @@ class PenguinRandomHouse {
|
||||
}
|
||||
return trendingBooks;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<TrendingBookData>> trendingBooks() async {
|
||||
try {
|
||||
final dio = Dio();
|
||||
final response = await dio.get(url,
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 20),
|
||||
receiveTimeout: const Duration(seconds: 20)));
|
||||
return _parser(response.data.toString());
|
||||
} on DioException catch (e) {
|
||||
return [];
|
||||
// if (e.type == DioExceptionType.unknown) {
|
||||
// throw "socketException";
|
||||
// }
|
||||
// rethrow;
|
||||
class BookDigits extends TrendingBooksImpl {
|
||||
BookDigits() {
|
||||
super.url = "https://bookdigits.com/fresh";
|
||||
}
|
||||
|
||||
@override
|
||||
List<TrendingBookData> _parser(data) {
|
||||
var document = parse(data.toString());
|
||||
var bookList = document.querySelectorAll('div[class="list-row"]');
|
||||
List<TrendingBookData> trendingBooks = [];
|
||||
for (var element in bookList) {
|
||||
if (element.querySelector('div[class="list-title link-reg"]')?.text !=
|
||||
null &&
|
||||
element.querySelector('img')?.attributes['src'] != null) {
|
||||
String? thumbnail = element.querySelector('img')?.attributes['src'];
|
||||
trendingBooks.add(
|
||||
TrendingBookData(
|
||||
title: element
|
||||
.querySelector('div[class="list-title link-reg"]')
|
||||
?.text
|
||||
.toString()
|
||||
.trim(),
|
||||
thumbnail: thumbnail.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
return trendingBooks;
|
||||
}
|
||||
}
|
||||
|
@ -72,18 +72,20 @@ final searchQueryProvider = StateProvider<String>((ref) => "");
|
||||
//Provider for Trending Books
|
||||
|
||||
final getTrendingBooks = FutureProvider<List<TrendingBookData>>((ref) async {
|
||||
OpenLibrary openLibrary = OpenLibrary();
|
||||
// OpenLibrary openLibrary = OpenLibrary();
|
||||
GoodReads goodReads = GoodReads();
|
||||
PenguinRandomHouse penguinTrending = PenguinRandomHouse();
|
||||
BookDigits bookDigits = BookDigits();
|
||||
List<TrendingBookData> trendingBooks =
|
||||
await Future.wait<List<TrendingBookData>>([
|
||||
goodReads.trendingBooks(),
|
||||
penguinTrending.trendingBooks(),
|
||||
openLibrary.trendingBooks(),
|
||||
// openLibrary.trendingBooks(),
|
||||
bookDigits.trendingBooks(),
|
||||
]).then((List<List<TrendingBookData>> listOfData) =>
|
||||
listOfData.expand((element) => element).toList());
|
||||
|
||||
if(trendingBooks.isEmpty){
|
||||
if (trendingBooks.isEmpty) {
|
||||
throw 'Nothing Trending Today :(';
|
||||
}
|
||||
trendingBooks.shuffle();
|
||||
|
@ -13,6 +13,8 @@ class AboutPage extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const version = "1.0.9";
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.background,
|
||||
@ -47,7 +49,7 @@ class AboutPage extends StatelessWidget {
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: 7, right: 7, top: 5),
|
||||
child: Text(
|
||||
"1.0.8",
|
||||
version,
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.bold,
|
||||
|
@ -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.8+11
|
||||
version: 1.0.9+12
|
||||
|
||||
environment:
|
||||
sdk: ">=3.3.0 <4.0.0"
|
||||
@ -36,6 +36,7 @@ dependencies:
|
||||
|
||||
flutter_pdfview: ^1.2.7
|
||||
epub_view: ^3.2.0
|
||||
# vocsy_epub_viewer: ^3.0.0
|
||||
# syncfusion_flutter_pdfviewer: ^22.2.5
|
||||
# pdfx: ^2.4.0
|
||||
|
||||
|
Reference in New Issue
Block a user