Skip to content

Commit

Permalink
adding templates
Browse files Browse the repository at this point in the history
- added the ability to use a templates for the note creation
  • Loading branch information
Superschnizel committed Nov 1, 2023
1 parent ea4f15f commit d093c97
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 14 deletions.
148 changes: 136 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App, Editor, MarkdownView, Modal, Menu, Notice, Plugin, PluginSettingTab, Setting, requestUrl, normalizePath, RequestUrlResponse } from 'obsidian';
import { App, Editor, MarkdownView, Modal, Menu, Notice, Plugin, PluginSettingTab, Setting, requestUrl, normalizePath, RequestUrlResponse, TFile } from 'obsidian';

import {MoviegrabberSettings, DEFAULT_SETTINGS} from "./src/MoviegrabberSettings"
import {MoviegrabberSettings, DEFAULT_SETTINGS, DEFAULT_TEMPLATE} from "./src/MoviegrabberSettings"
import {MoviegrabberSearchModal} from "./src/MoviegrabberSearchModal"
import {MovieData, MovieSearch, MovieSearchItem, TEST_SEARCH} from "./src/MoviegrabberSearchObject"
import { MoviegrabberSelectionModal } from 'src/MoviegrabberSelectionModal';
Expand Down Expand Up @@ -59,7 +59,13 @@ export default class Moviegrabber extends Plugin {
}

