Skip to content

Commit

Permalink
merging local Poster saves
Browse files Browse the repository at this point in the history
  • Loading branch information
Superschnizel committed Mar 5, 2024
2 parents e02ff51 + eb84e7e commit 3635c99
Show file tree
Hide file tree
Showing 4 changed files with 532 additions and 353 deletions.
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
# Obsidian Moviegrabber

A plugin to generate notes for movies and series with properties that can be used with [dataview](https://github.com/blacksmithgu/obsidian-dataview). Uses the [Open Movie Database (OMDb) API](http://www.omdbapi.com/) to retrieve movie/series data and the [Youtube Data API](https://developers.google.com/youtube/v3/docs?hl=de) to get the embed links for the trailers (optional).
Expand Down Expand Up @@ -92,3 +93,114 @@ FROM "Movies" WHERE type = "movie" AND seen = Null
````

Note that the cards need at least ``country, year, length, trailer_embed`` in the querry to show a card.
=======
# Obsidian Moviegrabber

A plugin to generate notes for movies and series with properties that can be used with [dataview](https://github.com/blacksmithgu/obsidian-dataview). Uses the [Open Movie Database (OMDb) API](http://www.omdbapi.com/) to retrieve movie/series data and the [Youtube Data API](https://developers.google.com/youtube/v3/docs?hl=de) to get the embed links for the trailers (optional).

<p float="left">
<img src="https://github.com/Superschnizel/Obsidian-Moviegrabber/assets/47162464/3df2496a-ad9c-46ec-a806-b048100e7d70" width="41%">
<img src="https://github.com/Superschnizel/Obsidian-Moviegrabber/assets/47162464/99770856-7e87-4333-88c0-690e16688d55" width="50%">
</p>

---

# Usage

https://github.com/Superschnizel/Obsidian-Moviegrabber/assets/47162464/28e2ca9d-e504-4923-9609-dc1e5953d219

*(Disclaimer: the choice selection uses outside assets for the movie posters in the preview, retrieved in the search request to OMDb)*

To use this plugin you need to create an API key for the OMDb [here](http://www.omdbapi.com/apikey.aspx) and optionally also a Youtube Data API Key as described [here](https://developers.google.com/youtube/v3/docs#calling-the-api). and set these in the plugin settings.

To search for a movie or series, simply call the command `Search movie` or `Search series` and search a movie by entering a title or a valid IMDB-id.

## Templates

To define how the data will be saved in your notes you can define a template. This template uses tags of the form `{{tag}}` to fill in the data. The available tags are:
```
{{Title}}
{{Year}}
{{Rated}}
{{Runtime}}
{{Genre}}
{{Director}}
{{Writer}}
{{Actors}}
{{Plot}}
{{Language}}
{{Country}}
{{Awards}}
{{Poster}}
{{Ratings}}
{{Metascore}}
{{imdbRating}}
{{imdbVotes}}
{{imdbID}}
{{Type}}
{{DVD}}
{{BoxOffice}}
{{Production}}
{{Website}}
{{totalSeasons}}
{{YoutubeEmbed}}
```
### 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

When trying to create a note for a movie that already exists you will be asked if you want to overwrite the existing note. If you want to keep something from the old note in the newly generated one you can make use of the delimiter string:

```
%%==MOVIEGRABBER_KEEP==%%
```
Everything below this will be transfered to the new note when overwriting.

# Using the generated notes with Dataview and custom CSS

Using a [dataview](https://github.com/blacksmithgu/) table in combination with a custom [css snippet](https://help.obsidian.md/Extending+Obsidian/CSS+snippets), you can use these notes to create an interactive display for your movies.

![grafik](https://github.com/Superschnizel/Obsidian-Moviegrabber/assets/47162464/be2345a6-eeab-4e8b-b2e1-2a5263a9dc41)


https://github.com/Superschnizel/Obsidian-Moviegrabber/assets/47162464/fc555eea-0ae4-46b4-87d2-44cc2626d387

To use this, copy `aditional_css/CardViewMovies.css` to your vault's snippets folder (`.obsidian/snippets/`) and put

```yaml
---
cssclass: CardViewMovies
---
```
at the top of your note.

A dataview query for movies not yet seen could look something like this:

````dataview
```dataview
TABLE country, year, length, trailer_embed, availability, rating, seen
FROM "Movies" WHERE type = "movie" AND seen = Null
```
````

Note that the cards need at least ``country, year, length, trailer_embed`` in the querry to show a card.
>>>>>>> eb84e7e8b72a19542ac9510d4053a36bbfbc310b
48 changes: 42 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,14 @@ export default class Moviegrabber extends Plugin {
return;
}

let titleTemplate = type == 'movie'
let filenameTemplate = type == 'movie'
? this.settings.FilenameTemplateMovie
: this.settings.FilenameTemplateSeries;
let title = await this.FillTemplate(titleTemplate, itemData);
title = title == '' ? item.Title : title;
let filename = await this.FillTemplate(filenameTemplate, itemData);
filename = filename == '' ? item.Title : filename;

let path = `${dir}${title.replace(/[/\\?%*:|"<>]/g, '')}.md`
const cleanedTitle = filename.replace(/[/\\?%*:|"<>]/g, '')
let path = `${dir}${cleanedTitle}.md`
let file = this.app.vault.getAbstractFileByPath(path);

// console.log(`${file}, path: ${path}`);
Expand All @@ -252,11 +253,27 @@ export default class Moviegrabber extends Plugin {
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);
}

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
Expand Down Expand Up @@ -313,7 +330,26 @@ export default class Moviegrabber extends Plugin {
}
}

async GetTemplate(type: 'movie' | 'series'): Promise<string | null> {

async downloadAndSavePoster(imageUrl: string, directory: string, imageName: string): Promise<string> {
if (!directory) {
console.error("Poster image directory is not specified.");
throw new Error("Poster image directory is not specified.");
}

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);
throw error;
}
}

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

0 comments on commit 3635c99

Please sign in to comment.