Skip to content

Commit

Permalink
local poster saves integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Superschnizel committed Mar 7, 2024
1 parent 3635c99 commit dec2b1a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 99 deletions.
40 changes: 21 additions & 19 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,32 +248,16 @@ export default class Moviegrabber extends Plugin {
// console.log(`${file}, path: ${path}`);
if (file != null && file instanceof TFile) {
new ConfirmOverwriteModal(this.app, item, () => {
this.createNote(itemData!, type, path, file as TFile);
this.createNote(itemData!, type, path, cleanedTitle, file as TFile);
}).open();
return;
}

if (this.settings.enablePosterImageSave && itemData.Poster !== null && itemData.Poster !== "N/A") {
const imageName = `${cleanedTitle}.jpg`;
const posterDirectory = this.settings.posterImagePath;
this.downloadAndSavePoster(item.Poster, posterDirectory, imageName)
.then(posterLocalPath => {
itemData!.PosterLocal = posterLocalPath;
new Notice(`Saved poster for: ${itemData!.Title} (${itemData!.Year})`);
})
.catch(error => {
console.error("Failed to download and save the poster:", error);
new Notice(`Failed to download and save the movie poster for: ${itemData!.Title} (${itemData!.Year})`);
n.noticeEl.addClass("notice_error");
itemData!.PosterLocal = 'null';
});

}

this.createNote(itemData, type, path);
this.createNote(itemData, type, path, cleanedTitle);
}

async createNote(item: MovieData, type: 'movie' | 'series', path: string, tFile: TFile | null = null) {
async createNote(item: MovieData, type: 'movie' | 'series', path: string, filename: string, tFile: TFile | null = null) {
new Notice(`Creating Note for: ${item.Title} (${item.Year})`);

// add and clean up data
Expand All @@ -283,6 +267,23 @@ export default class Moviegrabber extends Plugin {
item.YoutubeEmbed = await this.getTrailerEmbed(item.Title, item.Year);
}

if (this.settings.enablePosterImageSave && item.Poster !== null && item.Poster !== "N/A") {
const imageName = `${filename}.jpg`;
const posterDirectory = this.settings.posterImagePath;
await this.downloadAndSavePoster(item.Poster, posterDirectory, imageName)
.then(posterLocalPath => {
item.PosterLocal = imageName; // i think full path is usualy not needed.
new Notice(`Saved poster for: ${item!.Title} (${item!.Year})`);
})
.catch(error => {
console.error("Failed to download and save the poster:", error);
let n = new Notice(`Failed to download and save the movie poster for: ${item!.Title} (${item!.Year})`);
n.noticeEl.addClass("notice_error");
item!.PosterLocal = 'null';
});

}

// get and fill template

var template = await this.GetTemplate(type)
Expand Down Expand Up @@ -445,6 +446,7 @@ export default class Moviegrabber extends Plugin {
"{{Country}}\n" +
"{{Awards}}\n" +
"{{Poster}}\n" +
"{{PosterLocal}}" +
"{{Ratings}}\n" +
"{{Metascore}}\n" +
"{{imdbRating}}\n" +
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "moviegrabber",
"name": "Moviegrabber",
"version": "1.1.16",
"version": "1.1.17",
"minAppVersion": "0.15.0",
"description": "Grab movie data from public APIs and transform it into notes with a powerful templating engine.",
"author": "Superschnizel",
Expand Down
162 changes: 83 additions & 79 deletions src/MoviegrabberSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ export const DEFAULT_SETTINGS: MoviegrabberSettings = {
posterImagePath: '',
}

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`+
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`+
`poster: "{{Poster}}"\n` +
`availability:\n` +
`---\n` +
`{{Plot}}`;

export class MoviegrabberSettingTab extends PluginSettingTab {
Expand All @@ -63,36 +63,36 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
}

display(): void {
const {containerEl} = this;
const { containerEl } = this;

containerEl.empty();

new Setting(containerEl)
.setName('Movie folder')
.setDesc('Folder in which to save the generated notes for series')
.addSearch((cb) => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.MovieDirectory = newFolder;
await this.plugin.saveSettings();
});
});
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.MovieDirectory = newFolder;
await this.plugin.saveSettings();
});
});


new Setting(containerEl)
.setName('Series folder')
.setDesc('Folder in which to save the generated notes for series')
.addSearch((cb) => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.SeriesDirectory = newFolder;
await this.plugin.saveSettings();
});
});
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.SeriesDirectory = newFolder;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('OMDb API key')
Expand All @@ -104,7 +104,7 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
this.plugin.settings.OMDb_API_Key = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Youtube API key')
.setDesc('Your API key for Youtube (optional)')
Expand All @@ -115,7 +115,7 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
this.plugin.settings.YouTube_API_Key = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Plot length')
.setDesc('choose the plot length option for Omdb.')
Expand All @@ -137,34 +137,61 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
this.plugin.settings.SwitchToCreatedNote = value;
await this.plugin.saveSettings();
}));

containerEl.createEl('h1', { text : "Templates"})

new Setting(containerEl)
.setName('Enable poster image saving')
.setDesc('Toggle to enable or disable saving movie poster images as files.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enablePosterImageSave)
.onChange(async value => {
this.plugin.settings.enablePosterImageSave = value;
await this.plugin.saveSettings();
this.display();
}),
);

if (this.plugin.settings.enablePosterImageSave) {
new Setting(containerEl)
.setName('Poster image directory')
.setDesc('Specify the path where poster images should be saved.')
.addSearch(cb => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Enter the path (e.g., Movies/Posters)")
.setValue(this.plugin.settings.posterImagePath)
.onChange(async value => {
this.plugin.settings.posterImagePath = value.trim();
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')
.addSearch((cb) => {
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.MovieTemplatePath = newFile;
await this.plugin.saveSettings();
});
});
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.MovieTemplatePath = newFile;
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')
.addSearch((cb) => {
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.SeriesTemplatePath = newFile;
await this.plugin.saveSettings();
});
});
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.SeriesTemplatePath = newFile;
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`')
Expand All @@ -173,7 +200,7 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
.onClick((event) => {
this.plugin.CreateDefaultTemplateFile();
}));

new Setting(containerEl)
.setName('Movie filename template')
.setDesc('Template used for the filename of Movienotes. Used same template tags as other files.')
Expand All @@ -196,28 +223,5 @@ export class MoviegrabberSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Enable Poster Image Save')
.setDesc('Toggle to enable or disable saving movie poster images in notes.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enablePosterImageSave)
.onChange(async value => {
this.plugin.settings.enablePosterImageSave = value;
await this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName('Poster Image Path')
.setDesc('Specify the path where poster images should be saved.')
.addSearch(cb => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Enter the path (e.g., Movies/Posters)")
.setValue(this.plugin.settings.posterImagePath)
.onChange(async value => {
this.plugin.settings.posterImagePath = value.trim();
await this.plugin.saveSettings();
});
});
}
}

0 comments on commit dec2b1a

Please sign in to comment.