Skip to content

Commit 1e84848

Browse files
committed
Fixed trending page request connection error
1 parent 78a50e2 commit 1e84848

4 files changed

Lines changed: 79 additions & 52 deletions

File tree

lib/services/open_library.dart

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,31 @@ class TrendingBookData {
88
TrendingBookData({this.title, this.thumbnail});
99
}
1010

11-
class OpenLibrary {
12-
String url = "https://openlibrary.org/trending/daily";
11+
abstract class TrendingBooksImpl {
12+
String url = '';
13+
int timeOutDuration = 20;
14+
List<TrendingBookData> _parser(dynamic data);
1315

16+
Future<List<TrendingBookData>> trendingBooks() async {
17+
try {
18+
final dio = Dio();
19+
final response = await dio.get(url,
20+
options: Options(
21+
sendTimeout: Duration(seconds: timeOutDuration),
22+
receiveTimeout: Duration(seconds: timeOutDuration)));
23+
return _parser(response.data.toString());
24+
} on DioException catch (e) {
25+
return [];
26+
}
27+
}
28+
}
29+
30+
class OpenLibrary extends TrendingBooksImpl {
31+
OpenLibrary() {
32+
super.url = "https://openlibrary.org/trending/daily";
33+
}
34+
35+
@override
1436
List<TrendingBookData> _parser(data) {
1537
var document = parse(data.toString());
1638
var bookList = document.querySelectorAll('li[class="searchResultItem"]');
@@ -32,32 +54,33 @@ class OpenLibrary {
3254
return trendingBooks;
3355
}
3456

57+
@override
3558
Future<List<TrendingBookData>> trendingBooks() async {
3659
try {
3760
final dio = Dio();
61+
const timeOutDuration = 5;
3862
final response = await dio.get(url,
3963
options: Options(
40-
sendTimeout: const Duration(seconds: 20),
41-
receiveTimeout: const Duration(seconds: 20)));
64+
sendTimeout: const Duration(seconds: timeOutDuration),
65+
receiveTimeout: const Duration(seconds: timeOutDuration)));
4266
final response2 = await dio.get(
4367
"https://openlibrary.org/trending/daily?page=2",
4468
options: Options(
45-
sendTimeout: const Duration(seconds: 20),
46-
receiveTimeout: const Duration(seconds: 20)));
69+
sendTimeout: const Duration(seconds: timeOutDuration),
70+
receiveTimeout: const Duration(seconds: timeOutDuration)));
4771
return _parser('${response.data.toString()}${response2.data.toString()}');
4872
} on DioException catch (e) {
4973
return [];
50-
// if (e.type == DioExceptionType.unknown) {
51-
// throw "socketException";
52-
// }
53-
// rethrow;
5474
}
5575
}
5676
}
5777

