// ==UserScript==
// @name JSON-LD from IMDb to Wikidata
// @author CennoxX
// @description Get data from JSON-LD from IMDb, use QuickStatements to publish it on Wikidata
// @match https://www.imdb.com/*
// @contact [email protected]
// @namespace https://greasyfork.org/users/21515
// @version 0.5
// @grant GM.xmlHttpRequest
// @license MIT
// ==/UserScript==
var i = 0;
var val = [];
var valOld = [];
var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
//item
var item = '';
getWB(jsonld,'item');
//startTime/publication date
if (jsonld.datePublished != null) {
if (jsonld['@type'] == 'TVSeries') {
pushQSString('P580', jsonld.datePublished);
} else if (jsonld['@type'] == 'Movie') {
pushQSString('P577', jsonld.datePublished);
}
}
//birthDate
if (jsonld.birthDate != null) {
pushQSString('P569', jsonld.birthDate);
}
//actor
if (jsonld.actor != null) {
for (i = 0; i < jsonld.actor.length; i++) {
getWB(jsonld.actor[i],'P161');
}
}
//creator/writer
if (jsonld.creator != null) {
for (i = 0; i < 4; i++) {
if (jsonld.creator[i] != null && jsonld.creator[i].name != null) {
if (jsonld['@type'] == 'TVSeries') {
getWB(jsonld.creator[i],'P170');
} else if (jsonld['@type'] == 'Movie') {
getWB(jsonld.creator[i],'P58');
}
} else {
break;
}
}
}
//director
if (jsonld.director != null) {
for (i = 0; i < jsonld.director.length; i++) {
getWB(jsonld.director[i], 'P57');
}
}
var checkForChanges = setInterval(function() {
if (valOld != val && val != '') {
valOld = val;
} else {
for (var i = 0; i < val.length; i++) {
val[i] = val[i].replace(escape('#ITEM#'), item);
runQS(val[i]);
//console.log(val[i]);
}
clearInterval(checkForChanges);
}
}, 1000);
function getWB(id,prop) {
GM.xmlHttpRequest({
method: "GET",
url: "https://www.wikidata.org/w/api.php?action=query&format=json&list=search&srsearch=haswbstatement:P345=" + id.url.split("/")[2] + "&type=" + prop,
onload: function(response) {
if (response.responseText.length > 0) {
var jsonObj = JSON.parse(response.responseText);
if (jsonObj.query.search[0] != null) {
var qid = jsonObj.query.search[0].title;
var property = response.finalUrl.split("type=")[1].split("&")[0];
if (property == "item"){
item = qid;
} else {
pushQSString(property,qid);
}
}
}
},
onerror: function(response) {
console.log("Error in fetching contents: " + response.responseText);
}
});
}
function pushQSString(property, data) {
var startUrl = 'https://tools.wmflabs.org/quickstatements/api.php?action=run_single_command&site=wikidata&last_item&command=';
if (property == "P569" || property == "P577" || property == "P580" || property == "P582" ) {
val.push(startUrl + escape('{"action":"add","item":"#ITEM#","property":"' + property + '","what":"statement","datavalue":{"type":"time","value":{"time":"+' + data + 'T00:00:00Z","timezone":0,"before":0,"after":0,"precision":11,"calendarmodel":"http://www.wikidata.org/entity/Q1985727"}},"meta":{"message":"","status":"RUN","id":0},"summary":"#temporary_batch_from_IMDb"}').replace('+', '%2b'));
} else {
val.push(startUrl + escape('{"action":"add","item":"#ITEM#","property":"' + property + '","what":"statement","datavalue":{"type":"wikibase-entityid","value":{"entity-type":"item","id":"' + data + '"}},"meta":{"message":"","status":"RUN","id":1},"summary":"#temporary_batch_from_IMDb"}'));
}
}
function runQS(url) {
GM.xmlHttpRequest({
method: "GET",
url: url,
onload: function(response) {
if (response.responseText.length > 0) {
var jsonObj = JSON.parse(response.responseText);
console.log(response.responseText);
}
},
onerror: function(response) {
console.log("Error in fetching contents: " + response.responseText);
}
});
}