JSON-LD from IMDb to QuickStatements

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

当前为 2022-04-14 提交的版本,查看 最新版本

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