wikipedia api #3
This commit is contained in:
parent
912418d3cf
commit
7ba91c2a2f
4 changed files with 259 additions and 0 deletions
145
src/api/apis/LocGovAPI.ts
Normal file
145
src/api/apis/LocGovAPI.ts
Normal file
|
|
@ -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<string, string>;
|
||||
|
||||
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<string, string>();
|
||||
// this.typeMappings.set('movie', 'movie');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
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<MediaTypeModel> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
78
src/api/apis/WikipediaAPI.ts
Normal file
78
src/api/apis/WikipediaAPI.ts
Normal file
|
|
@ -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<MediaTypeModel[]> {
|
||||
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<MediaTypeModel> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<MediaTypeModel>): Promise<void> {
|
||||
|
|
|
|||
33
src/models/WikiModel.ts
Normal file
33
src/models/WikiModel.ts
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue