NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name BLU Add Wikidata Metadata_BETA
// @description Search Wikidata and add more metadata
// @version 1.3.0
// @author MiM
// @copyright 2024
// @license MIT
// @match https://blutopia.cc/torrents/similar/*
// @match https://blutopia.cc/torrents/*
// @match https://blutopia.cc/requests/*
// @match https://blutopia.cc/wikidata
// @grant GM_xmlhttpRequest
// @icon https://blutopia.cc/favicon.ico
// @updateURL https://openuserjs.org/meta/MiM/BLU_Add_Wikidata_Metadata.meta.js
// @downloadURL https://openuserjs.org/install/MiM/BLU_Add_Wikidata_Metadata.user.js
// @connect query.wikidata.org
// ==/UserScript==
// How long to cache. 24 hours is the default.
const CACHE_TIME = 1000 * 60 * 60 * 24;
var streamingRow;
var socialRow;
var customSites;
var customRow;
var imdb_id;
const break_html = `<div style="flex-basis: 100%; height: 0;"></div>`;
// Base URL to be used, potentially create this dynamically later updates
const sparql = "https://query.wikidata.org/sparql?query="
const sparql_log = "https://query.wikidata.org/index.html#"
const base_query = `SELECT DISTINCT *
WHERE {
?subject wdt:P345 "{IMDB}".
`
// Add metadata_links
const metadata_links_sparql = `
OPTIONAL {?Wikipedia schema:about ?subject}
OPTIONAL {?subject wdt:P2671 ?Google}
OPTIONAL {?subject wdt:P1258 ?RottenTomatoes}
OPTIONAL {?subject wdt:P345 ?IMDb}
OP`
const metadataType = Object.freeze({
METADATA: Symbol("metadata"),
STREAMING: Symbol("streaming"),
},
';
url += metadata_links_sparql + base_order_by
if (original_language_code != '') {
url = url.replace(`{ORIGINAL_LANGUAGE}`, '');
let original_language_order = original_language_sparql.replace(`{LANGUAGE_CODE}`, original_language_code);
url += original_language_sparql
}
url += final_order_by
url = encodeURIComponent(url)
console.log(sparql_log + url);
url = sparql + url
const headers = {
"Accept": "application/json"
};
let resolver;
let rejecter;
const p = new Promise((resolveFn, rejectFn) => {
resolver = resolveFn;
rejecter = rejectFn;
});
const final = GM_xmlhttpRequest({
method: "GET",
url: url,
headers: headers,
onload: (response) => resolver(response),
onerror: (response) => rejecter(response),
ontimeout: (response) => rejecter(response)
});
return p;
}
function get_imdb_id() {
let imdb_id = ''
try {
let imdb_url = document.getElementsByClassName("meta__imdb")[0].getElementsByTagName("a")[0].href;
let url_regex = /^(.*)(tt\d+)([^\d]*)$/;
imdb_id = imdb_url.match(url_regex)[2];
}
catch {}
return imdb_id;
}
site_title = site[0].trim().replace('"', "'");
site_class = site_title.replace(" ", "")
site_uri = site[1].trim().replace("{ttimdb}", imdb_id).replace("{imdb}", imdb_id.replace("tt", ""));
site_icon = site[2].trim();
custom_html = custom_html + `
<li class="meta__${site_class}">
<a class="meta-id-tag" href="${site_uri}" title="${site_title}" target="_blank">
<img src="${site_icon}">
</a>
</li>`;
}
return custom_html;
}
function add_to_site(wikidata) {
let meta_html_tag = document.getElementsByClassName("meta__ids")[0];
var meta_row;
var meta_url;
var meta_img;
var meta_title;
var social_found = false;
var streaming_found = false;
var meta_html;
let url = '';
var metadata_html = '';
var streaming_html = "";
var social_html = "";
ikidata">
<a class="meta-id-tag" href="${meta_url}" title="${meta_title}" target="_blank">
<img src="${meta_img}">
</a>
</li>`;
document.getElementsByClassName("meta__ids")[0].innerHTML += meta_html
}
}
var num_rows_added = 0;
var rows_needed = [];
if (streamingRow > 1 && streaming_found) {
rows_needed.push(streamingRow)
}
if (socialRow > 1 && social_found) {
rows_needed.push(socialRow)
}
if (customSites.length > 0 && customRow > 1) {
rows_needed.push(customRow)
}
rows_needed = removeDuplicates(rows_needed);
num_rows_added = rows_needed.length;
custom_html = get_custom_sites_html();
var current_row = 0;
rows_needed.sort();
meta_html_tag.innerHTML += metadata_html
if (streaming_found && streamingRow == 1) {
meta_html_tag.innerHTML += streaming_html
}
if (social_found && socialRow == 1) {
meta_html_tag.innerHTML += social_html
dow.localStorage.getItem(`wikidata-settings-social`)).row)
}
catch {}
try {
customRow = JSON.parse(window.localStorage.getItem(`wikidata-settings-custom`)).row
}
catch {}
try {
customSites = JSON.parse(window.localStorage.getItem(`wikidata-settings-custom`)).sites
}
catch {}
streamingRow = streamingRow >= 0 ? streamingRow : 2;
socialRow = socialRow >= 0 ? socialRow : 3;
customRow = customRow >= 0 ? customRow : 4;
customSites = customSites ? customSites : '';
}
}));
window.localStorage.setItem('wikidata-settings-social', JSON.stringify({
'row': social_value
}));
window.localStorage.setItem('wikidata-settings-custom', JSON.stringify({
'sites': custom_value,
'row': customPos_value
}));
}
const currentUrl = window.location.href;
(async function () {
set_from_user_settings();
if (currentUrl.endsWith("/wikidata")) {
document.head.innerHTML = '';
document.body.innerHTML = settings_page_body;
settings_page();
document.getElementById("update_wikidata").onclick = update_settings_page;
return;
}
'use strict';
imdb_id = get_imdb_id();
if (imdb_id === '') {
return; // No IMDb to search by
}
let primary_language = get_primary_language();
// Set flex-wrap so meta doesn't overflow.
document.getElementsByClassName("meta__ids")[0].style["flex-wrap"] = "wrap"
var wikidata;
try {
let updateCache = true;
try {
let oldTime = JSON.parse(window.localStorage.getItem(`wikidata-${imdb_id}-time`)).time
let difference = (new Date).getTime() - oldTime;
updateCache = difference > CACHE_TIME
}
catch {}
if (updateCache) {
try {
window.localStorage.removeItem(`wikidata-${imdb_id}-wikidata`);
window.localStorage.removeItem(`wikidata-${imdb_id}-time`);
}
catch {}
let wikidata_response = await search_wikidata_com(imdb_id, primary_language)
let wikidata_json = JSON.parse(wikidata_response.response);
wikidata = wikidata_json.results.bindings[0];
window.localStorage.setItem(`wikidata-${imdb_id}-wikidata`, JSON.stringify(wikidata));
window.localStorage.setItem(`wikidata-${imdb_id}-time`, JSON.stringify({
'time': (new Date).getTime()
}));
}
else {
wikidata = JSON.parse(window.localStorage.getItem(`wikidata-${imdb_id}-wikidata`));
}
add_to_site(wikidata);
}
catch {
//No Wikidata found, try adding custom
let meta_html_tag = document.getElementsByClassName("meta__ids")[0];
var custom_html = get_custom_sites_html();
if (custom_html.length > 0 && customRow == 1) {
meta_html_tag.innerHTML += custom_html
}
else if (custom_html.length > 0 && customRow >= 1) {
document.getElementsByClassName("meta__ids")[0].innerHTML += `<div style="flex-basis: 100%; height: 0;"></div>`;
document.getElementsByClassName("meta__ids")[0].innerHTML += custom_html;
}
}
})();