IMDb List Helper

Makes creating IMDb lists more efficient and convenient

当前为 2016-01-22 提交的版本,查看 最新版本

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