Greasy Fork

Filmografie von IMDb nach WP

Wandle die Filmografie von IMDb mithilfe von Wikidata in Wikipedia-Quelltext um.

当前为 2019-02-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         Filmografie von IMDb nach WP
// @namespace    https://greasyfork.org/users/21515
// @description  Wandle die Filmografie von IMDb mithilfe von Wikidata in Wikipedia-Quelltext um.
// @author       CennoxX
// @contact      [email protected]
// @version      0.9.2
// @match        https://www.imdb.com/name/*
// @grant        GM.xmlHttpRequest
// @license      MIT
// ==/UserScript==
(function() {
    var episodeName = 'Folge';
    var work = document.getElementsByClassName('filmo-category-section')[0].children;
    var workArray = [];
    var formattedWork = '';
    var aufruf = 0;
    var done = 0;
    for(var i = 0; i <document.links.length ; i++){
        if (document.links[i].href.slice(-1) == "#" && document.links[i].getAttribute('data-n') != null){
            document.links[i].click()
        }
    }
    setTimeout(function(){
        for (i = 0; i < work.length; i++) {
            var year = work[i].getElementsByClassName('year_column')[0].innerText.trim().split('/')[0].replace('-', '–');
            var title = work[i].getElementsByTagName('a')[0].innerText.replace(' - ', ' – ');
            var type = work[i].innerHTML.split('</b>')[1].split('<br>')[0].trim().replace('TV Movie documentary', 'Dokumentation').replace('Documentary short', 'Dokumentar-Kurzfilm').replace('TV Series documentary', 'Fernsehserie').replace('TV Series short', 'Webserie').replace('TV Series', 'Fernsehserie').replace('TV Mini-Series documentary', 'Miniserie').replace('TV Mini-Series', 'Miniserie').replace('TV Movie', 'Fernsehfilm').replace('Video short', 'Kurzfilm').replace('TV Short', 'Kurzfilm').replace('Short', 'Kurzfilm').replace('(Video)', '');
            if (!type.includes('in_production') && !type.includes('Video Game')) {
                var imdbid = work[i].getAttribute('id').split('-')[1];
                getItemFromWikidata(imdbid);
                if (type == '(Fernsehserie)' || type == '(Miniserie)' || type == '(Webserie)') {
                    var numberOfEpisodes = 0;
                    var allEpisodes = work[i].getElementsByClassName('filmo-episodes');
                    for (var j = 0; j < allEpisodes.length; j++) {
                        if (!allEpisodes[j].innerText.includes('credit only') && allEpisodes[j].innerText != 'Show less') {
                            numberOfEpisodes++
                        }
                    }
                    if (numberOfEpisodes == 0) {
                        workArray.push('* ' + year + ': [[' + imdbid + '|' + title + ']] ' + type.split(',') + '\n');
                    } else if (numberOfEpisodes == 1) {
                        workArray.push('* ' + year + ': [[' + imdbid + '|' + title + ']] ' + type.split(')') + ' eine '+episodeName+')\n');
                    } else {
                        workArray.push('* ' + year + ': [[' + imdbid + '|' + title + ']] ' + type.split(')') + ' ' + numberOfEpisodes + ' '+episodeName+'n)\n');
                    }
                } else {
                    workArray.push('* ' + year + ': [[' + imdbid + '|' + title + ']] ' + type + '\n');
                }
            }
        }
        workArray.sort().forEach(function(entry) {
            formattedWork += entry;
        });
        var checkForChanges = setInterval(function() {
            console.clear();
            if (aufruf == 0 || (done/aufruf) != 1) {
                if (aufruf != 0){
                    console.log(parseInt((done / aufruf) * 100) + '%');
                }
            } else {
                formattedWork = formatText(formattedWork);
                console.log('== Filmografie ==\n'+formattedWork);
                clearInterval(checkForChanges);
            }
        }, 1000);

        function formatText(text) { //check if OT same as DT
            var temp = '';
            text.split('\n').sort().forEach(function(entry) {
                if (entry != '') {
                    if (entry.includes('[[')) {
                        entry = entry.replace('|' + entry.split('[[')[1].split(']]')[0].split('|')[1], '|');
                        if (!entry.split(']]')[0].includes('(')) {
                            entry = entry.replace('|', '');
                        }
                        let titles = entry.split('[[')[1].split(']]')[0].split('|')[0].split('#');
                        if (titles[0].replace(/[-–:., |V\d’'!]/g, '').toLowerCase() != titles[1].split(' (')[0].replace(/[-–:., |V\d’'!]/g, '').toLowerCase()) {
                            if (entry.trim().slice(-1) == ')') {
                                entry = entry.split("]] (")[0] + "]] (''" + titles[0] + "'', " + entry.split("]] (")[1];
                            } else {
                                entry += " ''(" + titles[0] + ")''";
                            }
                        }
                        entry = entry.replace('[[' + titles[0] + '#', '[[');
                    } else {
                        if (entry.includes('|') && entry.split('|')[1].trim().split(']]').length == 2) { //wikidata title != imdb title
                            entry = entry.split('|')[0] + entry.split(']]')[1];
                        } else {
                            entry = entry.replace(']]', '');
                        }
                        let titles = entry.split(/: (.+)/)[1].trim().split(' (')[0].split('#');
                        if (titles[0].replace(/[-–:., |V\d’'!]/g, '').toLowerCase() != titles[1].split(' (')[0].replace(/[-–:., |V\d’'!]/g, '').toLowerCase()) {
                            if (entry.trim().slice(-1) == ')') {
                                entry = entry.split(" (")[0] + " (''" + titles[0] + "'', " + entry.split(" (")[1];
                            } else {
                                entry += " ''(" + titles[0] + ")''";
                            }
                        }
                        entry = entry.replace(': ' + titles[0] + '#', ': ');
                    }
                    temp += entry.replace(/  /g, ' ').replace(/\\u2026/g, '…').replace(/\\u2019/g, '’').replace(/\\u2013/g, '–').replace(/\\u00e4/g, 'ä').replace(/\\u00c4/g, 'Ä').replace(/\\u00f6/g, 'ö').replace(/\\u00d6/g, 'Ö').replace(/\\u00fc/g, 'ü').replace(/\\u00dc/g, 'Ü').replace(/\\u00df/g, 'ß').trim() + '\n';
                }
            });
            return temp;
        }

        function getItemFromWikidata(imdbid) {
            aufruf++;
            GM.xmlHttpRequest({
                method: 'GET',
                url: 'https://www.wikidata.org/w/api.php?action=query&format=json&list=search&srsearch=haswbstatement:P345=' + imdbid,
                onload: function(response) {
                    done++;
                    if (response.responseText.length > 0) {
                        var jsonObj = JSON.parse(response.responseText);
                        var imdbid = response.finalUrl.split('P345=')[1].split('&')[0];
                        if (jsonObj.query.searchinfo.totalhits > 0) {
                            var wikidataid = jsonObj.query.search[0].title;
                            formattedWork = formattedWork.replace(imdbid, wikidataid);
                            getDataFromWikidata(imdbid, wikidataid);
                        } else {
                            formattedWork = formattedWork.replace('[[' + imdbid + '|', imdbid + '#');
                            getDataFromIMDb(imdbid);
                        }
                    }
                },
                onerror: function(response) {
                    done++;
                    console.log('Error in fetching contents: ' + response.responseText);
                }
            });
        }

        function getDataFromWikidata(imdbid, wikidataid) {
            aufruf++;
            GM.xmlHttpRequest({
                method: 'GET',
                url: 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=sitelinks|claims|labels&sitefilter=dewiki&ids=' + wikidataid + '&imdbid=' + imdbid,
                onload: function(response) {
                    done++;
                    if (response.responseText.length > 0) {
                        var jsonObj = JSON.parse(response.responseText);
                        var imdbid = response.finalUrl.split('imdbid=')[1].split('&')[0];
                        var wikidataid = response.finalUrl.split('ids=')[1].split('&')[0];
                        var DT = '';
                        if (typeof Object.values(jsonObj.entities)[0].sitelinks.dewiki != 'undefined') { //wikipedia article
                            DT = Object.values(jsonObj.entities)[0].sitelinks.dewiki.title;
                            formattedWork = formattedWork.replace('[[' + wikidataid + '|', '[[' + imdbid + '#' + DT + '|');
                        } else if (typeof Object.values(jsonObj.entities)[0].labels.de != 'undefined') //wikidata label
                        {
                            DT = Object.values(jsonObj.entities)[0].labels.de.value;
                            formattedWork = formattedWork.replace('[[' + wikidataid + '|', imdbid + '#' + DT + '|');
                        } else //imdb title
                        {
                            formattedWork = formattedWork.replace('[[' + wikidataid + '|', imdbid + '#');
                        }
                        var OT = '';
                        if (typeof Object.values(jsonObj.entities)[0].claims.P1476 != 'undefined') { //check if OT of entity exists
                            OT = Object.values(jsonObj.entities)[0].claims.P1476[0].mainsnak.datavalue.value.text;
                            formattedWork = formattedWork.replace(imdbid + '#', OT + '#');
                        } else {
                            getDataFromIMDb(imdbid);
                        }
                    }
                },
                onerror: function(response) {
                    done++;
                    console.log('Error in fetching contents: ' + response.responseText);
                }

            });
        }

        function getDataFromIMDb(imdbid) {
            aufruf++;
            GM.xmlHttpRequest({
                method: 'GET',
                url: 'https://www.imdb.com/title/' + imdbid,
                onload: function(response) {
                    done++;
                    if (response.responseText.length > 0) {
                        var htmlText = response.responseText;
                        var imdbid = response.finalUrl.split('/')[4];
                        var OT = '';
                        if (htmlText.indexOf('originalTitle') != -1) { //check if OT exists in IMDb
                            OT = (/<div class="originalTitle">(.*?)<span/m).exec(htmlText)[1].replace(' - ', ' – ');
                        } else {
                            OT = (/<title>(.*?) \(/m).exec(htmlText)[1].replace(' - ', ' – ');
                        }
                        formattedWork = formattedWork.replace(imdbid + '#', OT + '#');
                    }
                },
                onerror: function(response) {
                    done++;
                    console.log('Error in fetching contents: ' + response.responseText);
                }

            });
        }
    }, 1000);
})();