parent
0db760b84e
commit
bb25caf6bc
3 changed files with 108 additions and 35 deletions
|
|
@ -41,13 +41,20 @@ export class MALAPI extends APIModel {
|
||||||
let ret: MediaTypeModel[] = [];
|
let ret: MediaTypeModel[] = [];
|
||||||
|
|
||||||
for (const result of data.data) {
|
for (const result of data.data) {
|
||||||
const type = this.typeMappings.get(result.type.toLowerCase());
|
const type = this.typeMappings.get(result.type?.toLowerCase());
|
||||||
if (type === undefined) {
|
if (type === undefined) {
|
||||||
continue;
|
ret.push(new MovieModel({
|
||||||
|
subType: '',
|
||||||
|
title: result.title,
|
||||||
|
englishTitle: result.title_english ?? result.title,
|
||||||
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
dataSource: this.apiName,
|
||||||
|
id: result.mal_id,
|
||||||
|
} as MovieModel));
|
||||||
}
|
}
|
||||||
if (type === 'movie' || type === 'special') {
|
if (type === 'movie' || type === 'special') {
|
||||||
ret.push(new MovieModel({
|
ret.push(new MovieModel({
|
||||||
type: type,
|
subType: type,
|
||||||
title: result.title,
|
title: result.title,
|
||||||
englishTitle: result.title_english ?? result.title,
|
englishTitle: result.title_english ?? result.title,
|
||||||
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
|
@ -56,7 +63,7 @@ export class MALAPI extends APIModel {
|
||||||
} as MovieModel));
|
} as MovieModel));
|
||||||
} else if (type === 'series' || type === 'ova') {
|
} else if (type === 'series' || type === 'ova') {
|
||||||
ret.push(new SeriesModel({
|
ret.push(new SeriesModel({
|
||||||
type: type,
|
subType: type,
|
||||||
title: result.title,
|
title: result.title,
|
||||||
englishTitle: result.title_english ?? result.title,
|
englishTitle: result.title_english ?? result.title,
|
||||||
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
|
@ -83,14 +90,39 @@ export class MALAPI extends APIModel {
|
||||||
debugLog(data);
|
debugLog(data);
|
||||||
const result = data.data;
|
const result = data.data;
|
||||||
|
|
||||||
const type = this.typeMappings.get(result.type.toLowerCase());
|
const type = this.typeMappings.get(result.type?.toLowerCase());
|
||||||
if (type === undefined) {
|
if (type === undefined) {
|
||||||
throw Error(`${result.type.toLowerCase()} is an unsupported type.`);
|
const model = new MovieModel({
|
||||||
|
subType: '',
|
||||||
|
title: result.title,
|
||||||
|
englishTitle: result.title_english ?? result.title,
|
||||||
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
dataSource: this.apiName,
|
||||||
|
url: result.url,
|
||||||
|
id: result.mal_id,
|
||||||
|
|
||||||
|
genres: result.genres?.map((x: any) => x.name) ?? [],
|
||||||
|
producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown',
|
||||||
|
duration: result.duration ?? 'unknown',
|
||||||
|
onlineRating: result.score ?? 0,
|
||||||
|
image: result.images?.jpg?.image_url ?? '',
|
||||||
|
|
||||||
|
released: true,
|
||||||
|
premiere: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown',
|
||||||
|
|
||||||
|
userData: {
|
||||||
|
watched: false,
|
||||||
|
lastWatched: '',
|
||||||
|
personalRating: 0,
|
||||||
|
},
|
||||||
|
} as MovieModel);
|
||||||
|
|
||||||
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'movie' || type === 'special') {
|
if (type === 'movie' || type === 'special') {
|
||||||
const model = new MovieModel({
|
const model = new MovieModel({
|
||||||
type: type,
|
subType: type,
|
||||||
title: result.title,
|
title: result.title,
|
||||||
englishTitle: result.title_english ?? result.title,
|
englishTitle: result.title_english ?? result.title,
|
||||||
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
|
@ -117,7 +149,7 @@ export class MALAPI extends APIModel {
|
||||||
return model;
|
return model;
|
||||||
} else if (type === 'series' || type === 'ova') {
|
} else if (type === 'series' || type === 'ova') {
|
||||||
const model = new SeriesModel({
|
const model = new SeriesModel({
|
||||||
type: type,
|
subType: type,
|
||||||
title: result.title,
|
title: result.title,
|
||||||
englishTitle: result.title_english ?? result.title,
|
englishTitle: result.title_english ?? result.title,
|
||||||
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
|
|
||||||
93
src/main.ts
93
src/main.ts
|
|
@ -1,4 +1,4 @@
|
||||||
import {Notice, Plugin, stringifyYaml, TFile, TFolder} from 'obsidian';
|
import {Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder} from 'obsidian';
|
||||||
import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings';
|
import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings';
|
||||||
import {APIManager} from './api/APIManager';
|
import {APIManager} from './api/APIManager';
|
||||||
import {MediaTypeModel} from './models/MediaTypeModel';
|
import {MediaTypeModel} from './models/MediaTypeModel';
|
||||||
|
|
@ -109,33 +109,13 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
console.log('MDB | Creating new note...');
|
console.log('MDB | Creating new note...');
|
||||||
// console.log(mediaTypeModel);
|
// console.log(mediaTypeModel);
|
||||||
|
|
||||||
let metadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
|
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
|
||||||
if (attachFile) {
|
let fileContent = '';
|
||||||
let attachFileMetadata: any = this.app.metadataCache.getFileCache(attachFile).frontmatter;
|
|
||||||
if (attachFileMetadata) {
|
|
||||||
attachFileMetadata = JSON.parse(JSON.stringify(attachFileMetadata)); // deep copy
|
|
||||||
delete attachFileMetadata.position;
|
|
||||||
} else {
|
|
||||||
attachFileMetadata = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata = Object.assign(attachFileMetadata, metadata);
|
({fileMetadata, fileContent} = await this.attachFile(fileMetadata, fileContent, attachFile));
|
||||||
}
|
({fileMetadata, fileContent} = await this.attachTemplate(fileMetadata, fileContent, await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app)));
|
||||||
|
|
||||||
debugLog(metadata);
|
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
|
||||||
|
|
||||||
let fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(metadata) : stringifyYaml(metadata)}---\n`;
|
|
||||||
|
|
||||||
if (this.settings.templates) {
|
|
||||||
fileContent += await this.mediaTypeManager.getContent(mediaTypeModel, this.app);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (attachFile) {
|
|
||||||
let attachFileContent: string = await this.app.vault.read(attachFile);
|
|
||||||
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
|
|
||||||
attachFileContent = attachFileContent.replace(regExp, '');
|
|
||||||
fileContent += '\n\n' + attachFileContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent);
|
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -144,6 +124,67 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any, fileContent: string }> {
|
||||||
|
if (!fileToAttach) {
|
||||||
|
return {fileMetadata: fileMetadata, fileContent: fileContent};
|
||||||
|
}
|
||||||
|
|
||||||
|
let attachFileMetadata: any = this.app.metadataCache.getFileCache(fileToAttach).frontmatter;
|
||||||
|
if (attachFileMetadata) {
|
||||||
|
attachFileMetadata = JSON.parse(JSON.stringify(attachFileMetadata)); // deep copy
|
||||||
|
delete attachFileMetadata.position;
|
||||||
|
} else {
|
||||||
|
attachFileMetadata = {};
|
||||||
|
}
|
||||||
|
fileMetadata = Object.assign(attachFileMetadata, fileMetadata);
|
||||||
|
|
||||||
|
let attachFileContent: string = await this.app.vault.read(fileToAttach);
|
||||||
|
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
|
||||||
|
attachFileContent = attachFileContent.replace(regExp, '');
|
||||||
|
fileContent += '\n' + attachFileContent;
|
||||||
|
|
||||||
|
return {fileMetadata: fileMetadata, fileContent: fileContent};
|
||||||
|
}
|
||||||
|
|
||||||
|
async attachTemplate(fileMetadata: any, fileContent: string, template: string): Promise<{ fileMetadata: any, fileContent: string }> {
|
||||||
|
if (!template) {
|
||||||
|
return {fileMetadata: fileMetadata, fileContent: fileContent};
|
||||||
|
}
|
||||||
|
|
||||||
|
let templateMetadata: any = this.getMetaDataFromFileContent(template);
|
||||||
|
fileMetadata = Object.assign(templateMetadata, fileMetadata);
|
||||||
|
|
||||||
|
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
|
||||||
|
const attachFileContent = template.replace(regExp, '');
|
||||||
|
fileContent += '\n' + attachFileContent;
|
||||||
|
|
||||||
|
return {fileMetadata: fileMetadata, fileContent: fileContent};
|
||||||
|
}
|
||||||
|
|
||||||
|
getMetaDataFromFileContent(fileContent: string): any {
|
||||||
|
let metadata: any;
|
||||||
|
|
||||||
|
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
|
||||||
|
const frontMatterRegExpResult = regExp.exec(fileContent);
|
||||||
|
if (!frontMatterRegExpResult) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
let frontMatter = frontMatterRegExpResult[0];
|
||||||
|
if (!frontMatter) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
frontMatter = frontMatter.substring(4);
|
||||||
|
frontMatter = frontMatter.substring(0, frontMatter.length - 3);
|
||||||
|
|
||||||
|
metadata = parseYaml(frontMatter);
|
||||||
|
|
||||||
|
if (!metadata) {
|
||||||
|
metadata = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
async createNote(fileName: string, fileContent: string, openFile: boolean = false) {
|
async createNote(fileName: string, fileContent: string, openFile: boolean = false) {
|
||||||
fileName = replaceIllegalFileNameCharactersInString(fileName);
|
fileName = replaceIllegalFileNameCharactersInString(fileName);
|
||||||
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ export class MediaTypeManager {
|
||||||
return replaceTags(this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType()), mediaTypeModel);
|
return replaceTags(this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType()), mediaTypeModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getContent(mediaTypeModel: MediaTypeModel, app: App) {
|
async getTemplate(mediaTypeModel: MediaTypeModel, app: App) {
|
||||||
const templateFileName = this.mediaTemplateMap.get(mediaTypeModel.getMediaType());
|
const templateFileName = this.mediaTemplateMap.get(mediaTypeModel.getMediaType());
|
||||||
|
|
||||||
if (!templateFileName) {
|
if (!templateFileName) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue