Music releases
This commit is contained in:
parent
47b69b5be7
commit
003c568099
12 changed files with 178 additions and 36 deletions
|
|
@ -2,6 +2,7 @@ import {APIModel} from '../APIModel';
|
|||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
import MediaDbPlugin from '../../main';
|
||||
|
||||
// WIP
|
||||
export class LocGovAPI extends APIModel {
|
||||
plugin: MediaDbPlugin;
|
||||
typeMappings: Map<string, string>;
|
||||
|
|
@ -21,7 +22,7 @@ export class LocGovAPI extends APIModel {
|
|||
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 searchUrl = `https://www.loc.gov/search/?q=${encodeURIComponent(title)}&fo=json&c=20`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
console.log(fetchData);
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ export class MALAPI extends APIModel {
|
|||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
const searchUrl = `https://api.jikan.moe/v4/anime?q=${title}&limit=20${this.plugin.settings.sfwFilter ? '&sfw' : ''}`;
|
||||
const searchUrl = `https://api.jikan.moe/v4/anime?q=${encodeURIComponent(title)}&limit=20${this.plugin.settings.sfwFilter ? '&sfw' : ''}`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
console.log(fetchData);
|
||||
|
|
|
|||
98
src/api/apis/MusicBrainzAPI.ts
Normal file
98
src/api/apis/MusicBrainzAPI.ts
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import {APIModel} from '../APIModel';
|
||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
import MediaDbPlugin from '../../main';
|
||||
import {requestUrl} from 'obsidian';
|
||||
import {MusicReleaseModel} from '../../models/MusicReleaseModel';
|
||||
// import {MusicBrainzApi} from 'musicbrainz-api';
|
||||
|
||||
// WIP
|
||||
export class MusicBrainzAPI extends APIModel {
|
||||
plugin: MediaDbPlugin;
|
||||
|
||||
constructor(plugin: MediaDbPlugin) {
|
||||
super();
|
||||
|
||||
this.plugin = plugin;
|
||||
this.apiName = 'MusicBrainz API';
|
||||
this.apiDescription = 'Free API for music albums.';
|
||||
this.apiUrl = 'https://musicbrainz.org/';
|
||||
this.types = ['music'];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
const searchUrl = `https://musicbrainz.org/ws/2/release-group?query=${encodeURIComponent(title)}&limit=20&fmt=json`;
|
||||
|
||||
const fetchData = await requestUrl({
|
||||
url: searchUrl,
|
||||
headers: {
|
||||
'User-Agent': 'obsidian-media-db-plugin/0.1.7 ( m.projects.code@gmail.com )',
|
||||
},
|
||||
});
|
||||
|
||||
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['release-groups']) {
|
||||
ret.push(new MusicReleaseModel({
|
||||
type: 'musicRelease',
|
||||
title: result.title,
|
||||
englishTitle: result.title,
|
||||
year: (new Date(result['first-release-date'])).getFullYear().toString(),
|
||||
dataSource: this.apiName,
|
||||
url: '',
|
||||
id: result.id,
|
||||
|
||||
artists: result['artist-credit'].map((a: any) => a.name),
|
||||
subType: result['primary-type'],
|
||||
} as MusicReleaseModel));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(item.id)}?inc=releases+artists+tags+ratings+genres&fmt=json`;
|
||||
|
||||
const fetchData = await requestUrl({
|
||||
url: searchUrl,
|
||||
headers: {
|
||||
'User-Agent': 'MyAwesomeTagger/1.2.0 ( me@example.com )',
|
||||
},
|
||||
});
|
||||
|
||||
const data = await fetchData.json;
|
||||
|
||||
console.log(data);
|
||||
|
||||
const result = data;
|
||||
|
||||
const model = new MusicReleaseModel({
|
||||
type: 'musicRelease',
|
||||
title: result.title,
|
||||
englishTitle: result.title,
|
||||
year: (new Date(result['first-release-date'])).getFullYear().toString(),
|
||||
dataSource: this.apiName,
|
||||
url: '',
|
||||
id: result.id,
|
||||
|
||||
artists: result['artist-credit'].map((a: any) => a.name),
|
||||
genres: result.genres.map((g: any) => g.name),
|
||||
subType: result['primary-type'],
|
||||
rating: result.rating.value * 2,
|
||||
} as MusicReleaseModel);
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ export class OMDbAPI extends APIModel {
|
|||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
const searchUrl = `http://www.omdbapi.com/?s=${title}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
const searchUrl = `http://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
import {APIModel} from '../APIModel';
|
||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
import {MovieModel} from '../../models/MovieModel';
|
||||
|
||||
export class TestAPI extends APIModel {
|
||||
constructor() {
|
||||
super();
|
||||
this.apiName = 'testAPI';
|
||||
this.apiDescription = 'The test API used during development.';
|
||||
this.apiUrl = 'www.test.api';
|
||||
this.types = ['test'];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
return [
|
||||
new MovieModel({title: 'test_1', type: this.types[0], dataSource: this.apiName}),
|
||||
new MovieModel({title: 'test_2', type: this.types[0], dataSource: this.apiName, producer: 'Max Musterman'}),
|
||||
];
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ export class WikipediaAPI extends APIModel {
|
|||
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 searchUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(title)}&srlimit=20&utf8=&format=json&origin=*`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
console.log(fetchData);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue