Skip to content

Instantly share code, notes, and snippets.

@bradbeattie
Last active January 25, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradbeattie/2636d102504bc352a1f61c1d8aba18b2 to your computer and use it in GitHub Desktop.
Save bradbeattie/2636d102504bc352a1f61c1d8aba18b2 to your computer and use it in GitHub Desktop.
Adds IMDB ratings to netflix
var OMDB_APIKEY = "..."
var omdb_searched = new Set()
var omdb_netflix_id_regex = new RegExp("/watch/([0-9]+)")
function omdb_get_key(link) {
return "omdb" + omdb_netflix_id_regex.exec(link.prop("href"))[1]
}
function omdb_find_cards() {
jQuery(".meta > .year").each(function() {
var year = jQuery(this)
var title_card = year.parents(".title-card")
var link = title_card.find("a[href^='/watch/']")
var title = title_card.find(".bob-title")
if (year.length && title_card.length && link.length && title.length) {
omdb_fetch_data(
title_card,
title.text().trim(),
omdb_get_key(link)
)
}
})
}
function omdb_fetch_data(title_card, title, ls_key, fallback) {
if (!fallback && localStorage[ls_key]) {
var data = JSON.parse(localStorage[ls_key])
if (title === data.searched.title && ls_key === data.searched.ls_key) {
omdb_show_data(title_card, data)
return
} else {
console.log("TITLE", title, "DATA SEARCHED TITLE", data.searched.title)
console.log("LS KEY", ls_key, "DATA SEARCHED LS_KEY", data.searched.ls_key)
}
}
if (!omdb_searched.has(ls_key) || fallback) {
omdb_searched.add(ls_key)
var url = "https://www.omdbapi.com/?t=" + title + "&apikey=" + OMDB_APIKEY
if (!fallback) url += "&y=" + title_card.find(".year").text()
jQuery.get(url, function(omdb_data) {
data = {
"omdb": omdb_data,
"searched": {
"date": Date.now(),
"title": title,
"ls_key": ls_key,
}
}
if (data.omdb.Error === "Movie not found!") {
if (!fallback) omdb_fetch_data(title_card, title, ls_key, true)
else {
localStorage[ls_key] = JSON.stringify(data)
console.log("Couldn't find", title)
}
} else {
localStorage[ls_key] = JSON.stringify(data)
omdb_show_data(title_card, data)
}
})
}
}
function omdb_show_data(title_card, data) {
title_card.find(".TvObtuseIo, .match-score").remove()
title_card.prepend("<a target='_blank' href='https://www.imdb.com/title/" + data.omdb.imdbID + "' class='TvObtuseIo' data-rating='" + Math.floor(data.omdb.imdbRating) + "'>" + (data.omdb.imdbRating || "?") + "</a>")
}
function omdb_mark_prefetched() {
jQuery("a[href^='/watch/']").each(function() {
var link = jQuery(this)
var title_card = link.parents(".title-card")
var ls_key = omdb_get_key(link)
if (localStorage[ls_key]) {
omdb_show_data(title_card, JSON.parse(localStorage[ls_key]))
}
})
}
var jq = document.createElement('script')
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"
document.getElementsByTagName('head')[0].appendChild(jq)
setTimeout(function() {
jQuery.noConflict()
jQuery("head").append(
"<style type='text/css'>"
+ " .TvObtuseIo { background: yellow; color: black; font-weight: bold; border-radius: 5px; padding: 0.5em; position: absolute; top: 0; right: 0; z-index: 1000; }"
+ " .TvObtuseIo[data-rating='9'] { background: hsl(150, 50%, 50%); }"
+ " .TvObtuseIo[data-rating='8'] { background: hsl(120, 50%, 50%); }"
+ " .TvObtuseIo[data-rating='7'] { background: hsl(90, 50%, 50%); }"
+ " .TvObtuseIo[data-rating='6'] { background: hsl(60, 50%, 50%); }"
+ " .TvObtuseIo[data-rating='5'] { background: hsl(30, 50%, 50%); }"
+ " .TvObtuseIo[data-rating='4'] { background: hsl(0, 50%, 50%); }"
+ "</style>"
)
omdb_mark_prefetched()
setInterval(omdb_find_cards, 500)
}, 500)
@bradbeattie
Copy link
Author

Not happy with this setInterval approach, but I can't seem to attach to the right event. This works fine for now.

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