IMDb Utility Library (API)

Utility library for the Internet Movie Database. Provides an API for grabbing info from IMDb.com

目前为 2019-09-14 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/390115/733070/IMDb%20Utility%20Library%20%28API%29.js

  1. // ==UserScript==
  2. // @name IMDb Utility Library (API)
  3. // @namespace driver8.net
  4. // @version 0.1
  5. // @description Utility library for the Internet Movie Database. Provides an API for grabbing info from IMDb.com
  6. // @author driver8
  7. // @match *://*.imdb.com/*
  8. // @grant GM_xmlhttpRequest
  9. // @connect imdb.com
  10. // ==/UserScript==
  11.  
  12. function getImdbIdFromTitle(title, year) {
  13. return new Promise(function(resolve, reject) {
  14. GM_xmlhttpRequest({
  15. method: 'GET',
  16. responseType: 'document',
  17. synchronous: false,
  18. url: 'https://www.imdb.com/find?s=tt&q=' + title,
  19. onload: (resp) => {
  20. const doc = document.implementation.createHTMLDocument().documentElement;
  21. doc.innerHTML = resp.responseText;
  22. console.log('doc', doc);
  23.  
  24. let links = Array.from(doc.querySelectorAll('.result_text > a'));
  25.  
  26. // Filter out TV episodes, shorts, and video games
  27. links = links.filter((el) => !el.parentNode.textContent.trim().match(/\((?:TV Episode|Short|Video Game|Video)\)/));
  28. let a = links[0];
  29. if (year) {
  30. console.log('year', year);
  31. let sorted = links.map((el) => {
  32. let m = el.parentNode.textContent.match(/\((\d{4})\)/);
  33. let year = new Date().getFullYear();
  34. if (m) {
  35. year = parseInt(m[1]);
  36. }
  37. return { el: el, year: year };
  38. });
  39. sorted = sorted.sort((a, b) => Math.abs(year - a.year) - Math.abs(year - b.year));
  40. a = sorted[0].el;
  41. }
  42.  
  43. let id = a && a.href.match(/title\/(tt\d+)/)[1];
  44. if (id) {
  45. resolve(id);
  46. } else {
  47. reject(`Error getting IMDb id for ${title} ${year}`);
  48. }
  49. }
  50. });
  51. });
  52. }
  53.  
  54. function getImdbInfoFromId(id) {
  55. return new Promise(function(resolve, reject) {
  56. GM_xmlhttpRequest({
  57. method: 'GET',
  58. responseType: 'document',
  59. synchronous: false,
  60. url: `https://www.imdb.com/title/${id}/`,
  61. onload: (resp) => {
  62. //console.log('resp', resp);
  63. const doc = document.implementation.createHTMLDocument().documentElement;
  64. doc.innerHTML = resp.responseText;
  65. console.log('doc', doc);
  66. const parse = function(query, regex) {
  67. try {
  68. let text = doc.querySelector(query).textContent.trim();
  69. //console.log('text', text);
  70. if (regex) {
  71. text = text.match(regex)[1];
  72. }
  73. return text.trim();
  74. } catch (e) {
  75. console.log('error', e);
  76. return '';
  77. }
  78. };
  79. let data = {
  80. id: id,
  81. title: parse('.title_wrapper > h1', /([^()]+)/),
  82. year: parse('.title_wrapper > h1', /[^()]+\s+\((\d+)\)/),
  83. description: parse('.plot_summary > .summary_text'),
  84. rating: parse('.ratingValue > strong > span'),
  85. votes: parse('.imdbRating > a > span'),
  86. metascore: parse('.metacriticScore > span'),
  87. popularity: parse('.titleReviewBarItem:last-of-type > .titleReviewBarSubItem > div > span', /^([0-9,]+)/),
  88. dateFetched: new Date()
  89. };
  90. if (data && data.id && data.title) {
  91. resolve(data);
  92. } else {
  93. reject('Error getting IMDb data for id ' + id);
  94. }
  95. }
  96. });
  97. });
  98. }
  99.  
  100. function getImdbInfoFromTitle(title, year) {
  101. return getImdbIdFromTitle(title, year).then((id) => {
  102. return getImdbInfoFromId(id);
  103. });
  104. }