it creates files now
This commit is contained in:
parent
c67a0bf590
commit
4405eb4a34
7 changed files with 61 additions and 19 deletions
|
|
@ -15,7 +15,7 @@ export class APIManager {
|
|||
|
||||
for (const api of this.apis) {
|
||||
if (types.length === 0 || api.hasTypeOverlap(types)) {
|
||||
const apiRes = await api.getByTitle(query);
|
||||
const apiRes = await api.searchByTitle(query);
|
||||
// console.log(apiRes);
|
||||
res = res.concat(apiRes);
|
||||
}
|
||||
|
|
@ -24,6 +24,14 @@ export class APIManager {
|
|||
return res;
|
||||
}
|
||||
|
||||
async queryDetailedInfo(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
for (const api of this.apis) {
|
||||
if (api.apiName === item.dataSource) {
|
||||
return api.getById(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registerAPI(api: APIModel): void {
|
||||
this.apis.push(api);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,9 @@ export abstract class APIModel {
|
|||
*
|
||||
* @param title the title to query for
|
||||
*/
|
||||
abstract getByTitle(title: string): Promise<MediaTypeModel[]>;
|
||||
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>;
|
||||
|
||||
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>;
|
||||
|
||||
hasType(type: string): boolean {
|
||||
return this.types.contains(type);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import {APIModel} from '../APIModel';
|
||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
import {MovieModel} from '../../models/MovieModel';
|
||||
|
||||
export class TestAPI extends APIModel {
|
||||
constructor() {
|
||||
|
|
@ -9,20 +10,16 @@ export class TestAPI extends APIModel {
|
|||
this.types = ['test'];
|
||||
}
|
||||
|
||||
async getByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
return [
|
||||
{title: 'test1', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
||||
{title: 'test2', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
||||
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'}),
|
||||
];
|
||||
}
|
||||
|
||||
getMataDataFromResult(item: MediaTypeModel): string {
|
||||
if (item.dataSource !== this.apiUrl) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
23
src/main.ts
23
src/main.ts
|
|
@ -3,6 +3,8 @@ import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './sett
|
|||
import {MediaDbSearchModal} from './modals/MediaDbSearchModal';
|
||||
import {APIManager} from './api/APIManager';
|
||||
import {TestAPI} from './api/apis/TestAPI';
|
||||
import {MediaTypeModel} from './models/MediaTypeModel';
|
||||
import {getFileName} from './utils/Utils';
|
||||
|
||||
export default class MediaDbPlugin extends Plugin {
|
||||
settings: MediaDbPluginSettings;
|
||||
|
|
@ -34,9 +36,28 @@ export default class MediaDbPlugin extends Plugin {
|
|||
}
|
||||
|
||||
async createMediaDbNote(): Promise<void> {
|
||||
const data = await this.openMediaDbSearchModal();
|
||||
let data: MediaTypeModel = await this.openMediaDbSearchModal();
|
||||
console.log('MDB | Create new note or something...');
|
||||
|
||||
data = await this.apiManager.queryDetailedInfo(data);
|
||||
|
||||
console.log(data);
|
||||
|
||||
data.toMetaData()
|
||||
|
||||
const fileContent = `---\n${data.toMetaData()}\n---\n`;
|
||||
|
||||
const fileName = getFileName(data);
|
||||
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
||||
const targetFile = await this.app.vault.create(filePath, fileContent);
|
||||
|
||||
// open file
|
||||
const activeLeaf = this.app.workspace.getLeaf();
|
||||
if (!activeLeaf) {
|
||||
console.warn('No active leaf');
|
||||
return;
|
||||
}
|
||||
await activeLeaf.openFile(targetFile, { state: { mode: 'source' } });
|
||||
}
|
||||
|
||||
async openMediaDbSearchModal(): Promise<any> {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ export abstract class MediaTypeModel {
|
|||
type: string;
|
||||
title: string;
|
||||
dataSource: string;
|
||||
id: string;
|
||||
|
||||
abstract toMetaData(): string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ export class MovieModel extends MediaTypeModel {
|
|||
type: string;
|
||||
title: string;
|
||||
dataSource: string;
|
||||
id: string;
|
||||
|
||||
genres: string[];
|
||||
producer: string;
|
||||
|
|
@ -19,14 +20,16 @@ export class MovieModel extends MediaTypeModel {
|
|||
personalRating: number;
|
||||
|
||||
|
||||
constructor() {
|
||||
constructor(obj: any = {}) {
|
||||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'movie';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
return '';
|
||||
return JSON.stringify(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,17 @@
|
|||
export const wrapAround = (value: number, size: number): number => {
|
||||
return ((value % size) + size) % size;
|
||||
};
|
||||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||
|
||||
export const sleep = (ms: number) => {
|
||||
export function wrapAround(value: number, size: number): number {
|
||||
return ((value % size) + size) % size;
|
||||
}
|
||||
|
||||
export function sleep (ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
};
|
||||
}
|
||||
|
||||
export function getFileName(item: MediaTypeModel) {
|
||||
return replaceIllegalFileNameCharactersInString(item.title);
|
||||
}
|
||||
|
||||
export function replaceIllegalFileNameCharactersInString(string: string) {
|
||||
return string.replace(/[\\,#%&{}/*<>$":@.]*/g, '');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue