Skip to content

Commit

Permalink
add: google book cover images in various sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 17, 2022
1 parent d7258df commit f9c3a86
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/apis/google_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,27 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
}

createBookItem(item: VolumeInfo): Book {
const coverUrl = item.imageLinks?.thumbnail?.replace('http:', 'https:') ?? '';
const book: Book = {
title: item.title,
author: this.formatList(item.authors),
category: this.formatList(item.categories),
publisher: item.publisher,
totalPage: item.pageCount,
coverUrl: `${item.imageLinks?.thumbnail ?? ''}`.replace('http:', 'https:'),
coverUrl: coverUrl,
coverSmallUrl: this.convertGoogleBookImageURLSize(coverUrl, 1),
coverMediumUrl: this.convertGoogleBookImageURLSize(coverUrl, 2),
coverLargeUrl: this.convertGoogleBookImageURLSize(coverUrl, 3),
publishDate: item.publishedDate ? `${new Date(item.publishedDate).getFullYear()}` : '',
...this.getISBN(item.industryIdentifiers),
};
return book;
}

convertGoogleBookImageURLSize(url: string, zoom: number) {
return url.replace(/(&zoom)=\d/, `$1=${zoom}`);
}

formatList(list?: string[]) {
if (list?.length > 1) {
return list.map(item => `${item.trim()}`).join(', ');
Expand Down
3 changes: 3 additions & 0 deletions src/models/book.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Book {
totalPage?: number | string; // 전체 페이지
coverUrl?: string; // 커버 URL
coverSmallUrl?: string; // 커버 URL
coverMediumUrl?: string; // 커버 URL
coverLargeUrl?: string; // 커버 URL
status?: string; // 읽기 상태(읽기전, 읽는중, 읽기완료)
startReadDate?: string; // 읽기 시작한 일시
Expand All @@ -33,6 +34,7 @@ export class BookModel implements Book {
totalPage?: number | string;
coverUrl?: string;
coverSmallUrl?: string;
coverMediumUrl?: string;
coverLargeUrl?: string;
status?: string;
startReadDate?: string;
Expand All @@ -54,6 +56,7 @@ export class BookModel implements Book {
this.totalPage = book.totalPage ?? '';
this.coverUrl = book.coverUrl ?? '';
this.coverSmallUrl = book.coverSmallUrl ?? '';
this.coverMediumUrl = book.coverMediumUrl ?? '';
this.coverLargeUrl = book.coverLargeUrl ?? '';
this.status = book.status ?? '';
this.startReadDate = book.startReadDate ?? '';
Expand Down

0 comments on commit f9c3a86

Please sign in to comment.