generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mProjectsCode/master
merge master into release for 0.1.8
- Loading branch information
Showing
21 changed files
with
479 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {APIModel} from '../APIModel'; | ||
import {MediaTypeModel} from '../../models/MediaTypeModel'; | ||
import MediaDbPlugin from '../../main'; | ||
import {debugLog} from '../../utils/Utils'; | ||
|
||
// WIP | ||
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 by Title`); | ||
|
||
const searchUrl = `https://www.loc.gov/search/?q=${encodeURIComponent(title)}&fo=json&c=20`; | ||
const fetchData = await fetch(searchUrl); | ||
debugLog(fetchData); | ||
|
||
if (fetchData.status !== 200) { | ||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`); | ||
} | ||
|
||
const data = await fetchData.json(); | ||
debugLog(data); | ||
let ret: MediaTypeModel[] = []; | ||
|
||
throw new Error('MDB | Under construction, API implementation not finished'); | ||
|
||
// return ret; | ||
} | ||
|
||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> { | ||
console.log(`MDB | api "${this.apiName}" queried by ID`); | ||
|
||
const searchUrl = `https://www.loc.gov/item/${item.id}/?fo=json`; | ||
const fetchData = await fetch(searchUrl); | ||
if (fetchData.status !== 200) { | ||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`); | ||
} | ||
|
||
const data = await fetchData.json(); | ||
debugLog(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.`); | ||
} | ||
|
||
throw new Error('MDB | Under construction, API implementation not finished'); | ||
|
||
// return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {APIModel} from '../APIModel'; | ||
import {MediaTypeModel} from '../../models/MediaTypeModel'; | ||
import MediaDbPlugin from '../../main'; | ||
import {requestUrl} from 'obsidian'; | ||
import {MusicReleaseModel} from '../../models/MusicReleaseModel'; | ||
import {contactEmail, debugLog, mediaDbVersion, pluginName} from '../../utils/Utils'; | ||
|
||
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 by Title`); | ||
|
||
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': `${pluginName}/${mediaDbVersion} (${contactEmail})`, | ||
}, | ||
}); | ||
|
||
debugLog(fetchData); | ||
|
||
if (fetchData.status !== 200) { | ||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`); | ||
} | ||
|
||
const data = await fetchData.json; | ||
debugLog(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 by ID`); | ||
|
||
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': `${pluginName}/0.1.7 (${contactEmail})`, | ||
}, | ||
}); | ||
|
||
if (fetchData.status !== 200) { | ||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`); | ||
} | ||
|
||
const data = await fetchData.json; | ||
debugLog(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; | ||
} | ||
} |
Oops, something went wrong.