From 7ba91c2a2f3592b389b8a4e77692bf75722b7aa8 Mon Sep 17 00:00:00 2001 From: mProjectsCode Date: Wed, 18 May 2022 20:05:45 +0200 Subject: [PATCH] wikipedia api #3 --- src/api/apis/LocGovAPI.ts | 145 +++++++++++++++++++++++++++++++++++ src/api/apis/WikipediaAPI.ts | 78 +++++++++++++++++++ src/main.ts | 3 + src/models/WikiModel.ts | 33 ++++++++ 4 files changed, 259 insertions(+) create mode 100644 src/api/apis/LocGovAPI.ts create mode 100644 src/api/apis/WikipediaAPI.ts create mode 100644 src/models/WikiModel.ts diff --git a/src/api/apis/LocGovAPI.ts b/src/api/apis/LocGovAPI.ts new file mode 100644 index 0000000..8e8fa42 --- /dev/null +++ b/src/api/apis/LocGovAPI.ts @@ -0,0 +1,145 @@ +import {APIModel} from '../APIModel'; +import {MediaTypeModel} from '../../models/MediaTypeModel'; +import MediaDbPlugin from '../../main'; + +export class LocGovAPI extends APIModel { + plugin: MediaDbPlugin; + typeMappings: Map; + + constructor(plugin: MediaDbPlugin) { + super(); + + this.plugin = plugin; + this.apiName = 'loc.gov API'; + this.apiDescription = 'A free API for the Library of Congress collections.'; + this.apiUrl = 'https://libraryofcongress.github.io/data-exploration/index.html'; + this.types = []; + this.typeMappings = new Map(); + // this.typeMappings.set('movie', 'movie'); + } + + async searchByTitle(title: string): Promise { + console.log(`MDB | api "${this.apiName}" queried`); + + const searchUrl = `https://www.loc.gov/search/?q=${title}&fo=json&c=20`; + + const fetchData = await fetch(searchUrl); + console.log(fetchData); + if (fetchData.status !== 200) { + throw Error(`Received status code ${fetchData.status} from an API.`); + } + const data = await fetchData.json(); + + console.log(data); + + let ret: MediaTypeModel[] = []; + + /* + for (const result of data.data) { + const type = this.typeMappings.get(result.type.toLowerCase()); + if (type === undefined) { + continue; + } + if (type === 'movie' || type === 'special') { + ret.push(new MovieModel({ + type: type, + title: result.title, + englishTitle: result.title_english ?? result.title, + year: result.year ?? result.aired?.prop?.from?.year ?? '', + dataSource: this.apiName, + id: result.mal_id, + } as MovieModel)); + } else if (type === 'series' || type === 'ova') { + ret.push(new SeriesModel({ + type: type, + title: result.title, + englishTitle: result.title_english ?? result.title, + year: result.year ?? result.aired?.prop?.from?.year ?? '', + dataSource: this.apiName, + id: result.mal_id, + } as SeriesModel)); + } + } + */ + + return ret; + } + + async getById(item: MediaTypeModel): Promise { + + const searchUrl = `https://www.loc.gov/item/{item.id}/?fo=json`; + + const fetchData = await fetch(searchUrl); + if (fetchData.status !== 200) { + throw Error(`Received status code ${fetchData.status} from an API.`); + } + + const data = await fetchData.json(); + console.log(data); + const result = data.data; + + const type = this.typeMappings.get(result.type.toLowerCase()); + if (type === undefined) { + throw Error(`${result.type.toLowerCase()} is an unsupported type.`); + } + + /* + if (type === 'movie' || type === 'special') { + const model = new MovieModel({ + type: type, + title: result.title, + englishTitle: result.title_english ?? result.title, + year: result.year ?? result.aired?.prop?.from?.year ?? '', + dataSource: this.apiName, + url: result.url, + id: result.mal_id, + + genres: result.genres?.map((x: any) => x.name) ?? [], + producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown', + duration: result.duration ?? 'unknown', + onlineRating: result.score ?? 0, + image: result.images?.jpg?.image_url ?? '', + + released: true, + premiere: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown', + + watched: false, + lastWatched: '', + personalRating: 0, + } as MovieModel); + + return model; + } else if (type === 'series' || type === 'ova') { + const model = new SeriesModel({ + type: type, + title: result.title, + englishTitle: result.title_english ?? result.title, + year: result.year ?? result.aired?.prop?.from?.year ?? '', + dataSource: this.apiName, + url: result.url, + id: result.mal_id, + + genres: result.genres?.map((x: any) => x.name) ?? [], + studios: result.studios?.map((x: any) => x.name) ?? [], + episodes: result.episodes, + duration: result.duration ?? 'unknown', + onlineRating: result.score ?? 0, + image: result.images?.jpg?.image_url ?? '', + + released: true, + airedFrom: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown', + airedTo: (new Date(result.aired?.to)).toLocaleDateString() ?? 'unknown', + airing: result.airing, + + watched: false, + lastWatched: '', + personalRating: 0, + } as SeriesModel); + + return model; + } + */ + + return; + } +} diff --git a/src/api/apis/WikipediaAPI.ts b/src/api/apis/WikipediaAPI.ts new file mode 100644 index 0000000..5e141e2 --- /dev/null +++ b/src/api/apis/WikipediaAPI.ts @@ -0,0 +1,78 @@ +import {APIModel} from '../APIModel'; +import {MediaTypeModel} from '../../models/MediaTypeModel'; +import MediaDbPlugin from '../../main'; +import {WikiModel} from '../../models/WikiModel'; + +export class WikipediaAPI extends APIModel { + plugin: MediaDbPlugin; + + constructor(plugin: MediaDbPlugin) { + super(); + + this.plugin = plugin; + this.apiName = 'Wikipedia API'; + this.apiDescription = 'The API behind Wikipedia'; + this.apiUrl = 'https://www.wikipedia.com'; + this.types = ['wiki']; + } + + async searchByTitle(title: string): Promise { + console.log(`MDB | api "${this.apiName}" queried`); + + const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${title}&srlimit=20&utf8=&format=json&origin=*`; + + const fetchData = await fetch(searchUrl); + console.log(fetchData); + if (fetchData.status !== 200) { + throw Error(`Received status code ${fetchData.status} from an API.`); + } + const data = await fetchData.json(); + + console.log(data); + + let ret: MediaTypeModel[] = []; + + + for (const result of data.query.search) { + ret.push(new WikiModel({ + type: 'wiki', + title: result.title, + englishTitle: result.title, + year: '', + dataSource: this.apiName, + id: result.pageid, + } as WikiModel)); + } + + return ret; + } + + async getById(item: MediaTypeModel): Promise { + + const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${item.id}&inprop=url&format=json&origin=*`; + + const fetchData = await fetch(searchUrl); + if (fetchData.status !== 200) { + throw Error(`Received status code ${fetchData.status} from an API.`); + } + + const data = await fetchData.json(); + console.log(data); + const result = Object.entries(data?.query?.pages)[0][1]; + + const model = new WikiModel({ + type: 'wiki', + title: result.title, + englishTitle: result.title, + year: '', + dataSource: this.apiName, + id: result.pageid, + + wikiUrl: result.fullurl, + lastUpdated: (new Date(result.touched)).toLocaleDateString() ?? 'unknown', + length: result.length, + } as WikiModel); + + return model; + } +} diff --git a/src/main.ts b/src/main.ts index 5cad2c0..880d086 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal'; import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal'; import {MALAPI} from './api/apis/MALAPI'; import {MediaDbIdSearchModal} from './modals/MediaDbIdSearchModal'; +import {WikipediaAPI} from './api/apis/WikipediaAPI'; export default class MediaDbPlugin extends Plugin { settings: MediaDbPluginSettings; @@ -45,6 +46,8 @@ export default class MediaDbPlugin extends Plugin { this.apiManager.registerAPI(new TestAPI()); this.apiManager.registerAPI(new OMDbAPI(this)); this.apiManager.registerAPI(new MALAPI(this)); + this.apiManager.registerAPI(new WikipediaAPI(this)); + // this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data } async createMediaDbNote(modal: () => Promise): Promise { diff --git a/src/models/WikiModel.ts b/src/models/WikiModel.ts new file mode 100644 index 0000000..b97eac8 --- /dev/null +++ b/src/models/WikiModel.ts @@ -0,0 +1,33 @@ +import {MediaTypeModel} from './MediaTypeModel'; +import {stringifyYaml} from 'obsidian'; + + +export class WikiModel extends MediaTypeModel { + type: string; + title: string; + englishTitle: string; + year: string; + dataSource: string; + url: string; + id: string; + + wikiUrl: string; + lastUpdated: string; + length: number; + + + constructor(obj: any = {}) { + super(); + + Object.assign(this, obj); + } + + toMetaData(): string { + return stringifyYaml(this); + } + + getFileName(): string { + return this.title; + } + +}