@@ -3,6 +3,7 @@ import { MediaTypeModel } from '../../models/MediaTypeModel';
33import MediaDbPlugin from '../../main' ;
44import { BookModel } from 'src/models/BookModel' ;
55import { requestUrl } from 'obsidian' ;
6+ import { contactEmail , mediaDbVersion , pluginName } from '../../utils/Utils' ;
67import { MediaType } from '../../utils/MediaType' ;
78
89export class OpenLibraryAPI extends APIModel {
@@ -21,7 +22,7 @@ export class OpenLibraryAPI extends APIModel {
2122async searchByTitle ( title : string ) : Promise < MediaTypeModel [ ] > {
2223 console . log ( `MDB | api "${ this . apiName } " queried by Title` ) ;
2324
24- const searchUrl = `https://openlibrary.org/search.json?title=${ encodeURIComponent ( title ) } &limit=20} ` ;
25+ const searchUrl = `https://openlibrary.org/search.json?title=${ encodeURIComponent ( title ) } ` ;
2526
2627 const fetchData = await fetch ( searchUrl ) ;
2728 console . debug ( fetchData ) ;
@@ -37,12 +38,11 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
3738 for ( const result of data . docs ) {
3839 ret . push (
3940 new BookModel ( {
40- subType : '' ,
4141 title : result . title ,
4242 englishTitle : result . title_english ?? result . title ,
43- year : result . year ?? result . aired ?. prop ?. from ?. year ?? '' ,
43+ year : result . first_publish_year ,
4444 dataSource : this . apiName ,
45- id : result . mal_id ,
45+ id : result . cover_edition_key ,
4646 } as BookModel )
4747 ) ;
4848 }
@@ -53,28 +53,34 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
5353async getById ( id : string ) : Promise < MediaTypeModel > {
5454 console . log ( `MDB | api "${ this . apiName } " queried by ID` ) ;
5555
56- const searchUrl = `https://openlibrary.org/isbn/${ encodeURIComponent ( id ) } .json` ;
57- const fetchData = await fetch ( searchUrl ) ;
56+ const searchUrl = `https://openlibrary.org/books/${ encodeURIComponent ( id ) } .json` ;
57+ const fetchData = await requestUrl ( {
58+ url : searchUrl ,
59+ headers : {
60+ 'User-Agent' : `${ pluginName } /${ mediaDbVersion } (${ contactEmail } )` ,
61+ } ,
62+ } ) ;
63+
64+ console . debug ( fetchData ) ;
5865
5966 if ( fetchData . status !== 200 ) {
6067 throw Error ( `MDB | Received status code ${ fetchData . status } from an API.` ) ;
6168 }
6269
63- const data = await fetchData . json ( ) ;
64- console . debug ( data ) ;
65- const result = data . data ;
70+ const result = await fetchData . json ;
6671
6772 const model = new BookModel ( {
68- subType : '' ,
6973 title : result . title ,
70- year : new Date ( result . publish_date . date ) . getFullYear ( ) . toString ( ) ,
74+ year : new Date ( result . publish_date ) . getFullYear ( ) . toString ( ) ,
7175 dataSource : this . apiName ,
7276 url : `https://openlibrary.org` + result . key ,
73- id : result . isbn_10 ,
77+ id : result . key . slice ( 7 ) ,
78+ isbn10 : result . isbn_10 ,
79+ englishTitle : result . title_english ?? result . title ,
7480
7581 author : result . authors . key ?? 'unknown' ,
7682 pages : result . number_of_pages ?? 'unknown' ,
77- image : `https://covers.openlibrary.org/b/isbn /` + result . isbn_10 + `-L.jpg` ?? '' ,
83+ image : `https://covers.openlibrary.org/b/OLID /` + result . key . slice ( 7 ) + `-L.jpg` ?? '' ,
7884
7985 released : true ,
8086
@@ -85,6 +91,6 @@ async getById(id: string): Promise<MediaTypeModel> {
8591 } ,
8692 } as BookModel ) ;
8793
88- return ;
94+ return model ;
8995}
9096}
0 commit comments