JSON-LD from IMDb to QuickStatements

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

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

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