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) {
|
for (const api of this.apis) {
|
||||||
if (types.length === 0 || api.hasTypeOverlap(types)) {
|
if (types.length === 0 || api.hasTypeOverlap(types)) {
|
||||||
const apiRes = await api.getByTitle(query);
|
const apiRes = await api.searchByTitle(query);
|
||||||
// console.log(apiRes);
|
// console.log(apiRes);
|
||||||
res = res.concat(apiRes);
|
res = res.concat(apiRes);
|
||||||
}
|
}
|
||||||
|
|
@ -24,6 +24,14 @@ export class APIManager {
|
||||||
return res;
|
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 {
|
registerAPI(api: APIModel): void {
|
||||||
this.apis.push(api);
|
this.apis.push(api);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,9 @@ export abstract class APIModel {
|
||||||
*
|
*
|
||||||
* @param title the title to query for
|
* @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 {
|
hasType(type: string): boolean {
|
||||||
return this.types.contains(type);
|
return this.types.contains(type);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import {APIModel} from '../APIModel';
|
import {APIModel} from '../APIModel';
|
||||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||||
|
import {MovieModel} from '../../models/MovieModel';
|
||||||
|
|
||||||
export class TestAPI extends APIModel {
|
export class TestAPI extends APIModel {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
@ -9,20 +10,16 @@ export class TestAPI extends APIModel {
|
||||||
this.types = ['test'];
|
this.types = ['test'];
|
||||||
}
|
}
|
||||||
|
|
||||||
async getByTitle(title: string): Promise<MediaTypeModel[]> {
|
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||||
console.log(`MDB | api "${this.apiName}" queried`);
|
console.log(`MDB | api "${this.apiName}" queried`);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{title: 'test1', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
new MovieModel ({title: 'test_1', type: this.types[0], dataSource: this.apiName}) ,
|
||||||
{title: 'test2', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
new MovieModel ({title: 'test_2', type: this.types[0], dataSource: this.apiName, producer: 'Max Musterman'}),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
getMataDataFromResult(item: MediaTypeModel): string {
|
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||||
if (item.dataSource !== this.apiUrl) {
|
return item;
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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 {MediaDbSearchModal} from './modals/MediaDbSearchModal';
|
||||||
import {APIManager} from './api/APIManager';
|
import {APIManager} from './api/APIManager';
|
||||||
import {TestAPI} from './api/apis/TestAPI';
|
import {TestAPI} from './api/apis/TestAPI';
|
||||||
|
import {MediaTypeModel} from './models/MediaTypeModel';
|
||||||
|
import {getFileName} from './utils/Utils';
|
||||||
|
|
||||||
export default class MediaDbPlugin extends Plugin {
|
export default class MediaDbPlugin extends Plugin {
|
||||||
settings: MediaDbPluginSettings;
|
settings: MediaDbPluginSettings;
|
||||||
|
|
@ -34,9 +36,28 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createMediaDbNote(): Promise<void> {
|
async createMediaDbNote(): Promise<void> {
|
||||||
const data = await this.openMediaDbSearchModal();
|
let data: MediaTypeModel = await this.openMediaDbSearchModal();
|
||||||
console.log('MDB | Create new note or something...');
|
console.log('MDB | Create new note or something...');
|
||||||
|
|
||||||
|
data = await this.apiManager.queryDetailedInfo(data);
|
||||||
|
|
||||||
console.log(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> {
|
async openMediaDbSearchModal(): Promise<any> {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ export abstract class MediaTypeModel {
|
||||||
type: string;
|
type: string;
|
||||||
title: string;
|
title: string;
|
||||||
dataSource: string;
|
dataSource: string;
|
||||||
|
id: string;
|
||||||
|
|
||||||
abstract toMetaData(): string;
|
abstract toMetaData(): string;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ export class MovieModel extends MediaTypeModel {
|
||||||
type: string;
|
type: string;
|
||||||
title: string;
|
title: string;
|
||||||
dataSource: string;
|
dataSource: string;
|
||||||
|
id: string;
|
||||||
|
|
||||||
genres: string[];
|
genres: string[];
|
||||||
producer: string;
|
producer: string;
|
||||||
|
|
@ -19,14 +20,16 @@ export class MovieModel extends MediaTypeModel {
|
||||||
personalRating: number;
|
personalRating: number;
|
||||||
|
|
||||||
|
|
||||||
constructor() {
|
constructor(obj: any = {}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
Object.assign(this, obj);
|
||||||
|
|
||||||
this.type = 'movie';
|
this.type = 'movie';
|
||||||
}
|
}
|
||||||
|
|
||||||
toMetaData(): string {
|
toMetaData(): string {
|
||||||
return '';
|
return JSON.stringify(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,17 @@
|
||||||
export const wrapAround = (value: number, size: number): number => {
|
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||||
return ((value % size) + size) % size;
|
|
||||||
};
|
|
||||||
|
|
||||||
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));
|
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