JSON-LD from IMDb to QuickStatements

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

当前为 2021-10-19 提交的版本,查看 最新版本

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