|
| 1 | +import {APIModel} from '../APIModel'; |
| 2 | +import {MediaTypeModel} from '../../models/MediaTypeModel'; |
| 3 | +import MediaDbPlugin from '../../main'; |
| 4 | +import {requestUrl} from 'obsidian'; |
| 5 | +import {MusicReleaseModel} from '../../models/MusicReleaseModel'; |
| 6 | +// import {MusicBrainzApi} from 'musicbrainz-api'; |
| 7 | + |
| 8 | +// WIP |
| 9 | +export class MusicBrainzAPI extends APIModel { |
| 10 | + plugin: MediaDbPlugin; |
| 11 | + |
| 12 | + constructor(plugin: MediaDbPlugin) { |
| 13 | + super(); |
| 14 | + |
| 15 | + this.plugin = plugin; |
| 16 | + this.apiName = 'MusicBrainz API'; |
| 17 | + this.apiDescription = 'Free API for music albums.'; |
| 18 | + this.apiUrl = 'https://musicbrainz.org/'; |
| 19 | + this.types = ['music']; |
| 20 | + } |
| 21 | + |
| 22 | + async searchByTitle(title: string): Promise<MediaTypeModel[]> { |
| 23 | + console.log(`MDB | api "${this.apiName}" queried`); |
| 24 | + |
| 25 | + const searchUrl = `https://musicbrainz.org/ws/2/release-group?query=${encodeURIComponent(title)}&limit=20&fmt=json`; |
| 26 | + |
| 27 | + const fetchData = await requestUrl({ |
| 28 | + url: searchUrl, |
| 29 | + headers: { |
| 30 | + 'User-Agent': 'obsidian-media-db-plugin/0.1.7 ( [email protected] )', |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + console.log(fetchData); |
| 35 | + |
| 36 | + if (fetchData.status !== 200) { |
| 37 | + throw Error(`Received status code ${fetchData.status} from an API.`); |
| 38 | + } |
| 39 | + const data = await fetchData.json; |
| 40 | + |
| 41 | + console.log(data); |
| 42 | + |
| 43 | + let ret: MediaTypeModel[] = []; |
| 44 | + |
| 45 | + for (const result of data['release-groups']) { |
| 46 | + ret.push(new MusicReleaseModel({ |
| 47 | + type: 'musicRelease', |
| 48 | + title: result.title, |
| 49 | + englishTitle: result.title, |
| 50 | + year: (new Date(result['first-release-date'])).getFullYear().toString(), |
| 51 | + dataSource: this.apiName, |
| 52 | + url: '', |
| 53 | + id: result.id, |
| 54 | + |
| 55 | + artists: result['artist-credit'].map((a: any) => a.name), |
| 56 | + subType: result['primary-type'], |
| 57 | + } as MusicReleaseModel)); |
| 58 | + } |
| 59 | + |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + async getById(item: MediaTypeModel): Promise<MediaTypeModel> { |
| 64 | + console.log(`MDB | api "${this.apiName}" queried`); |
| 65 | + |
| 66 | + const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(item.id)}?inc=releases+artists+tags+ratings+genres&fmt=json`; |
| 67 | + |
| 68 | + const fetchData = await requestUrl({ |
| 69 | + url: searchUrl, |
| 70 | + headers: { |
| 71 | + 'User-Agent': 'MyAwesomeTagger/1.2.0 ( [email protected] )', |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const data = await fetchData.json; |
| 76 | + |
| 77 | + console.log(data); |
| 78 | + |
| 79 | + const result = data; |
| 80 | + |
| 81 | + const model = new MusicReleaseModel({ |
| 82 | + type: 'musicRelease', |
| 83 | + title: result.title, |
| 84 | + englishTitle: result.title, |
| 85 | + year: (new Date(result['first-release-date'])).getFullYear().toString(), |
| 86 | + dataSource: this.apiName, |
| 87 | + url: '', |
| 88 | + id: result.id, |
| 89 | + |
| 90 | + artists: result['artist-credit'].map((a: any) => a.name), |
| 91 | + genres: result.genres.map((g: any) => g.name), |
| 92 | + subType: result['primary-type'], |
| 93 | + rating: result.rating.value * 2, |
| 94 | + } as MusicReleaseModel); |
| 95 | + |
| 96 | + return model; |
| 97 | + } |
| 98 | +} |
0 commit comments