Skip to content

Commit 75166ec

Browse files
committed
clean up merged PRs
1 parent 6699805 commit 75166ec

File tree

5 files changed

+74
-109
lines changed

5 files changed

+74
-109
lines changed

src/api/apis/MobyGamesAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class MobyGamesAPI extends APIModel {
6161
year: new Date(result.platforms[0].first_release_date).getFullYear().toString(),
6262
dataSource: this.apiName,
6363
id: result.game_id,
64-
} as GameModel),
64+
}),
6565
);
6666
}
6767

src/api/apis/MusicBrainzAPI.ts

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import type MediaDbPlugin from '../../main';
33
import type { MediaTypeModel } from '../../models/MediaTypeModel';
44
import { MusicReleaseModel } from '../../models/MusicReleaseModel';
55
import { MediaType } from '../../utils/MediaType';
6-
import { contactEmail, extractTracksFromMedia, getLanguageName, mediaDbVersion, pluginName } from '../../utils/Utils';
6+
import { contactEmail, getLanguageName, mediaDbVersion, pluginName } from '../../utils/Utils';
77
import { APIModel } from '../APIModel';
8-
import { iso6392 } from 'iso-639-2';
98

109
// sadly no open api schema available
1110

@@ -26,6 +25,18 @@ interface Release {
2625
status: string;
2726
}
2827

