Skip to content

Instantly share code, notes, and snippets.

@basilioss
Last active December 9, 2022 18:34
Show Gist options
  • Save basilioss/2c12b0f7ebeb9d89b86816e8ae829be2 to your computer and use it in GitHub Desktop.
Save basilioss/2c12b0f7ebeb9d89b86816e8ae829be2 to your computer and use it in GitHub Desktop.
This is an Obsidian Templater script that generates frontmatter from IMDb link
---<%*
// v1.3: Fix error if imdbRating is undefined
// Get page source
let url = await tp.system.clipboard()
url = url.replace(/\?.*$/g,"") // remove unnecessary part after '?'
let page = await tp.obsidian.request({url})
let p = new DOMParser()
let doc = p.parseFromString(page, "text/html")
// Aliases for querySelector
let $ = s => doc.querySelector(s)
let $$ = s => doc.querySelectorAll(s)
// Get JSON with metadata
var json = JSON.parse(doc.querySelector("script[type='application/ld+json']").innerHTML)
let title = ""
if (json.alternateName != null) {
title = json.alternateName.replace(/&apos;/g, "'")
} else {
title = json.name.replace(/&apos;/g, "'")
}
let image = ""
if(json.image != null) {
image = json.image
}
let datePublished = ""
if (json.datePublished != null) {
datePublished = JSON.stringify(json.datePublished).substring(1,5)
}
let keywords = ""
if (json.keywords != null) {
keywords = JSON.stringify(json.keywords).toLowerCase().replace(/,/g, '", "')
}
let directors = ""
if (json.director != null) {
directors = json.director.map((director) => director.name)
directors = JSON.stringify(directors).replace(/null,?/g, "").replace(/,/g, ", ")
}
let creators = ""
if (json.creator != null) {
creators = json.creator.map((creator) => creator.name)
creators = JSON.stringify(creators).replace(/null,?/g, "").replace(/,/g, ", ")
}
let duration = ""
if (json.duration != null) {
duration = JSON.stringify(json.duration).toLowerCase()
duration = duration.substring(3, 5) + " " + duration.substring(5, 8)
}
let description = $("span[data-testid='plot-xl']").innerText
let type = JSON.stringify(json['@type']).replace(/TV/, "").replace(/"/g, "").toLowerCase()
// G/PG/PG-13/R/NC-17 etc.
let contentRating = json?.contentRating || ""
let genres = JSON.stringify(json.genre).toLowerCase().replace(/,/g, ", ")
let stars = JSON.stringify(json.actor.map((actor) => actor.name)).replace(/,/g, ", ")
let imdbRating = json.aggregateRating?.ratingValue || ""
let countries = $$("a[href*='country_of_origin']")
countries = Array.from(countries, countries => countries.textContent).join('", "')
%>
title: "<% title %>"
cover: "<% image %>"
description: "<% description %>"
released: <% datePublished %>
genres: <% genres %>
keywords: [<% keywords %>]
stars: <% stars %>
<%* type == "movie" ? tR += "directors: " + directors : tR += "creators: " + creators %>
type: <% type %>
contentRating: <% contentRating %>
imdbRating: <% imdbRating %>
url: "<% url %>"
countries: ["<% countries %>"]
---
@basilioss
Copy link
Author

basilioss commented Apr 9, 2022

@codyburleson
Copy link

Impressive!

@filmgal
Copy link

filmgal commented Sep 1, 2022

Not sure if this is allowed here, but I couldn't find how to send you a private message. Any chance I could hire you to write a very similar thing for me using Letterboxd.com instead of imdb? I already have a google script that does what I need, but your method is much quicker. I've been trying to figure out how to do it on my own, but I can't even get just "title" to work!

@basilioss
Copy link
Author

basilioss commented Sep 5, 2022

@emway66
Copy link

emway66 commented Sep 7, 2022

Hi, thnx for the template, there are just 2 typos in the template:
\nrealesed -> released
\ndesctiption. -> description

@filmgal
Copy link

filmgal commented Sep 28, 2022

Such a great template!
When a film has no rating (Example imdb page) I get console log error: "Cannot read properties of undefined (reading ratingValue')".
To fix it I just removed the two lines that refer to it.
If anyone knows how to fix it so that the template works regardless of whether a rating exists, please let me know

@basilioss
Copy link
Author

@filmgal Fixed, but further development will be continued here: https://github.com/basilioss/obsidian-scrapers/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment