From 80256d99a9bbff55ac36fc8e4916e8e122373b89 Mon Sep 17 00:00:00 2001 From: Leon Holtmeier <47162464+Superschnizel@users.noreply.github.com> Date: Thu, 18 Jan 2024 20:16:42 +0100 Subject: [PATCH 1/6] Update README.md --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e509481..3212281 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,25 @@ To define how the data will be saved in your notes you can define a template. Th {{totalSeasons}} {{YoutubeEmbed}} ``` -Additionally you can define a pre- and suffix to be applied to the data. this is done by using `{{tag|prefix|suffix}}`. *(if you want to use the "|" character, it can be escaped using "\\|")*. +### Pre- and Suffix + +You can define a pre- and suffix to be applied to the data. this is done by using `{{tag|prefix|suffix}}`. *(if you want to use the "|" character, it can be escaped using "\\|")*. Example: ``{{Director|"[[|]]"}}`` will create an internal link of the form `"[[Director]]"` +### Regex Transformation + +Additionally you can also give a *regex transform* to transform the data to your liking by using `{{tag|prefix|suffix|regexTransform}}`. + +Inside a regex transformation every regular expression given inside `<$ $>` delimiters will be replaced by the matching string from the input. This allows you to bring the data into the form you need it. + +Examples: +- `{{Actors|"[[|]]"|<$\w+$$>, <$^\w+$>\|@<$^\w+$> <$\w+$$>}}`. This regex transformation will transform the "Actors" data in the form of `Firstname Lastname` into a link in the form of `"[[Lastname, Firstname|@Firstname Lastname]]"` (see issue #21). +- `{{Ratings|"|"|<$Rotten Tomatoes\: .*$>}}`. This regex transformation will result in only the Rotten Tomatoes rating to be shown. + +When a regex transformation results in an empty string pre- and suffix will not be applied. + + You can generate an example template in the plugin settings. If no template is given, this default template is used. ## Regenerating notes From bc87fd12470df8f43a78f74ad6bb7c933b60d8ed Mon Sep 17 00:00:00 2001 From: Marlon May <77202149+Marlon154@users.noreply.github.com> Date: Sat, 24 Feb 2024 18:46:50 +0100 Subject: [PATCH 2/6] add feature save poster locally --- main.ts | 24 ++++++++++++++++++++++++ src/MoviegrabberSearchObject.ts | 2 ++ src/MoviegrabberSettings.ts | 31 ++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index a28280a..b8b47d2 100644 --- a/main.ts +++ b/main.ts @@ -251,6 +251,12 @@ export default class Moviegrabber extends Plugin { } async createNote(item : MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { + if (this.settings.enablePosterImageSave && item.Poster && item.Poster !== "N/A") { + // Construct the image name in a similar fashion to the provided example + const imageName = `${item.Title}_${item.Year}`.replace(/[^a-z0-9]+/gi, '_').toLowerCase() + '.jpg'; + const posterDirectory = this.settings.posterImagePath; + item.PosterLocal = await this.downloadAndSavePoster(item.Poster, posterDirectory, imageName); + } new Notice(`Creating Note for: ${item.Title} (${item.Year})`); @@ -308,6 +314,24 @@ export default class Moviegrabber extends Plugin { } } + async downloadAndSavePoster(imageUrl: string, directory: string, imageName: string): Promise { + if (!directory) { + console.error("Poster image directory is not specified."); + return ""; + } + + const filePath = normalizePath(`${directory}/${imageName}`); + try { + const response = await requestUrl({ url: imageUrl, method: "GET" }); + const imageData = response.arrayBuffer; + await this.app.vault.adapter.writeBinary(filePath, imageData); + return filePath; + } catch (error) { + console.error("Error downloading or saving poster image:", error); + return ""; + } + } + async GetTemplate(type : 'movie' | 'series') : Promise { if (this.settings.MovieTemplatePath == '') { // no template given, return default diff --git a/src/MoviegrabberSearchObject.ts b/src/MoviegrabberSearchObject.ts index 1fa56d5..5399338 100644 --- a/src/MoviegrabberSearchObject.ts +++ b/src/MoviegrabberSearchObject.ts @@ -27,6 +27,7 @@ export interface MovieData{ Country: string, Awards: string, Poster: string, + PosterLocal: string, Ratings: Array, Metascore: number, imdbRating: number, @@ -74,6 +75,7 @@ export const MOVIE_DATA_LOWER : MovieDataLowercase = { "country" : "Country", "awards" : "Awards", "poster" : "Poster", + "posterlocal": "PosterLocal", "ratings" : "Ratings", "metascore" : "Metascore", "imdbrating" : "imdbRating", diff --git a/src/MoviegrabberSettings.ts b/src/MoviegrabberSettings.ts index b5f7542..13f3d40 100644 --- a/src/MoviegrabberSettings.ts +++ b/src/MoviegrabberSettings.ts @@ -16,6 +16,9 @@ export interface MoviegrabberSettings { PlotLength: string; FilenameTemplateMovie: string; FilenameTemplateSeries: string; + + enablePosterImageSave: boolean; + posterImagePath: string; } export const DEFAULT_SETTINGS: MoviegrabberSettings = { @@ -28,7 +31,9 @@ export const DEFAULT_SETTINGS: MoviegrabberSettings = { SeriesTemplatePath: '', PlotLength: 'short', FilenameTemplateMovie: '{{Title}}', - FilenameTemplateSeries: '{{Title}}' + FilenameTemplateSeries: '{{Title}}', + enablePosterImageSave: false, + posterImagePath: '', } export const DEFAULT_TEMPLATE: string = "---\n"+ @@ -190,5 +195,29 @@ export class MoviegrabberSettingTab extends PluginSettingTab { this.plugin.settings.FilenameTemplateSeries = value; 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(); + }); + }); } } \ No newline at end of file From d71c23f57f315fcaf00ea86a57ed4c9635ed2dc2 Mon Sep 17 00:00:00 2001 From: Marlon May <77202149+Marlon154@users.noreply.github.com> Date: Sat, 2 Mar 2024 19:59:31 +0100 Subject: [PATCH 3/6] fix naming of poster image --- main.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index b8b47d2..f20a602 100644 --- a/main.ts +++ b/main.ts @@ -252,8 +252,12 @@ export default class Moviegrabber extends Plugin { async createNote(item : MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { if (this.settings.enablePosterImageSave && item.Poster && item.Poster !== "N/A") { - // Construct the image name in a similar fashion to the provided example - const imageName = `${item.Title}_${item.Year}`.replace(/[^a-z0-9]+/gi, '_').toLowerCase() + '.jpg'; + const titleTemplate = type == 'movie' + ? this.settings.FilenameTemplateMovie + : this.settings.FilenameTemplateSeries; + let noteTitle = await this.FillTemplate(titleTemplate, item); + noteTitle = noteTitle == '' ? item.Title : noteTitle; + const imageName = `${noteTitle.replace(/[/\\?%*:|"<>]/g, '').toLowerCase()}.jpg`; const posterDirectory = this.settings.posterImagePath; item.PosterLocal = await this.downloadAndSavePoster(item.Poster, posterDirectory, imageName); } From e01ef8c9bfd56e6a0027e38746554873583be8be Mon Sep 17 00:00:00 2001 From: Marlon May <77202149+Marlon154@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:54:08 +0100 Subject: [PATCH 4/6] move save image --- main.ts | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/main.ts b/main.ts index f20a602..e377570 100644 --- a/main.ts +++ b/main.ts @@ -236,7 +236,8 @@ export default class Moviegrabber extends Plugin { let title = await this.FillTemplate(titleTemplate, itemData); title = title == '' ? item.Title : title; - let path = `${dir}${title.replace(/[/\\?%*:|"<>]/g, '')}.md` + const cleanedTitle = title.replace(/[/\\?%*:|"<>]/g, '') + let path = `${dir}${cleanedTitle}.md` let file = this.app.vault.getAbstractFileByPath(path); // console.log(`${file}, path: ${path}`); @@ -248,20 +249,27 @@ export default class Moviegrabber extends Plugin { } this.createNote(itemData, type, path); - } - async createNote(item : MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { - if (this.settings.enablePosterImageSave && item.Poster && item.Poster !== "N/A") { - const titleTemplate = type == 'movie' - ? this.settings.FilenameTemplateMovie - : this.settings.FilenameTemplateSeries; - let noteTitle = await this.FillTemplate(titleTemplate, item); - noteTitle = noteTitle == '' ? item.Title : noteTitle; - const imageName = `${noteTitle.replace(/[/\\?%*:|"<>]/g, '').toLowerCase()}.jpg`; + if (this.settings.enablePosterImageSave && itemData.Poster !== null && itemData.Poster !== "N/A") { + const imageName = `${cleanedTitle}.jpg`; const posterDirectory = this.settings.posterImagePath; - item.PosterLocal = await this.downloadAndSavePoster(item.Poster, posterDirectory, imageName); + this.downloadAndSavePoster(item.Poster, posterDirectory, imageName) + 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; + }); + } - + } + + async createNote(item : MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { new Notice(`Creating Note for: ${item.Title} (${item.Year})`); // add and clean up data @@ -321,7 +329,7 @@ export default class Moviegrabber extends Plugin { async downloadAndSavePoster(imageUrl: string, directory: string, imageName: string): Promise { if (!directory) { console.error("Poster image directory is not specified."); - return ""; + throw new Error("Poster image directory is not specified."); } const filePath = normalizePath(`${directory}/${imageName}`); @@ -332,7 +340,7 @@ export default class Moviegrabber extends Plugin { return filePath; } catch (error) { console.error("Error downloading or saving poster image:", error); - return ""; + throw error; } } From 85777f52903d8877de1967322748e97cfc363e83 Mon Sep 17 00:00:00 2001 From: Marlon May <77202149+Marlon154@users.noreply.github.com> Date: Mon, 4 Mar 2024 06:36:54 +0100 Subject: [PATCH 5/6] Update main.ts --- main.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/main.ts b/main.ts index e377570..d3f48fa 100644 --- a/main.ts +++ b/main.ts @@ -254,7 +254,6 @@ export default class Moviegrabber extends Plugin { const imageName = `${cleanedTitle}.jpg`; const posterDirectory = this.settings.posterImagePath; this.downloadAndSavePoster(item.Poster, posterDirectory, imageName) - this.downloadAndSavePoster(item.Poster, posterDirectory, imageName) .then(posterLocalPath => { itemData.PosterLocal = posterLocalPath; new Notice(`Saved poster for: ${itemData.Title} (${itemData.Year})`); From e219cafcf834ece50e4651edfcee56c1f9a6e91b Mon Sep 17 00:00:00 2001 From: Marlon May <77202149+Marlon154@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:38:17 +0100 Subject: [PATCH 6/6] Update main.ts --- main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index d3f48fa..1f407cc 100644 --- a/main.ts +++ b/main.ts @@ -248,8 +248,6 @@ export default class Moviegrabber extends Plugin { return; } - this.createNote(itemData, type, path); - if (this.settings.enablePosterImageSave && itemData.Poster !== null && itemData.Poster !== "N/A") { const imageName = `${cleanedTitle}.jpg`; const posterDirectory = this.settings.posterImagePath; @@ -266,6 +264,8 @@ export default class Moviegrabber extends Plugin { }); } + + this.createNote(itemData, type, path); } async createNote(item : MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) {