28+
interface ArtistCredit {
29+
name: string;
30+
artist: {
31+
tags: Tag[];
32+
type: string;
33+
id: string;
34+
name: string;
35+
'short-name': string;
36+
country: string;
37+
};
38+
}
39+
2940
interface SearchResponse {
3041
id: string;
3142
'type-id': string;
@@ -36,14 +47,7 @@ interface SearchResponse {
3647
title: string;
3748
'first-release-date': string;
3849
'primary-type': string;
39-
'artist-credit': {
40-
name: string;
41-
artist: {
42-
id: string;
43-
name: string;
44-
'short-name': string;
45-
};
46-
}[];
50+
'artist-credit': ArtistCredit[];
4751
releases: Release[];
4852
tags: Tag[];
4953
}
@@ -52,17 +56,7 @@ interface IdResponse {
5256
id: string;
5357
tags: Tag[];
5458
'primary-type-id': string;
55-
'artist-credit': {
56-
name: string;
57-
artist: {
58-
tags: Tag[];
59-
type: string;
60-
id: string;
61-
name: string;
62-
'short-name': string;
63-
country: string;
64-
};
65-
}[];
59+
'artist-credit': ArtistCredit[];
6660
title: string;
6761
genres: Genre[];
6862
'first-release-date': string;
@@ -74,6 +68,27 @@ interface IdResponse {
7468
};
7569
}
7670

71+
interface MediaResponse {
72+
media: {
73+
'track-count': number;
74+
tracks: {
75+
'artist-credit': ArtistCredit[];
76+
length: number | null;
77+
number: string;
78+
position: number;
79+
title: string;
80+
recording: {
81+
length: number;
82+
title: string;
83+
};
84+
}[];
85+
}[];
86+
'text-representation': {
87+
language: string;
88+
script: string;
89+
};
90+
}
91+
7792
export class MusicBrainzAPI extends APIModel {
7893
plugin: MediaDbPlugin;
7994
apiDateFormat: string = 'YYYY-MM-DD';
@@ -173,9 +188,11 @@ export class MusicBrainzAPI extends APIModel {
173188
throw Error(`MDB | Received status code ${releaseResponse.status} from ${this.apiName}.`);
174189
}
175190

176-
const releaseData = await releaseResponse.json;
191+
const releaseData = (await releaseResponse.json) as MediaResponse;
177192
const tracks = extractTracksFromMedia(releaseData.media);
178193

194+
console.log(releaseData);
195+
179196
return new MusicReleaseModel({
180197
type: 'musicRelease',
181198
title: result.title,
@@ -204,3 +221,32 @@ export class MusicBrainzAPI extends APIModel {
204221
return this.plugin.settings.MusicBrainzAPI_disabledMediaTypes;
205222
}
206223
}
224+
225+
function extractTracksFromMedia(media: MediaResponse['media']): {
226+
number: number;
227+
title: string;
228+
duration: string;
229+
featuredArtists: string[];
230+
}[] {
231+
if (!media || media.length === 0 || !media[0].tracks) return [];
232+
233+
return media[0].tracks.map((track, index) => {
234+
const title = track.title ?? track.recording?.title ?? 'Unknown Title';
235+
const rawLength = track.length ?? track.recording?.length;
236+
const duration = rawLength ? millisecondsToMinutes(rawLength) : 'unknown';
237+
const featuredArtists = track['artist-credit']?.map(ac => ac.name) ?? [];
238+
239+
return {
240+
number: index + 1,
241+
title,
242+
duration,
243+
featuredArtists,
244+
};
245+
});
246+
}
247+
248+
function millisecondsToMinutes(milliseconds: number): string {
249+
const minutes = Math.floor(milliseconds / 60000);
250+
const seconds = Math.floor((milliseconds % 60000) / 1000);
251+
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
252+
}

src/main.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder } from 'obsidian';
2-
import { requestUrl, normalizePath } from 'obsidian'; // Add requestUrl import
2+
import { requestUrl, normalizePath } from 'obsidian';
33
import type { MediaType } from 'src/utils/MediaType';
44
import { APIManager } from './api/APIManager';
55
import { BoardGameGeekAPI } from './api/apis/BoardGameGeekAPI';
@@ -59,7 +59,6 @@ export default class MediaDbPlugin extends Plugin {
5959
this.apiManager.registerAPI(new ComicVineAPI(this));
6060
this.apiManager.registerAPI(new MobyGamesAPI(this));
6161
this.apiManager.registerAPI(new GiantBombAPI(this));
62-
// this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
6362

6463
this.mediaTypeManager = new MediaTypeManager();
6564
this.modelPropertyMapper = new PropertyMapper(this);
@@ -157,12 +156,6 @@ export default class MediaDbPlugin extends Plugin {
157156
});
158157
}
159158

160-
/**
161-
* first very simple approach
162-
* TODO:
163-
* - replace the detail query
164-
* - maybe custom link syntax
165-
*/
166159
async createLinkWithSearchModal(): Promise<void> {
167160
const apiSearchResults = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
168161
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis);
@@ -381,18 +374,7 @@ export default class MediaDbPlugin extends Plugin {
381374
* @param options
382375
*/
383376
async generateMediaDbNoteContents(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions): Promise<string> {
384-
const template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
385-
386-
return this.generateContentWithDefaultFrontMatter(mediaTypeModel, options, template);
387-
388-
// if (this.settings.useDefaultFrontMatter || !template) {
389-
// return this.generateContentWithDefaultFrontMatter(mediaTypeModel, options, template);
390-
// } else {
391-
// return this.generateContentWithCustomFrontMatter(mediaTypeModel, options, template);
392-
// }
393-
}
394-
395-
async generateContentWithDefaultFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template?: string): Promise<string> {
377+
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
396378
let fileMetadata: Record<string, unknown>;
397379

398380
if (this.settings.useDefaultFrontMatter) {
@@ -421,48 +403,6 @@ export default class MediaDbPlugin extends Plugin {
421403
return fileContent;
422404
}
423405

424-
async generateContentWithCustomFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template: string): Promise<string> {
425-
const regExp = new RegExp(this.frontMatterRexExpPattern);
426-
427-
const frontMatter = this.getMetaDataFromFileContent(template);
428-
let fileContent: string = template.replace(regExp, '');
429-
430-
// Updating a previous file
431-
if (options.attachFile) {
432-
const previousMetadata = this.app.metadataCache.getFileCache(options.attachFile)?.frontmatter ?? {};
433-
434-
// Use contents (below front matter) from previous file
435-
fileContent = await this.app.vault.read(options.attachFile);
436-
437-
fileContent = fileContent.replace(regExp, '');
438-
fileContent = fileContent.startsWith('\n') ? fileContent.substring(1) : fileContent;
439-
440-
// Update updated front matter with entries from the old front matter, if it isn't defined in the new front matter
441-
Object.keys(previousMetadata).forEach(key => {
442-
const value: unknown = previousMetadata[key];
443-
444-
if (!frontMatter[key] && value) {
445-
frontMatter[key] = value;
446-
}
447-
});
448-
}
449-
450-
// Ensure that id, type, and dataSource are defined
451-
frontMatter.id ??= mediaTypeModel.id;
452-
frontMatter.type ??= mediaTypeModel.type;
453-
frontMatter.dataSource ??= mediaTypeModel.dataSource;
454-
455-
if (this.settings.enableTemplaterIntegration && hasTemplaterPlugin(this.app)) {
456-
// Only support stringifyYaml for templater plugin
457-
// Include the media variable in all templater commands by using a top level JavaScript execution command.
458-
fileContent = `---\n<%* const media = ${JSON.stringify(mediaTypeModel)} %>\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
459-
} else {
460-
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
461-
}
462-
463-
return fileContent;
464-
}
465-
466406
async attachFile(fileMetadata: Metadata, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: Metadata; fileContent: string }> {
467407
if (!fileToAttach) {
468408
return { fileMetadata: fileMetadata, fileContent: fileContent };

src/models/MusicReleaseModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MediaType } from '../utils/MediaType';
22
import type { ModelToData } from '../utils/Utils';
3-
import { mediaDbTag, migrateObject, getLanguageName } from '../utils/Utils';
3+
import { mediaDbTag, migrateObject } from '../utils/Utils';
44
import { MediaTypeModel } from './MediaTypeModel';
55

66
export type MusicReleaseData = ModelToData<MusicReleaseModel>;

src/utils/Utils.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { iso6392 } from 'iso-639-2';
12
import type { TFile, TFolder, App } from 'obsidian';
23
import { requestUrl } from 'obsidian';
34
import type { MediaTypeModel } from '../models/MediaTypeModel';
4-
import { iso6392 } from 'iso-639-2';
55

66
export const pluginName: string = 'obsidian-media-db-plugin';
77
export const contactEmail: string = '[email protected]';
@@ -297,28 +297,7 @@ export async function obsidianFetch(input: Request): Promise<Response> {
297297
text: async () => res.text,
298298
} as Response;
299299
}
300-
export function extractTracksFromMedia(media: any[]): {
301-
number: number;
302-
title: string;
303-
duration: string;
304-
featuredArtists: string[];
305-
}[] {
306-
if (!media || media.length === 0 || !media[0].tracks) return [];
307-
308-
return media[0].tracks.map((track: any, index: number) => {
309-
const title = track.title || track.recording?.title || 'Unknown Title';
310-
const rawLength = track.length || track.recording?.length;
311-
const duration = rawLength ? new Date(rawLength).toISOString().substr(14, 5) : 'unknown';
312-
const featuredArtists = track['artist-credit']?.map((ac: { name: string }) => ac.name) ?? [];
313-
314-
return {
315-
number: index + 1,
316-
title,
317-
duration,
318-
featuredArtists,
319-
};
320-
});
321-
}
300+
322301
export function getLanguageName(code: string): string | null {
323302
const language = iso6392.find(lang => lang.iso6392B === code || lang.iso6392T === code);
324303

0 commit comments

Comments
 (0)