JSON-LD from IMDb to QuickStatements

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

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