JSON-LD from IMDb to Wikidata

Get data from JSON-LD from IMDb, use QuickStatements to publish it on Wikidata

目前為 2019-02-06 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
        }

    });
}