Skip to content

Commit 7ba91c2

Browse files
committed
wikipedia api #3
1 parent 912418d commit 7ba91c2

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

src/api/apis/LocGovAPI.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

src/api/apis/WikipediaAPI.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {APIModel} from '../APIModel';
2+
import {MediaTypeModel} from '../../models/MediaTypeModel';
3+
import MediaDbPlugin from '../../main';
4+
import {WikiModel} from '../../models/WikiModel';
5+
6+
export class WikipediaAPI extends APIModel {
7+
plugin: MediaDbPlugin;
8+
9+
constructor(plugin: MediaDbPlugin) {
10+
super();
11+
12+
this.plugin = plugin;
13+
this.apiName = 'Wikipedia API';
14+
this.apiDescription = 'The API behind Wikipedia';
15+
this.apiUrl = 'https://www.wikipedia.com';
16+
this.types = ['wiki'];
17+
}
18+
19+
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
20+
console.log(`MDB | api "${this.apiName}" queried`);
21+
22+
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${title}&srlimit=20&utf8=&format=json&origin=*`;
23+
24+
const fetchData = await fetch(searchUrl);
25+
console.log(fetchData);
26+
if (fetchData.status !== 200) {
27+
throw Error(`Received status code ${fetchData.status} from an API.`);
28+
}
29+
const data = await fetchData.json();
30+
31+
console.log(data);
32+
33+
let ret: MediaTypeModel[] = [];
34+
35+
36+
for (const result of data.query.search) {
37+
ret.push(new WikiModel({
38+
type: 'wiki',
39+
title: result.title,
40+
englishTitle: result.title,
41+
year: '',
42+
dataSource: this.apiName,
43+
id: result.pageid,
44+
} as WikiModel));
45+
}
46+
47+
return ret;
48+
}
49+
50+
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
51+
52+
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${item.id}&inprop=url&format=json&origin=*`;
53+
54+
const fetchData = await fetch(searchUrl);
55+
if (fetchData.status !== 200) {
56+
throw Error(`Received status code ${fetchData.status} from an API.`);
57+
}
58+
59+
const data = await fetchData.json();
60+
console.log(data);
61+
const result = Object.entries(data?.query?.pages)[0][1];
62+
63+
const model = new WikiModel({
64+
type: 'wiki',
65+
title: result.title,
66+
englishTitle: result.title,
67+
year: '',
68+
dataSource: this.apiName,
69+
id: result.pageid,
70+
71+
wikiUrl: result.fullurl,
72+
lastUpdated: (new Date(result.touched)).toLocaleDateString() ?? 'unknown',
73+
length: result.length,
74+
} as WikiModel);
75+
76+
return model;
77+
}
78+
}

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
99
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
1010
import {MALAPI} from './api/apis/MALAPI';
1111
import {MediaDbIdSearchModal} from './modals/MediaDbIdSearchModal';
12+
import {WikipediaAPI} from './api/apis/WikipediaAPI';
1213

1314
export default class MediaDbPlugin extends Plugin {
1415
settings: MediaDbPluginSettings;
@@ -45,6 +46,8 @@ export default class MediaDbPlugin extends Plugin {
4546
this.apiManager.registerAPI(new TestAPI());
4647
this.apiManager.registerAPI(new OMDbAPI(this));
4748
this.apiManager.registerAPI(new MALAPI(this));
49+
this.apiManager.registerAPI(new WikipediaAPI(this));
50+
// this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
4851
}
4952

5053
async createMediaDbNote(modal: () => Promise<MediaTypeModel>): Promise<void> {

src/models/WikiModel.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {MediaTypeModel} from './MediaTypeModel';
2+
import {stringifyYaml} from 'obsidian';
3+
4+
5+
export class WikiModel extends MediaTypeModel {
6+
type: string;
7+
title: string;
8+
englishTitle: string;
9+
year: string;
10+
dataSource: string;
11+
url: string;
12+
id: string;
13+
14+
wikiUrl: string;
15+
lastUpdated: string;
16+
length: number;
17+
18+
19+
constructor(obj: any = {}) {
20+
super();
21+
22+
Object.assign(this, obj);
23+
}
24+
25+
toMetaData(): string {
26+
return stringifyYaml(this);
27+
}
28+
29+
getFileName(): string {
30+
return this.title;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)