Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added openLibrary API #1437

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions packages/uni_app/lib/controller/fetchers/book_fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
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 results = await Future.wait([googleBooksFuture, openLibraryFuture]);

return results.firstWhere((result) => result != null, orElse: () => null);
}

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;

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;
}

Future<String?> _fetchFromOpenLibrary(String isbn) async {
final url = Uri.https('covers.openlibrary.org', '/b/isbn/$isbn-M.jpg');

final bookInformation = ((json.decode(response.body)
as Map<String, dynamic>)['items'] as List<dynamic>)
.first as Map<String, dynamic>;
final response = await http.get(url);

final thumbnailURL =
((bookInformation['volumeInfo'] as Map<String, dynamic>)['imageLinks']
as Map<String, dynamic>)['thumbnail'];
if (response.statusCode == 200) {
final contentType = response.headers['content-type'];
if (contentType != null && contentType.startsWith('image/')) {
return url.toString();
}
}

return thumbnailURL.toString();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,46 +176,48 @@ Widget buildExpandedProfessors(
}

Widget buildBooksRow(BuildContext context, List<Book> books) {
return SizedBox(
height: 500,
width: double.infinity,
child: Wrap(
alignment: WrapAlignment.spaceBetween,
children: [
...books.asMap().entries.map((book) {
return FutureBuilder<String?>(
builder: (context, snapshot) {
return Padding(
padding: const EdgeInsets.only(left: 15, right: 15, top: 10),
child: Column(
children: [
SizedBox(
width: 135,
height: 140, // adjust this value as needed
child: snapshot.data != null
? Image(image: NetworkImage(snapshot.data!))
: const Image(
image: AssetImage(
'assets/images/book_placeholder.png',
),
return Wrap(
alignment: WrapAlignment.spaceBetween,
children: books.map((book) {
return Padding(
padding: const EdgeInsets.only(left: 15, right: 15, top: 10),
child: Column(
children: [
SizedBox(
width: 135,
height: 140,
child: book.isbn.isNotEmpty
? FutureBuilder<String?>(
future: BookThumbFetcher().fetchBookThumb(book.isbn),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
return Image(image: NetworkImage(snapshot.data!));
} else {
return const Image(
image: AssetImage(
'assets/images/book_placeholder.png',
),
),
SizedBox(
width: 135,
child: Text(
book.value.title,
textAlign: TextAlign.center,
);
}
},
)
: const Image(
image: AssetImage(
'assets/images/book_placeholder.png',
),
),
],
),
);
},
future: BookThumbFetcher().fetchBookThumb(book.value.isbn),
);
}),
],
),
),
SizedBox(
width: 135,
child: Text(
book.title,
textAlign: TextAlign.center,
),
),
],
),
);
}).toList(),
);
}

Expand Down
Loading