Greasy Fork is available in English.

IMDb Utility Library (API)

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

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/390115/733074/IMDb%20Utility%20Library%20%28API%29.js

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