// search the OMDb and oben selection Modal if some are found
async searchOmdb(title : string, type : 'movie' | 'series') {
async searchOmdb(title : string, type : 'movie' | 'series', depth : number=0) {
// cancel recursive retries if depth is too low
if (depth >= 4) {
this.SendWarningNotice(`Stopping after ${depth +1 } tries.`)
return;
}

// build request URL
var url = new URL("http://www.omdbapi.com");

Expand All @@ -72,8 +78,8 @@ export default class Moviegrabber extends Plugin {
try {
response = await requestUrl(url.toString());
} catch (error) {
var n = new Notice(`Error in request while trying to search ${type}!`);
n.noticeEl.addClass("notice_error");
this.SendWarningNotice(`Error in request while trying to search ${type}!\nretrying...`);
this.searchOmdb(title, type, depth + 1);
return;
}

Expand Down Expand Up @@ -186,9 +192,8 @@ export default class Moviegrabber extends Plugin {

var dir = type == 'movie' ? this.settings.MovieDirectory : this.settings.SeriesDirectory;

dir.replace(/(^[/\s]+)|([/\s]+$)/g, ''); // clean up

var dir = dir != '' ? `/${dir.replace(/\/$/, "")}/` : '';
dir = this.CleanPath(dir);
dir = dir != '' ? `/${dir}/` : ''; // this might be unecessary.

if (!(await this.app.vault.adapter.exists(dir))) {
var n = new Notice(`Folder for ${type}: ${dir} does not exist!`)
Expand All @@ -214,10 +219,21 @@ export default class Moviegrabber extends Plugin {

new Notice(`Creating Note for: ${item.Title} (${item.Year})`);

// clean Movie Title to avoid frontmatter issues
itemData.Title = itemData.Title.replace(/[/\\?%*:|"<>]/g, '');
// add and clean up data
itemData.Title = itemData.Title.replace(/[/\\?%*:|"<>]/g, ''); // clean Movie Title to avoid frontmatter issues
itemData.Runtime = itemData.Runtime ? itemData.Runtime.split(" ")[0] : '';
if (this.settings.YouTube_API_Key != '') {
itemData.YoutubeEmbed = await this.getTrailerEmbed(itemData.Title, itemData.Year);
}

// get and fill template

var content =
var template = await this.GetTemplate(type)
if (template == null) {
return;
}
var content = await this.FillTemplate(template, itemData)
/* var content =
`---\n`+
`type: ${type}\n`+
`country: ${itemData.Country}\n`+
Expand All @@ -235,13 +251,90 @@ export default class Moviegrabber extends Plugin {
`poster: "${itemData.Poster}"\n`+
`availability:\n`+
`---\n`+
`${itemData.Plot}`
`${itemData.Plot}` */

// create and open file
var tFile = await this.app.vault.create(path, content);
if (this.settings.SwitchToCreatedNote) {
this.app.workspace.getLeaf().openFile(tFile);
}
}

async GetTemplate(type : 'movie' | 'series') : Promise<string | null> {
if (this.settings.MovieTemplatePath == '') {
// no template given, return default
return DEFAULT_TEMPLATE;
}

// handle paths with both .md and not .md at the end
var path = type == 'movie' ? this.settings.MovieTemplatePath : this.settings.SeriesTemplatePath;
path = this.CleanPath(path).replace(".md", '') + '.md';

var tAbstractFile = await this.app.vault.getAbstractFileByPath(path);
if (tAbstractFile == null || !(tAbstractFile instanceof TFile)) {
this.SendWarningNotice("Template Path is not a File in Vault.\nstopping")
return null;
}

var tFile = tAbstractFile as TFile;
var text = await this.app.vault.cachedRead(tFile);
return text;
}

async FillTemplate(template : string, data : MovieData) : Promise<string> {
return template.replace(/{{(.*?)}}/g, (match) => {
return data[match.split(/{{|}}/).filter(Boolean)[0].trim()]
});
}

SendWarningNotice(text:string) {
var n = new Notice(text);
n.noticeEl.addClass("notice_error")
}

async CreateDefaultTemplateFile(){
var content = DEFAULT_TEMPLATE +
"\n\n\n%%\n" +
"Available tags:\n" +
"----------------------\n" +
"{{Title}}\n" +
"{{Year}}\n" +
"{{Rated}}\n" +
"{{Runtime}}\n" +
"{{Genre}}\n" +
"{{Director}}\n" +
"{{Writer}}\n" +
"{{Actors}}\n" +
"{{Plot}}\n" +
"{{Language}}\n" +
"{{Country}}\n" +
"{{Awards}}\n" +
"{{Poster}}\n" +
"{{Ratings}}\n" +
"{{Metascore}}\n" +
"{{imdbRating}}\n" +
"{{imdbVotes}}\n" +
"{{imdbID}}\n" +
"{{Type}}\n" +
"{{DVD}}\n" +
"{{BoxOffice}}\n" +
"{{Production}}\n" +
"{{Website}}\n" +
"{{totalSeasons}}\n" +
"{{YoutubeEmbed}}\n" +
"%%";

// create and open file
var tFile = await this.app.vault.create('/Moviegrabber-example-template.md', content);
this.app.workspace.getLeaf().openFile(tFile);

}

CleanPath(path : string) :string {
path.replace(/(^[/\s]+)|([/\s]+$)/g, ''); // clean up forbidden symbols
path = path != '' ? `${path.replace(/\/$/, "")}` : ''; // trim "/"
return path;
}
}

class MoviegrabberSettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -310,5 +403,36 @@ class MoviegrabberSettingTab extends PluginSettingTab {
this.plugin.settings.SwitchToCreatedNote = value;
await this.plugin.saveSettings();
}));

containerEl.createEl('h1', { text : "Templates"})
new Setting(containerEl)
.setName('Movie template file path')
.setDesc('Path to the template file that is used to create notes for movies')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.MovieTemplatePath)
.onChange(async (value) => {
this.plugin.settings.MovieTemplatePath = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Series template file path')
.setDesc('Path to the template file that is used to create notes for series')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.SeriesTemplatePath)
.onChange(async (value) => {
this.plugin.settings.SeriesTemplatePath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Create example template file')
.setDesc('Creates an example template file to expand and use.\nThe file is called `/Moviegrabber-example-template`')
.addButton(btn => btn
.setButtonText("Create")
.onClick((event) => {
this.plugin.CreateDefaultTemplateFile();
}));
}
}
1 change: 1 addition & 0 deletions src/MoviegrabberSearchObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface MovieSearchItem{
}

export interface MovieData{
[key:string]: any,
Title: string,
Year: number,
Rated: string
Expand Down
27 changes: 25 additions & 2 deletions src/MoviegrabberSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,35 @@ export interface MoviegrabberSettings {
OMDb_API_Key: string;
YouTube_API_Key: string;
SwitchToCreatedNote: boolean;

MovieTemplatePath: string;
SeriesTemplatePath: string;
}

export const DEFAULT_SETTINGS: MoviegrabberSettings = {
MovieDirectory: 'Movies',
SeriesDirectory: 'Series',
OMDb_API_Key: '',
YouTube_API_Key: '',
SwitchToCreatedNote: true
}
SwitchToCreatedNote: true,
MovieTemplatePath: '',
SeriesTemplatePath: ''
}

export const DEFAULT_TEMPLATE: string = "---\n"+
"type: {{Type}}\n"+
`country: {{Country}}\n`+
`title: {{Title}}\n`+
`year: {{Year}}\n`+
`director: {{Director}}\n`+
`actors: [{{Actors}}]\n`+
`genre: [{{Genre}}]\n`+
`length: {{Runtime}}\n`+
`seen:\n`+
`rating: \n`+
`found_at: \n`+
`trailer_embed: {{YoutubeEmbed}}\n` +
`poster: "{{Poster}}"\n`+
`availability:\n`+
`---\n`+
`{{Plot}}`

0 comments on commit d093c97

Please sign in to comment.