Coder Social home page Coder Social logo

toyhouse_downloader's Introduction

README

Project: ToyScrape

Main page for application

This app is a Gallery downloader for the character creating/sharing site Toyhouse. It uses a custom built API to fetch and then download galleries for characters.

Check it out here!

Usage

Using this downloader app is quite simple! You only need the link of the character that you wish to download the gallery of. After pasting it in the input, simply clicking "Download" will fetch all the images for the user and download them in a zip folder for you! The app also includes credits for each artists and important data for each image (tagged characters, description, etc.)!

Sample Links:

Since Toyhouse is currently an invite only app, sample links are necessary to test the app without an account. Here's a few:

Challenges

The main challenge that stumped me for a while was the way file-saver.js worked. As it simply waits for the fetch call to end rather than the blob object, it would download the zip file before actually getting all of the images for the character. This was a perfect opportunity to practice using Promises.

I scrapped the original code, and rewrote it to use Promise.all():

response.gallery.forEach(async (link, idx) => {
  // We iterate through the gallery 
   // and create a promise for each item
  
  const linkPromise = new Promise(async (resolve, reject) => { 
  
  let response = null;
  try {
    response = await fetch(link);
  } catch(err) {
    setHasError(err);
  }
  
  const blob = await response.blob();
  
  // Get datatype from image link
  let dataType = link.split(".")[3]
  if(dataType.length > 4) {
  
    // Sometimes, the link includes extra characters,
     // so we check to make sure we dont get the datatype wrong
    dataType = dataType.split("?")[0]
  }
    resolve({data: blob, type: dataType });
  });
  
// Push the promise to the promises array,
 // so that we can use it in Promise.all()
promises.push(linkPromise);

When all of the images resolve, we use Promise.all() to save the images to a zip folder.

// If we're at the last item,

if(idx === response.gallery.length - 1 ) {
  // Set a loading indicator,
  props.setLoading("Saving files...")
  // And call Promise.all()
  Promise.all(promises)
    .then(data => {
      // And we save each file to a zip
      data.forEach((blob, idx) => zip.file(`${idx}.${blob.type}`, blob.data))
    })
    .then(data => {
      props.setLoading(null);
      
      // And we finally generate our zip and download it.
      zip.generateAsync({type:"blob"})
        .then(content => {
          setQueryStr("");
          saveAs(content, `${response.name}-gallery.zip`)
      })
   })
 }

Screenshots

Error handling

Fetching gallery

Saving files

Technologies used

This app is written in React and plain Javascript for the fetch code and uses Bootstrap for styling. To save the files to memory, I used file-saver.js and JSZip to zip everything up and download it as a zip file. My own Toyhouse API was used to gather all the necessary data for the characters.

toyhouse_downloader's People

Contributors

erayalkis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.