-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
48 deletions.
There are no files selected for viewing
77 changes: 66 additions & 11 deletions
77
packages/uni_app/lib/controller/fetchers/book_fetcher.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,81 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:uni/controller/networking/network_router.dart'; | ||
|
||
class BookThumbFetcher { | ||
Future<String> fetchBookThumb(String isbn) async { | ||
final url = Uri.https( | ||
Future<String?> fetchBookThumb(String isbn) async { | ||
final googleBooksFuture = _fetchFromGoogleBooks(isbn); | ||
final openLibraryFuture = _fetchFromOpenLibrary(isbn); | ||
|
||
final completer = Completer<String?>(); | ||
|
||
googleBooksFuture.then((result) { | ||
if (result != null && !completer.isCompleted) { | ||
completer.complete(result); | ||
} | ||
}); | ||
|
||
openLibraryFuture.then((result) { | ||
if (result != null && !completer.isCompleted) { | ||
completer.complete(result); | ||
} | ||
}); | ||
|
||
Future.wait([googleBooksFuture, openLibraryFuture]).then((results) { | ||
if (!completer.isCompleted) { | ||
completer.complete(null); | ||
} | ||
}); | ||
|
||
return completer.future; | ||
} | ||
|
||
Future<String?> _fetchFromGoogleBooks(String isbn) async { | ||
final googleBooksUrl = Uri.https( | ||
'www.googleapis.com', | ||
'/books/v1/volumes', | ||
{'q': '+isbn:$isbn'}, | ||
{'q': 'isbn:$isbn'}, | ||
); | ||
|
||
final response = | ||
await http.get(Uri.decodeComponent(url.toString()).toUri()); | ||
await http.get(Uri.decodeComponent(googleBooksUrl.toString()).toUri()); | ||
|
||
final numBooks = ((json.decode(response.body) | ||
as Map<String, dynamic>)['totalItems'] as int) | ||
.toInt(); | ||
|
||
if (numBooks > 0) { | ||
final bookInformation = ((json.decode(response.body) | ||
as Map<String, dynamic>)['items'] as List<dynamic>) | ||
.first as Map<String, dynamic>; | ||
final thumbnailURL = | ||
((bookInformation['volumeInfo'] as Map<String, dynamic>)['imageLinks'] | ||
as Map<String, dynamic>)['thumbnail']; | ||
return thumbnailURL.toString(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
final bookInformation = ((json.decode(response.body) | ||
as Map<String, dynamic>)['items'] as List<dynamic>) | ||
.first as Map<String, dynamic>; | ||
Future<String?> _fetchFromOpenLibrary(String isbn) async { | ||
final url = Uri.https('covers.openlibrary.org', '/b/isbn/$isbn-M.jpg'); | ||
|
||
final thumbnailURL = | ||
((bookInformation['volumeInfo'] as Map<String, dynamic>)['imageLinks'] | ||
as Map<String, dynamic>)['thumbnail']; | ||
try { | ||
final response = await http.get(url); | ||
|
||
return thumbnailURL.toString(); | ||
if (response.statusCode == 200) { | ||
final contentType = response.headers['content-type']; | ||
if (contentType != null && contentType.startsWith('image/')) { | ||
return url.toString(); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters