1fichier tools

Tools to manage 1fichier links easily

  1. // ==UserScript==
  2. // @name 1fichier tools
  3. // @namespace surrealmoviez.info
  4. // @description Tools to manage 1fichier links easily
  5. // @require http://code.jquery.com/jquery-1.11.1.min.js
  6. // @include http://www.1fichier.com/console/*
  7. // @include http://www.1fichier.com/*/console/*
  8. // @include http://1fichier.com/console/*
  9. // @include http://1fichier.com/*/console/*
  10. // @include https://www.1fichier.com/console/*
  11. // @include https://www.1fichier.com/*/console/*
  12. // @include https://1fichier.com/console/*
  13. // @include https://1fichier.com/*/console/*
  14. // @grant none
  15. // @version 1.2.3
  16. // ==/UserScript==
  17.  
  18. this.$ = this.jQuery = jQuery.noConflict(true);
  19.  
  20. // Creates an array of direct links
  21. function createArrayLinks() {
  22. var links = [];
  23. $("li.file.ext_gif.ui-selectee.ui-selected").each(function () {
  24. var temp_id = $(this).attr("rel");
  25. var id_start = temp_id.lastIndexOf("_") + 1;
  26. var id = "https://1fichier.com/?" + temp_id.substring(id_start);
  27. links.push(id);
  28. });
  29. return (links);
  30. }
  31.  
  32. // Retrieves all IDs of the folders in the current account as an array
  33. function getFoldersIds() {
  34.  
  35. var folders_droppable = ["0"];
  36. var folders_ids = ["0"];
  37. var temp_html = "";
  38.  
  39. while (folders_droppable.length !== 0) {
  40. $.ajax({
  41. type: "GET",
  42. url: "/console/dirs.pl?dir_id=" + folders_droppable[0] + "&map=0&start=0&fk=false",
  43. async: false,
  44. success: function (text) {
  45. temp_html = text;
  46. }
  47. });
  48.  
  49. $(temp_html).find('li').each(function () {
  50. if (!$(this).hasClass('root') && $("div > a", $(this)).text() !== "Ignore") {
  51. folders_ids.push($(this).attr('rel'));
  52. }
  53. if ($(this).hasClass('fcp')) {
  54. folders_droppable.push($(this).attr('rel'));
  55. }
  56. });
  57. folders_droppable.shift();
  58. }
  59.  
  60. return folders_ids;
  61. }
  62.  
  63. // Extracts the ID, query ID, name and upload date of all files in the current folder
  64. // Returns an array of [id, id_ref, date, name]. Date as yyyy-mm-dd
  65. function extractFoldersData(folder_ids) {
  66.  
  67. var collection = [];
  68.  
  69. for (var i = 0; i < folder_ids.length; i++) {
  70. $.ajax({
  71. type: "GET",
  72. url: "/console/files.pl?dir_id=" + folder_ids[i] + "&oby=",
  73. async: false,
  74. success: function (data) {
  75. $("li.file", $(data)).each(function () {
  76. var current = [];
  77. var temp_id = $(this).attr("rel");
  78. current[0] = temp_id.substring(temp_id.lastIndexOf("_") + 1);
  79. current[1] = "selected%5B%5D=" + temp_id;
  80. current[2] = formatDate($(this).find("div.dD").text());
  81. current[3] = $(this).find("div.dF").text();
  82. collection.push(current);
  83. });
  84. },
  85. error: function (request, status, error) {
  86. console.log("Error in extractFoldersData()\nStatus: " + status + ", error: " + error + "\n" + request.responseText);
  87. }
  88. });
  89. }
  90.  
  91. return (collection);
  92. }
  93.  
  94. // Groups the ref_ids to make the info requests
  95. // Returns an array of querries
  96. function createQueries(folders_data) {
  97.  
  98. var array_id_ref = [];
  99. for (var i = 0; i < folders_data.length; i++) {
  100. array_id_ref.push(folders_data[i][1]);
  101. }
  102. var array_queries = []; // Array containing all IDs in the current folder
  103. var size_split = 150; // Number of files to send in each info request
  104.  
  105. while (array_id_ref.length !== 0) {
  106. var current = array_id_ref.splice(0, size_split);
  107. array_queries.push(current.join("&"));
  108. }
  109.  
  110. return (array_queries);
  111. }
  112.  
  113. // Returns the id, total downloads and last download date of all files in the account
  114. // Returns an array with [id, total_downloads, date_last_download]. Date as yyyy-mm-dd
  115. function retrieveInfo(array_queries) {
  116.  
  117. var files_info = [];
  118.  
  119. for (var i = 0; i < array_queries.length; i++) {
  120. $.ajax({
  121. method: "POST",
  122. async: false,
  123. url: "/console/info.pl",
  124. data: array_queries[i],
  125. success: function (data) {
  126. files_info = files_info.concat(extractStats(data));
  127. }
  128. });
  129. }
  130.  
  131. return files_info;
  132.  
  133. }
  134.  
  135. // Extracts the info of the curent query
  136. // Returns an array of [id, total_downloads, date_last_download]. Date as yyyy-mm-dd
  137. function extractStats(data) {
  138.  
  139. var info = [];
  140. $("tr td:has(a):first-child", $(data)).each(function () {
  141. var id = $("a", $(this)).attr("href");
  142. id = id.substring(id.lastIndexOf('?') + 1);
  143. var total_downloads, date_last_download;
  144. var $nextTr = $(this).parent().next("tr");
  145. // Check if a "not downloaded yet" message is present
  146. if ($("td", $nextTr).length === 1) {
  147. total_downloads = 0;
  148. date_last_download = "NO_DOWNLOADS";
  149. } else {
  150. var $lastDownloadTr = $(this).parent().nextUntil('tr:not(.t,.t1)').last();
  151. var $totalTr = $lastDownloadTr.next();
  152. total_downloads = $('td:eq(2)', $totalTr).text();
  153. date_last_download = $('td:eq(1) > a', $lastDownloadTr).text();
  154. }
  155. info.push([id, total_downloads, date_last_download]);
  156. });
  157.  
  158. return info;
  159.  
  160. }
  161.  
  162. // Returns a date in the format yyyy-mm-dd
  163. function formatDate(uglyDate) {
  164. var arr_date = uglyDate.split("/");
  165. if (arr_date.length === 3) {
  166. arr_date[2] = arr_date[2].substring(0, arr_date[2].indexOf(" "));
  167. var niceDate = arr_date[2] + "-" + arr_date[1] + "-" + arr_date[0];
  168. } else {
  169. console.log("Error parsing date: " + uglyDate);
  170. niceDate = uglyDate;
  171. }
  172.  
  173. return (niceDate);
  174. }
  175.  
  176. // Checks if the link weren't downloaded since a given ammount of days
  177. // Returns an array with the file name and the download link as a string, containing only the endangered ones
  178. function checkEndangered(date_last_download, milliseconds_now) {
  179.  
  180. var danger_time = 45; // days since the last download. Files are stored for 90 days
  181. danger_time = danger_time * 86400000; // days to milliseconds
  182. var milliseconds_last_download = Date.parse(date_last_download);
  183.  
  184. if (milliseconds_now - milliseconds_last_download > danger_time) {
  185. return true;
  186. }
  187.  
  188. return false;
  189. }
  190.  
  191. // Creates a table containing all file names and time since the last download
  192. // Displayed in a new window
  193. function createExtensiveTable(complete_info) {
  194. //[id, total_downloads, date_last_download, name]
  195.  
  196. complete_info = sortArrayOfArrays(complete_info, 3);
  197.  
  198. var page_beginning = "<html><head><title>Files Summary</title></head><body>"
  199. + "<table align='center' border='0' width='80%' cellpadding='1' cellspacing='0'>"
  200. + "<tr style='background-color:#484848; color:#EAE9E9'><th>File name</th><th>Link</th><th>Total downloads</th><th>Time since last download</th>";
  201. var page_end = "</table></body></html>";
  202. var table = "";
  203.  
  204. var els = complete_info.length;
  205. var milliseconds_date_now = Date.now();
  206. var row_colour = "#F8F7F7";
  207. var grand_total = 0;
  208.  
  209. for (var i = 0; i < els; i++) {
  210. var milliseconds_last_download = milliseconds_date_now - Date.parse(complete_info[i][2]);
  211. var real_time_last_download = convertMS(milliseconds_last_download);
  212. table += "<tr style='background-color:" + row_colour + "'><td>" + complete_info[i][3] + "</td><td>http://" + complete_info[i][0] + ".1fichier.com/</td><td>" + complete_info[i][1] + "</td><td>" + real_time_last_download + "</td></tr>";
  213.  
  214. if (i % 2 === 0)
  215. row_colour = "#DAD9D9";
  216. else
  217. row_colour = "#F8F7F7";
  218.  
  219. grand_total += parseInt(complete_info[i][1]);
  220. }
  221.  
  222. table += "<tr style='background-color:" + row_colour + "'><td></td><td></td><td><b>" + grand_total + "</b></td><td></td></tr>";
  223.  
  224. var page_table = page_beginning + table + page_end;
  225.  
  226. var j = window.open('', '_blank');
  227. j.document.write(page_table);
  228. j.stop();
  229.  
  230. }
  231.  
  232. // Converts milliseconds to a string formatted as "X days Y hours"
  233. function convertMS(ms) {
  234. var d, h, m, s;
  235. s = Math.floor(ms / 1000);
  236. m = Math.floor(s / 60);
  237. s = s % 60;
  238. h = Math.floor(m / 60);
  239. m = m % 60;
  240. d = Math.floor(h / 24);
  241. h = h % 24;
  242. return (" " + d + " days " + h + " hours ");
  243. }
  244.  
  245. function sortArrayOfArrays(array, index) {
  246. var sortedArray = array.sort(function (a, b) {
  247. return a[index] > b[index] ? 1 : -1;
  248. });
  249.  
  250. return sortedArray;
  251. }
  252.  
  253. function dogbert() {
  254.  
  255. $("#transparentLayerWaiting").show();
  256. $("#containerWaiting").show();
  257.  
  258. var now = Date.now();
  259. var list_endangered_links = [];
  260.  
  261. var folder_ids = getFoldersIds();
  262. var folders_data = extractFoldersData(folder_ids);
  263. var array_queries = createQueries(folders_data);
  264. var files_info = retrieveInfo(array_queries);
  265.  
  266. if (folders_data.length !== files_info.length) {
  267. alert("Something failed! " + folders_data.length + " files were detected in the folders but " + files_info.length + " were processed for information.\nPlease try again and inform this error if it persists over days.");
  268. } else {
  269. folders_data = sortArrayOfArrays(folders_data, 0);
  270. files_info = sortArrayOfArrays(files_info, 0);
  271.  
  272. for (var i = 0; i < files_info.length; i++) {
  273. if (folders_data[i][0] === files_info[i][0]) {
  274. if (files_info[i][2] === "NO_DOWNLOADS") {
  275. files_info[i][2] = folders_data[i][2];
  276. }
  277. if (checkEndangered(files_info[i][2], now)) {
  278. list_endangered_links.push(folders_data[i]);
  279. }
  280. } else {
  281. alert("The IDs of an element didn't match, the process wasn't accurate any more and was stopped.\nPlease reload the page, try again and report this error if the problem persists.");
  282. break;
  283. }
  284. }
  285. }
  286.  
  287. var complete_info = [];
  288. for (var i = 0; i < folders_data.length; i++) {
  289. complete_info.push([files_info[i][0], files_info[i][1], files_info[i][2], folders_data[i][3]]);
  290. }
  291.  
  292. if (list_endangered_links.length === 0) {
  293. $("#clean_links").val("No links dying in the next 2 weeks found :)");
  294. $('#message_links').html("<br>" + files_info.length + " link(s) in " + folder_ids.length + " folder(s) checked<br>Click <a href='#' id='ext_table' style='color:#E6E6E6; text-decoration:underline; font-weight: bold;'><b>here</b></a> for a detailed list (opens in a new window)");
  295. } else {
  296. var readeable_endangered_links = [];
  297. for (var i = 0; i < list_endangered_links.length; i++) {
  298. readeable_endangered_links.push(list_endangered_links[i][3] + " - http://" + list_endangered_links[i][0] + ".1fichier.com/");
  299. }
  300. readeable_endangered_links.sort(function (a, b) {
  301. return a.localeCompare(b);
  302. });
  303. $("#clean_links").val("Dying in 2 weeks or less:\n\n" + readeable_endangered_links.join("\n"));
  304. $('#message_links').html("<br>" + list_endangered_links.length + " endangered link(s) found<br>" + files_info.length + " link(s) in " + folder_ids.length + " folder(s) checked<br>Click <a href='#' id='ext_table' style='color:#E6E6E6; text-decoration:underline; font-weight: bold;'><b>here</b></a> for a detailed list (opens in a new window)");
  305. }
  306.  
  307. $('#ext_table').click(function (event) {
  308. event = event || window.event;
  309. createExtensiveTable(complete_info);
  310. return false;
  311. });
  312.  
  313. $("#transparentLayer").show();
  314. $("#container").show();
  315. $("#transparentLayerWaiting").hide();
  316. $("#containerWaiting").hide();
  317. list_endangered_links = "";
  318. $("#clean_links").select();
  319. }
  320.  
  321. // New elements for the page
  322. {
  323. var img_endangered = '<div id="div_endangered" style="position: fixed; bottom: 0px; left: 0px; z-index: 3000">'
  324. + '<img id="img_endangered" src="http://i.imgur.com/x3CC8ht.png" alt="Retrieve endangered links" title="Retrieve endangered links">'
  325. + '</div>';
  326.  
  327. var winHeight = $(window).height();
  328. var winWidth = $(window).width();
  329. var marginTop = Math.floor((winHeight - 400) / 2);
  330. var marginLeft = Math.floor((winWidth - 470) / 2);
  331. var marginTopW = Math.floor((winHeight - 200) / 2);
  332. var marginLeftW = Math.floor((winWidth - 200) / 2);
  333.  
  334. var div_links = '<div id="transparentLayer" style="display: none; opacity: 0.9; position: fixed; top: 0px; left: 0px; right: 0px; bottom: 0px; background-color: black; z-index: 5001;"></div>'
  335. + '<div id="container" style="display: none; position: fixed; top: ' + marginTop + 'px; left: ' + marginLeft + 'px; z-index: 6001">'
  336. + '<textarea id="clean_links" style="width:450px; height:300px; background-color:#D1C5C5; float:top;"></textarea>'
  337. + '<div id="message_links" style="width:450px; height:120px; float:bottom; color:silver;"></div>'
  338. + '</div>';
  339.  
  340. var div_waiting = '<div id="transparentLayerWaiting" style="display: none; opacity: 0.9; position: fixed; top: 0px; left: 0px; right: 0px; bottom: 0px; background-color: black; z-index: 3001;"></div>'
  341. + '<div id="containerWaiting" style="display: none; position: fixed; top: ' + marginTopW + 'px; left: ' + marginLeftW + 'px; z-index: 4001">'
  342. + '<img src="http://i.imgur.com/uQqkT4p.png" title="absolutely">'
  343. + '</div>';
  344.  
  345. $("body").append(div_links + img_endangered + div_waiting);
  346.  
  347. var interval = window.setInterval(function () {
  348. if ($('#dirTree > .jqFT').length === 1 && $('#fileTree #sable').length === 1) {
  349. $('#dirTree').append('<textarea id="links-display" style="bottom: 10px; height: 150px; width: 95%; margin-top: 15px;"></textarea>');
  350. $('#links-display').click(function () {
  351. this.select();
  352. });
  353. $('#dirTree, #fileTree').bind('mousedown.namespace', function (e) {
  354. $(document).one('mouseup click', function () {
  355. $('#links-display').val(createArrayLinks().join("\n"));
  356. $(document).unbind('mousedown.namespace');
  357. $('#links-display').select();
  358. });
  359. });
  360. window.clearInterval(interval);
  361. }
  362. }, 500);
  363. }
  364.  
  365. // Show/hide containers method, Wally's and Show links functions setters
  366. {
  367. $(document).mouseup(function (e) {
  368. var container_sel = $("#container");
  369. var container_tohide1 = $("#transparentLayer");
  370. var container_tohide2 = $("#container");
  371. if (container_sel.has(e.target).length === 0) {
  372. container_tohide1.hide();
  373. container_tohide2.hide();
  374. }
  375. });
  376. }
  377.  
  378. // Setter for Dogbert's function
  379. $('#img_endangered').click(function () {
  380. dogbert();
  381. });