JSON-LD from IMDb to QuickStatements

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

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

  1. // ==UserScript==
  2. // @name JSON-LD from IMDb to QuickStatements
  3. // @author CennoxX
  4. // @description Get data from JSON-LD from IMDb to QuickStatements, to publish it on Wikidata
  5. // @match https://www.imdb.com/*
  6. // @match https://tools.wmflabs.org/quickstatements/*
  7. // @contact cesar.bernard@gmx.de
  8. // @namespace https://greasyfork.org/users/21515
  9. // @version 0.6
  10. // @grant GM.xmlHttpRequest
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @license MIT
  14. // ==/UserScript==
  15. (function() {
  16. 'use strict';
  17. //
  18. //QuickStatements
  19. //
  20. if (location.href == 'https://tools.wmflabs.org/quickstatements/#/batch') {
  21. var quickstatements = '';
  22. var checkForChanges = setInterval(function() {
  23. if (quickstatements) {
  24. var quickForm = document.querySelector('textarea.form-control');
  25. if (!quickForm.innerHTML.includes(quickstatements)){
  26. quickForm.innerHTML += quickstatements;
  27. }
  28. GM_setValue('quickstatements','');
  29. quickstatements = '';
  30. }else{
  31. quickstatements = GM_getValue('quickstatements');
  32. }
  33. }, 250);
  34. //
  35. //IMDb
  36. //
  37. }else if (location.host == "www.imdb.com"){
  38. var request = 0;
  39. var done = 0;
  40. var i = 0;
  41. var val = [];
  42. var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
  43.  
  44. //item
  45. var item = '';
  46. getWikidataId(jsonld,'item');
  47.  
  48. //startTime/publication date
  49. if (jsonld['@type'] == 'TVSeries') {
  50. pushQSString('P580', jsonld.datePublished);
  51. } else if (jsonld['@type'] == 'Movie' || jsonld['@type'] == 'TVEpisode') {
  52. pushQSString('P577', jsonld.datePublished);
  53. }
  54.  
  55. //actor
  56. if (jsonld.actor) {
  57. for (i = 0; i < jsonld.actor.length; i++) {
  58. getWikidataId(jsonld.actor[i],'P161');
  59. }
  60. }
  61.  
  62. //creator/writer
  63. if (jsonld.creator) {
  64. for (i = 0; i < 4; i++) {
  65. if (jsonld.creator[i] && jsonld.creator[i].name) {
  66. if (jsonld['@type'] == 'TVSeries') {
  67. getWikidataId(jsonld.creator[i],'P170');
  68. } else if (jsonld['@type'] == 'Movie') {
  69. getWikidataId(jsonld.creator[i],'P58');
  70. }
  71. } else {
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. //director
  78. if (jsonld.director) {
  79. for (i = 0; i < jsonld.director.length; i++) {
  80. getWikidataId(jsonld.director[i], 'P57');
  81. }
  82. }
  83.  
  84. //birthdate
  85. pushQSString('P569', jsonld.birthDate);
  86.  
  87. //deathdate
  88. pushQSString('P570', jsonld.deathDate);
  89.  
  90. //loop to check if ready to set data
  91. var checkIfComplete = setInterval(function() {
  92. if (request != 0 && (done/request) == 1 && GM_getValue('quickstatements')=='') {
  93. var tempQ = '';
  94. val.sort().forEach(function(entry) {
  95. tempQ += item +entry;
  96. });
  97. if (tempQ && item){
  98. GM_setValue('quickstatements',tempQ);
  99. }
  100. clearInterval(checkIfComplete);
  101. }
  102. }, 500);
  103.  
  104. function getWikidataId(id,prop) {
  105. request++;
  106. GM.xmlHttpRequest({
  107. method: "GET",
  108. url: "https://www.wikidata.org/w/api.php?action=query&format=json&list=search&srsearch=haswbstatement:P345=" + id.url.split('/')[2] + "&type=" + prop,
  109. onload: function(response) {
  110. done++;
  111. if (response.responseText.length > 0) {
  112. var jsonObj = JSON.parse(response.responseText);
  113. if (jsonObj.query.search[0] != null) {
  114. var qid = jsonObj.query.search[0].title;
  115. var property = response.finalUrl.split('type=')[1].split('&')[0];
  116. if (property == "item"){
  117. item = qid;
  118. } else {
  119. pushQSString(property,qid);
  120. }
  121. }
  122. }
  123. },
  124. onerror: function(response) {
  125. done++;
  126. console.log("Error in fetching contents: " + response.responseText);
  127. }
  128.  
  129. });
  130. }
  131.  
  132. function pushQSString(property, data) {
  133. if (data){
  134. val.push('|' + property + '|' + (!isNaN(Date.parse(data))? '+'+data+'T00:00:00Z/11':data) + '|S248|Q37312|S345|"'+location.href.split('/')[4]+'"|S813|+'+new Date().toISOString().substring(0, 11)+'00:00:00Z/11\n');
  135. }
  136. }
  137. }
  138. })();