|
| 1 | +import {APIModel} from '../APIModel'; |
| 2 | +import {MediaTypeModel} from '../../models/MediaTypeModel'; |
| 3 | +import MediaDbPlugin from '../../main'; |
| 4 | + |
| 5 | +export class LocGovAPI extends APIModel { |
| 6 | + plugin: MediaDbPlugin; |
| 7 | + typeMappings: Map<string, string>; |
| 8 | + |
| 9 | + constructor(plugin: MediaDbPlugin) { |
| 10 | + super(); |
| 11 | + |
| 12 | + this.plugin = plugin; |
| 13 | + this.apiName = 'loc.gov API'; |
| 14 | + this.apiDescription = 'A free API for the Library of Congress collections.'; |
| 15 | + this.apiUrl = 'https://libraryofcongress.github.io/data-exploration/index.html'; |
| 16 | + this.types = []; |
| 17 | + this.typeMappings = new Map<string, string>(); |
| 18 | + // this.typeMappings.set('movie', 'movie'); |
| 19 | + } |
| 20 | + |
| 21 | + async searchByTitle(title: string): Promise<MediaTypeModel[]> { |
| 22 | + console.log(`MDB | api "${this.apiName}" queried`); |
| 23 | + |
| 24 | + const searchUrl = `https://www.loc.gov/search/?q=${title}&fo=json&c=20`; |
| 25 | + |
| 26 | + const fetchData = await fetch(searchUrl); |
| 27 | + console.log(fetchData); |
| 28 | + if (fetchData.status !== 200) { |
| 29 | + throw Error(`Received status code ${fetchData.status} from an API.`); |
| 30 | + } |
| 31 | + const data = await fetchData.json(); |
| 32 | + |
| 33 | + console.log(data); |
| 34 | + |
| 35 | + let ret: MediaTypeModel[] = []; |
| 36 | + |
| 37 | + /* |
| 38 | + for (const result of data.data) { |
| 39 | + const type = this.typeMappings.get(result.type.toLowerCase()); |
| 40 | + if (type === undefined) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + if (type === 'movie' || type === 'special') { |
| 44 | + ret.push(new MovieModel({ |
| 45 | + type: type, |
| 46 | + title: result.title, |
| 47 | + englishTitle: result.title_english ?? result.title, |
| 48 | + year: result.year ?? result.aired?.prop?.from?.year ?? '', |
| 49 | + dataSource: this.apiName, |
| 50 | + id: result.mal_id, |
| 51 | + } as MovieModel)); |
| 52 | + } else if (type === 'series' || type === 'ova') { |
| 53 | + ret.push(new SeriesModel({ |
| 54 | + type: type, |
| 55 | + title: result.title, |
| 56 | + englishTitle: result.title_english ?? result.title, |
| 57 | + year: result.year ?? result.aired?.prop?.from?.year ?? '', |
| 58 | + dataSource: this.apiName, |
| 59 | + id: result.mal_id, |
| 60 | + } as SeriesModel)); |
| 61 | + } |
| 62 | + } |
| 63 | + */ |
| 64 | + |
| 65 | + return ret; |
| 66 | + } |
| 67 | + |
| 68 | + async getById(item: MediaTypeModel): Promise<MediaTypeModel> { |
| 69 | + |
| 70 | + const searchUrl = `https://www.loc.gov/item/{item.id}/?fo=json`; |
| 71 | + |
| 72 | + const fetchData = await fetch(searchUrl); |
| 73 | + if (fetchData.status !== 200) { |
| 74 | + throw Error(`Received status code ${fetchData.status} from an API.`); |
| 75 | + } |
| 76 | + |
| 77 | + const data = await fetchData.json(); |
| 78 | + console.log(data); |
| 79 | + const result = data.data; |
| 80 | + |
| 81 | + const type = this.typeMappings.get(result.type.toLowerCase()); |
| 82 | + if (type === undefined) { |
| 83 | + throw Error(`${result.type.toLowerCase()} is an unsupported type.`); |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + if (type === 'movie' || type === 'special') { |
| 88 | + const model = new MovieModel({ |
| 89 | + type: type, |
| 90 | + title: result.title, |
| 91 | + englishTitle: result.title_english ?? result.title, |
| 92 | + year: result.year ?? result.aired?.prop?.from?.year ?? '', |
| 93 | + dataSource: this.apiName, |
| 94 | + url: result.url, |
| 95 | + id: result.mal_id, |
| 96 | +
|
| 97 | + genres: result.genres?.map((x: any) => x.name) ?? [], |
| 98 | + producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown', |
| 99 | + duration: result.duration ?? 'unknown', |
| 100 | + onlineRating: result.score ?? 0, |
| 101 | + image: result.images?.jpg?.image_url ?? '', |
| 102 | +
|
| 103 | + released: true, |
| 104 | + premiere: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown', |
| 105 | +
|
| 106 | + watched: false, |
| 107 | + lastWatched: '', |
| 108 | + personalRating: 0, |
| 109 | + } as MovieModel); |
| 110 | +
|
| 111 | + return model; |
| 112 | + } else if (type === 'series' || type === 'ova') { |
| 113 | + const model = new SeriesModel({ |
| 114 | + type: type, |
| 115 | + title: result.title, |
| 116 | + englishTitle: result.title_english ?? result.title, |
| 117 | + year: result.year ?? result.aired?.prop?.from?.year ?? '', |
| 118 | + dataSource: this.apiName, |
| 119 | + url: result.url, |
| 120 | + id: result.mal_id, |
| 121 | +
|
| 122 | + genres: result.genres?.map((x: any) => x.name) ?? [], |
| 123 | + studios: result.studios?.map((x: any) => x.name) ?? [], |
| 124 | + episodes: result.episodes, |
| 125 | + duration: result.duration ?? 'unknown', |
| 126 | + onlineRating: result.score ?? 0, |
| 127 | + image: result.images?.jpg?.image_url ?? '', |
| 128 | +
|
| 129 | + released: true, |
| 130 | + airedFrom: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown', |
| 131 | + airedTo: (new Date(result.aired?.to)).toLocaleDateString() ?? 'unknown', |
| 132 | + airing: result.airing, |
| 133 | +
|
| 134 | + watched: false, |
| 135 | + lastWatched: '', |
| 136 | + personalRating: 0, |
| 137 | + } as SeriesModel); |
| 138 | +
|
| 139 | + return model; |
| 140 | + } |
| 141 | + */ |
| 142 | + |
| 143 | + return; |
| 144 | + } |
| 145 | +} |
0 commit comments