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

Sometimes publishedDate is only a year #5

Merged
merged 9 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ build/
# Directory created by dartdoc
doc/api/

test/
.vscode

.fvm/
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ final info = book.info;
| authors (`List<String>`) | All the authors names |
| publisher (`String`) | The publisher name |
| publishedDate (`DateTime`) | The date it was published |
| rawPublishedDate (`String`) | The date it was published in raw format |
| description (`String`) | Description of the book |
| pageCount (`int`) | The amount of pages |
| categories (`List<String>`) | The categories the book is in |
| averageRating (`double`) | The average rating of the book |
| ratingsCount (`int`) | The amount of people that rated it |
| maturityRating (`String`) | The maturity rating |
| contentVersion (`String`) | The version of the content |
| industryIdentifier (`List<IndustryIdentifier>`)| The identifiers of the book (isbn) |
| imageLinks (`List<Map<String, Uri>>`) | The links with the avaiable image resources |
| language (`String`) | The language code of the book |

Expand Down
79 changes: 68 additions & 11 deletions lib/src/scripts/books.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ class Book {
}
}

class IndustryIdentifier {
final String type;
final String identifier;


const IndustryIdentifier({
required this.type,
required this.identifier,
});

@override
String toString() => '$type:$identifier';

static IndustryIdentifier fromJson(Map<String, dynamic> json) {
return IndustryIdentifier(
type: json['type']??'',
identifier: json['identifier']??'',
);
}
}

class BookInfo {
/// The book title
final String title;
Expand All @@ -43,7 +64,10 @@ class BookInfo {
final String publisher;

/// The date the book was published
final DateTime publishedDate;
final DateTime? publishedDate;

/// The date the book was published in raw string format
final String rawPublishedDate;

/// The description of the book
final String description;
Expand Down Expand Up @@ -72,6 +96,9 @@ class BookInfo {
/// The original language of the book
final String language;

/// The industryIdentifiers of the book (isbn)
final List<IndustryIdentifier> industryIdentifier;

const BookInfo({
required this.title,
required this.authors,
Expand All @@ -85,16 +112,43 @@ class BookInfo {
required this.maturityRating,
required this.pageCount,
required this.publishedDate,
required this.rawPublishedDate,
required this.ratingsCount,
required this.industryIdentifier,
});

static BookInfo fromJson(Map<String, dynamic> json) {
final publishedDateArray =
((json['publishedDate'] as String?) ?? '0000-00-00').split('-');
final year = int.parse(publishedDateArray[0]);
final month = int.parse(publishedDateArray[1]);
final day = int.parse(publishedDateArray[2]);
final publishedDate = DateTime(year, month, day);

// initialize date
int year = 0;
int month = 1;
int day = 1;

// now test the date string
if(publishedDateArray.length==1) {
// assume we have only the year
year = int.parse(publishedDateArray[0]);
}
if(publishedDateArray.length==2) {
// assume we have the year and maybe the month (this could be just a speculative case)
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
}
if(publishedDateArray.length==3) {
// assume we have year-month-day
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
day = int.parse(publishedDateArray[2]);
}

// initialize datetime variable
DateTime? publishedDate = null;
if(publishedDateArray.length>0) {
publishedDate = DateTime(year, month, day);
}


final imageLinks = <String, Uri>{};
final map = json['imageLinks'] as Map<String, dynamic>?;
Expand All @@ -103,19 +157,21 @@ class BookInfo {
});

return BookInfo(
title: json['title'],
title: json['title']??'',
authors: ((json['authors'] as List<dynamic>?) ?? []).toStringList(),
publisher: json['publisher'],
publisher: json['publisher']??'',
averageRating: ((json['averageRating'] ?? 0) as num).toDouble(),
categories: ((json['categories'] as List<dynamic>?) ?? []).toStringList(),
contentVersion: json['contentVersion'],
description: json['description'],
language: json['language'],
maturityRating: json['maturityRating'],
contentVersion: json['contentVersion']??'',
description: json['description']??'',
language: json['language']??'',
maturityRating: json['maturityRating'] ??'',
pageCount: json['pageCount'] ?? 0,
ratingsCount: json['ratingsCount'] ?? 0,
publishedDate: publishedDate,
rawPublishedDate: (json['publishedDate'] as String?) ?? '',
imageLinks: imageLinks,
industryIdentifier: ((json['industryIdentifiers']??[]) as List).map((industryIdentifier) => IndustryIdentifier.fromJson(industryIdentifier)).toList(),
);
}

Expand All @@ -126,6 +182,7 @@ class BookInfo {
authors: $authors
publisher: $publisher
publishedDate: $publishedDate
rawPublishedDate: $rawPublishedDate
averageRating: $averageRating
categories: $categories
contentVersion $contentVersion
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ environment:

dependencies:
http: ^0.13.0

dev_dependencies:
test: ^1.16.8
63 changes: 63 additions & 0 deletions test/test_book_finder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:books_finder/src/books_finder_base.dart';
import 'package:test/test.dart';

void main() {
test('Get books', () async {
final List<Book> books = await queryBooks(
'twilight',
maxResults: 3,
printType: PrintType.books,
orderBy: OrderBy.relevance,
);
expect(books.length, 3);
});

test('Get book with special id', () async {
final Book book = await getSpecificBook('eI0TEAAAQBAJ');
expect(book.info.title,'Twilight, Say Cheese!');
expect(book.info.publisher,'Simon and Schuster');
expect(book.info.publishedDate,DateTime(2021,02,09));
expect(book.info.rawPublishedDate,'2021-02-09');
expect(book.info.authors.length,1);
expect(book.info.authors[0],'Daisy Sunshine');
expect(book.info.categories.length,4);
expect(book.info.categories[0],'Juvenile Fiction / Animals / Dragons, Unicorns & Mythical');
expect(book.info.pageCount,112);
expect(book.info.language,'en');
expect(book.info.description.isNotEmpty,true);
expect(book.info.maturityRating,'NOT_MATURE');
expect(book.info.contentVersion,'preview-1.0.0');
expect(book.info.industryIdentifier[0].type,'ISBN_10');
expect(book.info.industryIdentifier[0].identifier,'1534461655');
});

test('Get magazines', () async {
final List<Book> magazines = await queryBooks(
'New York Magazine',
maxResults: 3,
printType: PrintType.magazines,
orderBy: OrderBy.relevance,
);
expect(magazines.length, 3);
expect(magazines[0].info.industryIdentifier.length, 0);
});

test('Get magazine with special id', () async {
final Book book = await getSpecificBook('OugCAAAAMBAJ');
expect(book.info.title,'New York Magazine');
expect(book.info.publisher,'New York Media, LLC');
expect(book.info.publishedDate,DateTime(1997,06,02));
expect(book.info.rawPublishedDate,'1997-06-02');
expect(book.info.authors.length,1);
expect(book.info.authors[0],'New York Media, LLC');
expect(book.info.categories.length,0);
expect(book.info.pageCount,142);
expect(book.info.language,'en');
expect(book.info.description.isNotEmpty,true);
expect(book.info.maturityRating,'NOT_MATURE');
expect(book.info.contentVersion,'0.0.2.0.preview.1');
expect(book.info.industryIdentifier.length,1);
expect(book.info.industryIdentifier[0].type,'ISSN');
expect(book.info.industryIdentifier[0].identifier,'00287369');
});
}