58-
class GoodReads {
59-
String url = "https://www.goodreads.com/shelf/show/trending";
78+
class GoodReads extends TrendingBooksImpl {
79+
GoodReads() {
80+
super.url = "https://www.goodreads.com/shelf/show/trending";
81+
}
6082

83+
@override
6184
List<TrendingBookData> _parser(data) {
6285
var document = parse(data.toString());
6386
var bookList = document.querySelectorAll('div[class="elementList"]');
@@ -85,29 +108,15 @@ class GoodReads {
85108
}
86109
return trendingBooks;
87110
}
88-
89-
Future<List<TrendingBookData>> trendingBooks() async {
90-
try {
91-
final dio = Dio();
92-
final response = await dio.get(url,
93-
options: Options(
94-
sendTimeout: const Duration(seconds: 20),
95-
receiveTimeout: const Duration(seconds: 20)));
96-
return _parser(response.data.toString());
97-
} on DioException catch (e) {
98-
return [];
99-
// if (e.type == DioExceptionType.unknown) {
100-
// throw "socketException";
101-
// }
102-
// rethrow;
103-
}
104-
}
105111
}
106112

107-
class PenguinRandomHouse {
108-
String url =
109-
"https://www.penguinrandomhouse.com/ajaxc/categories/books/?from=0&to=50&contentId=&elClass=book&dataType=html&catFilter=best-sellers";
113+
class PenguinRandomHouse extends TrendingBooksImpl {
114+
PenguinRandomHouse() {
115+
super.url =
116+
"https://www.penguinrandomhouse.com/ajaxc/categories/books/?from=0&to=50&contentId=&elClass=book&dataType=html&catFilter=best-sellers";
117+
}
110118

119+
@override
111120
List<TrendingBookData> _parser(data) {
112121
var document = parse(data.toString());
113122
var bookList = document.querySelectorAll('div[class="book"]');
@@ -134,21 +143,34 @@ class PenguinRandomHouse {
134143
}
135144
return trendingBooks;
136145
}
146+
}
137147

138-
Future<List<TrendingBookData>> trendingBooks() async {
139-
try {
140-
final dio = Dio();
141-
final response = await dio.get(url,
142-
options: Options(
143-
sendTimeout: const Duration(seconds: 20),
144-
receiveTimeout: const Duration(seconds: 20)));
145-
return _parser(response.data.toString());
146-
} on DioException catch (e) {
147-
return [];
148-
// if (e.type == DioExceptionType.unknown) {
149-
// throw "socketException";
150-
// }
151-
// rethrow;
148+
class BookDigits extends TrendingBooksImpl {
149+
BookDigits() {
150+
super.url = "https://bookdigits.com/fresh";
151+
}
152+
153+
@override
154+
List<TrendingBookData> _parser(data) {
155+
var document = parse(data.toString());
156+
var bookList = document.querySelectorAll('div[class="list-row"]');
157+
List<TrendingBookData> trendingBooks = [];
158+
for (var element in bookList) {
159+
if (element.querySelector('div[class="list-title link-reg"]')?.text !=
160+
null &&
161+
element.querySelector('img')?.attributes['src'] != null) {
162+
String? thumbnail = element.querySelector('img')?.attributes['src'];
163+
trendingBooks.add(
164+
TrendingBookData(
165+
title: element
166+
.querySelector('div[class="list-title link-reg"]')
167+
?.text
168+
.toString()
169+
.trim(),
170+
thumbnail: thumbnail.toString()),
171+
);
172+
}
152173
}
174+
return trendingBooks;
153175
}
154176
}

lib/state/state.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,22 @@ final searchQueryProvider = StateProvider<String>((ref) => "");
7272
//Provider for Trending Books
7373

7474
final getTrendingBooks = FutureProvider<List<TrendingBookData>>((ref) async {
75-
OpenLibrary openLibrary = OpenLibrary();
75+
// OpenLibrary openLibrary = OpenLibrary();
7676
GoodReads goodReads = GoodReads();
7777
PenguinRandomHouse penguinTrending = PenguinRandomHouse();
78+
BookDigits bookDigits = BookDigits();
7879
List<TrendingBookData> trendingBooks =
7980
await Future.wait<List<TrendingBookData>>([
8081
goodReads.trendingBooks(),
8182
penguinTrending.trendingBooks(),
82-
openLibrary.trendingBooks(),
83+
// openLibrary.trendingBooks(),
84+
bookDigits.trendingBooks(),
8385
]).then((List<List<TrendingBookData>> listOfData) =>
8486
listOfData.expand((element) => element).toList());
8587

86-
if(trendingBooks.isEmpty){
88+
if (trendingBooks.isEmpty) {
8789
throw 'Nothing Trending Today :(';
88-
}
90+
}
8991
trendingBooks.shuffle();
9092
return trendingBooks;
9193
});

lib/ui/about_page.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class AboutPage extends StatelessWidget {
1313

1414
@override
1515
Widget build(BuildContext context) {
16+
const version = "1.0.9";
17+
1618
return Scaffold(
1719
appBar: AppBar(
1820
backgroundColor: Theme.of(context).colorScheme.background,
@@ -47,7 +49,7 @@ class AboutPage extends StatelessWidget {
4749
Padding(
4850
padding: EdgeInsets.only(left: 7, right: 7, top: 5),
4951
child: Text(
50-
"1.0.8",
52+
version,
5153
style: TextStyle(
5254
fontSize: 15,
5355
fontWeight: FontWeight.bold,

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
1616
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
1717
# In Windows, build-name is used as the major, minor, and patch parts
1818
# of the product and file versions while build-number is used as the build suffix.
19-
version: 1.0.8+11
19+
version: 1.0.9+12
2020

2121
environment:
2222
sdk: ">=3.3.0 <4.0.0"
@@ -36,6 +36,7 @@ dependencies:
3636

3737
flutter_pdfview: ^1.2.7
3838
epub_view: ^3.2.0
39+
# vocsy_epub_viewer: ^3.0.0
3940
# syncfusion_flutter_pdfviewer: ^22.2.5
4041
# pdfx: ^2.4.0
4142

0 commit comments

Comments
 (0)