IMDb List Helper

Makes creating IMDb lists more efficient and convenient

当前为 2017-12-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDb List Helper
  3. // @namespace imdb
  4. // @description Makes creating IMDb lists more efficient and convenient
  5. // @version 2.4.0
  6. // @include http://*imdb.com/list/*/edit
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. var jQuery = unsafeWindow.jQuery;
  12. var $ = jQuery;
  13.  
  14. //
  15. // CHANGELOG
  16. //
  17. // 2.4.0
  18. // bugfix: IMDb changed layout
  19. //
  20. // 2.3.0
  21. // bugfix: importing ratings works again
  22. //
  23. // 2.2.0
  24. // added: support for people
  25. //
  26. // 2.1.1
  27. // added: only show import form if ratings is selected
  28. //
  29. // 2.1
  30. // added: importers for imdb, rateyourmusic, criticker
  31. //
  32. // 2.0
  33. // added: import ratings
  34. // added: if regex doesn't match, skip entry
  35. //
  36. // 1.6.1.2
  37. // added: input text suggestion as a placeholder
  38. //
  39. // 1.6.1.1
  40. // fixed: some entries are skipped when adding imdb ids/urls
  41. //
  42.  
  43. //
  44. // milliseconds between each request
  45. //
  46. var REQUEST_DELAY = 1000;
  47.  
  48. function processImdb(file) {
  49. var csv_lines = file.split("\n");
  50. for(var i = 1; i < csv_lines.length; ++i) {
  51. try {
  52. var data = jQuery.csv.toArray(csv_lines[i]);
  53. var rating = data[8];
  54. if(rating === "") {
  55. continue;
  56. }
  57. $("#filmList").append(rating + "," + data[1] + "\n");
  58. }
  59. catch(e) {
  60. console.log("Exception: " + e);
  61. console.log("Bad line: " + csv_lines[i]);
  62. }
  63. }
  64. }
  65.  
  66. function processRym(file) {
  67. var csv_lines = file.split("\n");
  68. for(var i = 1; i < csv_lines.length; ++i) {
  69. try {
  70. var data = jQuery.csv.toArray(csv_lines[i]);
  71. if(data.length < 6) {
  72. continue;
  73. }
  74. $("#filmList").append(data[4] + "," + data[1] + "\n");
  75. }
  76. catch(e) {
  77. console.log("Exception: " + e);
  78. console.log("Bad line: " + csv_lines[i]);
  79. }
  80. }
  81. }
  82.  
  83. function processCriticker(file) {
  84. var lines = file.split("\n");
  85. for(var i = 1; i < lines.length; ++i) {
  86. var line = lines[i];
  87. var m = line.match(/([1-9]{1}|10)([0-9])?\t(.+)/);
  88. if(m === null) {
  89. continue;
  90. }
  91. var rating = m[1];
  92. var second = m[2];
  93. if(second === undefined) {
  94. // If second value is undefined the rating is < 10 (on criticker)
  95. // and will be changed to 1 because you can't rate 0 on IMDb
  96. rating = "1";
  97. }
  98. $("#filmList").append(rating + "," + m[3] + "\n");
  99. }
  100. }
  101.  
  102. function handleImport(evt) {
  103. var files = evt.target.files;
  104. var file = files[0];
  105. var reader = new FileReader();
  106. reader.onload = function(event){
  107. var file = event.target.result;
  108. var format = $("select[name=import]").val();
  109. if(format === "none") {
  110. alert("Select importer and try again.");
  111. return;
  112. }
  113. else if(format === "imdb") {
  114. processImdb(file);
  115. }
  116. else if(format === "rym") {
  117. processRym(file);
  118. }
  119. else if(format === "criticker") {
  120. processCriticker(file);
  121. }
  122. }
  123.  
  124. reader.readAsText(file);
  125. }
  126.  
  127. var ListManager = {
  128. regex: "^(.*)$",
  129. processRegex: function(rx, cb) {
  130. var filmTitle = rx[1];
  131. cb(filmTitle);
  132. },
  133. handleSelection: function(imdbId, cb) {
  134. cb();
  135. }
  136. };
  137.  
  138. var RatingManager = {
  139. rating: 0,
  140. regex: "^([1-9]{1}|10),(.*)$",
  141. processRegex: function(rx, cb) {
  142. RatingManager.rating = rx[1];
  143. var filmTitle = rx[2];
  144. cb(filmTitle);
  145. },
  146. handleSelection: function(imdbId, cb) {
  147. console.log("RatingManager::handleSelection: Rating " + imdbId);
  148. $.get("http://www.imdb.com/title/" + imdbId, function(data) {
  149. var authHash = $(data).find("#star-rating-widget").data("auth");
  150. var params = {tconst: imdbId, rating: RatingManager.rating,
  151. auth: authHash, tracking_tag: "list",
  152. pageId: imdbId, pageType: "title", subPageType: "main"};
  153. $.post("http://www.imdb.com/ratings/_ajax/title", params, function(data) {
  154. if(data.status !== 200) {
  155. alert("Rating failed. Status code " + data.status);
  156. }
  157. else {
  158. cb();
  159. }
  160. }, "json");
  161. });
  162. }
  163. };
  164.  
  165. var App = {
  166. manager: ListManager,
  167. films: [],
  168. regexObj: null,
  169. isEmpty: function() {
  170. return App.films.length === 0;
  171. },
  172. next: function() {
  173. return App.films.shift();
  174. },
  175. run: function() {
  176. GM_addStyle("#imdbListTextEdit { margin: 0 5% 5% 5%; } \
  177. #imdbListTextEdit input[type=text], #imdbListTextEdit textarea { width: 100%; } \
  178. #imdbListTextEdit textarea { background-color: lightyellow; }");
  179. var textEdit = '<div id="imdbListTextEdit" style="'
  180. + 'padding: 10px; border: 1px solid #e8e8e8">'
  181. + '<p><b>Import mode:</b><input type="radio" name="importmode" value="list" checked="checked">List</input>'
  182. + '<input type="radio" name="importmode" value="ratings">Ratings</input></p>'
  183. + '<textarea id="filmList" rows="7" cols="60" placeholder="Input titles or IMDb IDs and click Start"></textarea><br />'
  184. + '<input type="button" id="doList" value="Start" /> '
  185. + '<input type="button" id="skipFilm" value="Skip" /> '
  186. + '<input type="button" id="retryPost" value="Retry" /> '
  187. + '<span style="font-weight: bold">Remaining: <span id="filmsRemaining">0</span></span><br /><br />'
  188. + '<span style="font-weight: bold;">Current: <input type="text" id="filmCurrent" size="65" style="font-family: monospace" /></span><br />'
  189. + '<span style="font-weight: bold;">Regexp (matches only): <input type="text" value="" id="filmRegexp" size="65" style="font-family: monospace; margin-top: 4px; margin-left: 1px" /></span><br />'
  190. + '<p id="importform" style="display: none"><b>Import from:</b> <select name="import"><option value="none">Select</option><option value="imdb">IMDb</option>'
  191. + '<option value="rym">RateYourMusic</option><option value="criticker">Criticker</option></select><b> File: </b><input type="file" id="fileimport"></p>'
  192. + '</div>';
  193. $("div.lister-search").after(textEdit);
  194. $("#filmRegexp").val(App.manager.regex);
  195. $("#fileimport").on("change", handleImport);
  196. $("#imdbListTextEdit").on("change", "input[name=importmode]", function() {
  197. var value = $(this).val();
  198. if(value === "list") {
  199. App.manager = ListManager;
  200. $("#importform").hide();
  201. }
  202. else {
  203. App.manager = RatingManager;
  204. $("#importform").show();
  205. }
  206. $("#filmRegexp").val(App.manager.regex);
  207. });
  208. // When start button is clicked
  209. $("#imdbListTextEdit").on("click", "#doList", function(e) {
  210. $regexBox = $("#filmRegexp");
  211. if($regexBox.val()) {
  212. App.regexObj = RegExp($regexBox.val());
  213. }
  214. else {
  215. App.regexObj = RegExp(App.manager.regex);
  216. }
  217. // Disable the text area and the button and the regexp box
  218. // as well as the import mode
  219. $filmList = $("#filmList");
  220. $filmList.attr("disabled", "disabled");
  221. $regexBox.attr("disabled", "disabled");
  222. $("#doList").attr("disabled", "disabled");
  223. $("input[name=importmode]").attr("disabled", "disabled");
  224. App.films = $filmList.val().split("\n");
  225. App.handleNext();
  226. });
  227. // when skip button is clicked
  228. $("#imdbListTextEdit").on("click", "#skipFilm", function(e) {
  229. App.handleNext();
  230. });
  231. // Sometimes the request fails forcing the user to skip an entry to continue
  232. $("#imdbListTextEdit").on("click", "#retryPost", function(e) {
  233. $("#add-to-list-search").trigger("keydown");
  234. });
  235. },
  236. reset: function() {
  237. App.films = [];
  238. App.regexObj = null;
  239. $("#filmList").removeAttr("disabled");
  240. $("#filmRegexp").removeAttr("disabled"); // leave regex
  241. $("#doList").removeAttr("disabled");
  242. $("input[name=importmode]").removeAttr("disabled");
  243. $("#filmCurrent").val("");
  244. $("#add-to-list-search", "div.add").val("");
  245. //$("div.results", "div.add").html("");
  246. },
  247. search: function(filmTitle) {
  248. // remove unnecessary whitespace
  249. filmTitle = $.trim(filmTitle);
  250. // set current text to what we're searching
  251. $("#filmCurrent").val(filmTitle);
  252. // remove the first title from the text box and set the remaining number
  253. $filmList = $("#filmList");
  254. var newList = $filmList.val().split("\n");
  255. $("#filmsRemaining").text(newList.length-1);
  256. $filmList.val(newList.slice(1).join("\n"));
  257. // Run regex if it matches and let the manager process the result
  258. var result = App.regexObj.exec(filmTitle);
  259. if(result !== null) {
  260. App.manager.processRegex(result, function(filmTitle) {
  261. // Set imdb search input field to film title
  262. $("#add-to-list-search").val(filmTitle);
  263. // And perform search
  264. $("#add-to-list-search").trigger("keydown");
  265. });
  266. }
  267. else {
  268. App.handleNext();
  269. }
  270. },
  271. handleNext: function() {
  272. if(!App.isEmpty()) {
  273. // if there's more items, search next...
  274. App.search(App.next());
  275. }
  276. else {
  277. // if last film
  278. App.reset();
  279. }
  280. }
  281. };
  282.  
  283. $(document).ready(App.run);
  284.  
  285. // When a search result item is clicked by user or script
  286. $("#add-to-list-search-results").on("click", "a", function(e) {
  287. App.manager.handleSelection($(this).attr("id"), function() {
  288. // Some delay is needed
  289. setTimeout(function() {
  290. App.handleNext();
  291. }, REQUEST_DELAY);
  292. });
  293. });
  294.  
  295. // Monitors for changes to the search result box
  296. // If it recognizes an IMDBb URL/ID, it's clicked automatically
  297. // since there's only one result
  298. var clickId = null;
  299. $("#add-to-list-search-results").bind("DOMNodeInserted", function(e) {
  300. if($("#filmCurrent").val().match(/([CHMNTchmnt]{2}[0-9]{7})/) !== null && $("a", "#add-to-list-search-results").length) {
  301. // Some delay is needed for all results to appear
  302. clickId = setTimeout(function() {
  303. $("a", "#add-to-list-search-results").first()[0].click();
  304. clearTimeout(clickId);
  305. }, REQUEST_DELAY);
  306. }
  307. });