Advertisement
StephenLujan

rover image downloader

Jun 15th, 2022
3,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function loadScript(url, async = true, type = "text/javascript") {
  2.   return new Promise((resolve, reject) => {
  3.     try {
  4.       const scriptEle = document.createElement("script");
  5.       scriptEle.type = type;
  6.       scriptEle.async = async;
  7.       scriptEle.src = url;
  8.  
  9.       scriptEle.addEventListener("load", (ev) => {
  10.         resolve({ status: true });
  11.       });
  12.  
  13.       scriptEle.addEventListener("error", (ev) => {
  14.         reject({
  15.           status: false,
  16.           message: `Failed to load the script ${url}`,
  17.         });
  18.       });
  19.  
  20.       document.body.appendChild(scriptEle);
  21.     } catch (error) {
  22.       reject(error);
  23.     }
  24.   });
  25. }
  26.  
  27. function getPics() {
  28.   return new Promise((resolve, reject) => {
  29.     try {
  30.       let pics = new Set();
  31.       let rightButton = $("button.rsArrowRight");
  32.       let intervalId;
  33.       function check() {
  34.         let isLoading = $(".rsPreloader").length !== 0;
  35.         if (!isLoading) {
  36.           $("img.rsImg").each((i, elem) => pics.add(elem.src));
  37.           if (rightButton.hasClass("rsArrowDisabled")) {
  38.             clearInterval(intervalId);
  39.             resolve(pics);
  40.           } else {
  41.             rightButton.click();
  42.           }
  43.         }
  44.       }
  45.       check();
  46.       intervalId = setInterval(check, 50);
  47.     } catch (error) {
  48.       reject(error);
  49.     }
  50.   });
  51. }
  52.  
  53. const scripts = Promise.all(
  54.   [
  55.     "https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js",
  56.     "https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.0.2/jszip-utils.min.js",
  57.     "https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js",
  58.   ].map((x) => loadScript(x, false))
  59. ).then((values) => console.log("Scripts loaded"));
  60.  
  61. let pics = await getPics();
  62. console.log("pics loaded")
  63. await scripts;
  64.  
  65. function urlToPromise(url) {
  66.   return new Promise(function(resolve, reject) {
  67.       JSZipUtils.getBinaryContent(url, function (err, data) {
  68.           if(err) {
  69.               reject(err);
  70.           } else {
  71.               resolve(data);
  72.           }
  73.       });
  74.   });
  75. }
  76.  
  77. let zip = new JSZip();
  78.  
  79. pics.forEach((url, i)=> {
  80.   let filename = url.replace(/.*\/(\w*)\/(\w*)\/original.*/, "$1-$2.webp")
  81.   zip.file(filename, urlToPromise(url), { binary: true });
  82. });
  83. zip.generateAsync({ type: "blob" }).then((content) => saveAs(content, "RoverPictures.zip"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement