Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # This is modified from the original at: http://pastebin.com/8zqRpKkY
- clear;
- DIR="$HOME/Pictures/4ch"; # Base download dir for pictures
- DELAY=10; # Number of seconds to wait before checking for new posts
- URL="";
- THREADS=8;
- ARGS=$#;
- THIS=`basename $0`;
- # Exit codes for wget
- WEXITS[0]="No problems occurred.";
- WEXITS[1]="Generic error code.";
- WEXITS[2]="Parse error -- for instance, when parsing command-line options, the .wgetrc or .netrc …";
- WEXITS[3]="File I/O error.";
- WEXITS[4]="Network failure.";
- WEXITS[5]="SSL verification failure.";
- WEXITS[6]="Username/password authentication failure.";
- WEXITS[7]="Protocol errors.";
- WEXITS[8]="Server issued an error response.";
- WEXITS[9]="Unknown error code.";
- ##
- # Checks to see if the input is an integer
- # Returns 1 if it is, and a 0 if it isn't
- ##
- is_int ()
- {
- expr $1 % 1 &> /dev/null;
- isint=$?;
- if [[ "$isint" -lt "2" ]]; then
- return 1;
- else
- return 0;
- fi
- }
- ##
- # Prints out the command help and exits
- ##
- usage ()
- {
- tput bold;
- echo "NAME";
- tput sgr0;
- echo " $THIS -- Download images from a 4chan thread...";
- tput bold;
- echo "SYNPOSIS";
- tput sgr0;
- echo " $THIS [DEST] [THREADS] [DELAY] <URL>\n";
- tput bold;
- echo "DESCRIPTION";
- tput sgr0;
- # using a variable for the description paragraph to make it easier to format..
- # I hate horozontal scrolling when I code.
- DESC="$THIS will download all images (\"THREADS\" simultaneously) from a post on 4chan at the URL you provide, and save them to \"DEST\", creating new folders as necessary. After it downloads the current images it will wait \"DELAY\" seconds and check for new images. If there are new images, it will grab them.";
- echo " $DESC" | fmt -s 66 66;
- echo "\n $THIS will stop when you Ctrl+C or it receives a 404 from 4chan.
- DEFAULTS:
- DEST: $DIR/<post number>/
- DELAY: $DELAY
- THREADS: $THREADS\n";
- echo " NOTES:
- Only URL is required unless you specify [THREADS] at which point you must also specify a [DELAY], and if you specify a [DEST] you also have to specify [THREADS] AND [DELAY]." | fmt -s 66 66;
- tput bold;
- echo "EXAMPLES";
- tput sgr0;
- echo " Download from URL with default options…
- `basename $0` http://boards.4chan.org/b/res/359372915
- Download from URL with a 15s delay before checking for new images…
- `basename $0` 15 http://boards.4chan.org/b/res/359372915
- Download from URL using 8 threads, with a 15s delay before checking for new images…
- `basename $0` 8 15 http://boards.4chan.org/b/res/359372915
- Download from URL using 8 threads, with a 15s delay before checking for new images. Saves the images in /Users/John/4chan…
- `basename $0` \"/Users/John/4chan\" 8 15 http://boards.4chan.org/b/res/359372915";
- exit 1;
- }
- ##
- # Check for the proper number of args, and returns the usage summary if an incorrect number is provided.
- ##
- if [ "$ARGS" -lt 1 ] || [ "$ARGS" -gt 4 ]; then
- usage;
- fi
- ##
- # Set the correct arg to the correct var based on # of args provided…
- ##
- if [ "$ARGS" -eq 1 ]; then
- URL="$1";
- elif [ "$ARGS" -eq 2 ]; then
- URL="$2";
- DELAY="$1";
- elif [ "$ARGS" -eq 3 ]; then
- URL="$3";
- DELAY="$2";
- THREADS="$1";
- elif [ "$ARGS" -eq 4 ]; then
- URL="$4";
- DELAY="$3";
- THREADS="$2";
- DIR="$1";
- else
- usage;
- fi
- `is_int $DELAY`;
- delint=$?;
- `is_int $THREADS`;
- thrint=$?;
- if [[ "$delint" -ne "1" ]] || [[ "$thrint" -ne "1" ]]; then
- echo "\nDELAY and THREADS must both be integers!\n";
- usage;
- fi
- ##
- # Don't want no negative delays…
- ##
- if [ "$DELAY" -lt 0 ]; then
- DELAY=0;
- fi
- ##
- # Want at least one thread right?
- ##
- if [ "$THREADS" -lt 1 ]; then
- THREADS=1;
- fi
- SUBDIR=$( echo "$URL" | egrep -o '([0-9]*)$' | sed 's/\.html//g' );
- LOC="$DIR/$SUBDIR";
- LOGFILE="$LOC/log.txt";
- if [ ! -d $LOC ]; then
- mkdir -p $LOC;
- fi
- cd $LOC;
- while [ true ]; do
- # Fuck you clutter!
- clear;
- # Lets show some user-friendly info
- echo "4Chan Image Downloader
- URL: $URL
- DELAY: $DELAY
- THREADS: $THREADS
- DEST: $LOC
- I'll download all images ($THREADS at a time) from $URL and save them to $LOC.
- I'll pause for $DELAY seconds before checking for new images.
- I'll stop when I see a CTRL+C from you, or a 404 from 4Chan.\n\n";
- TMP=`mktemp /tmp/4chan.XXXXXX`; # Holds the webpage
- TMP2=`mktemp /tmp/4chanm.XXXXXX`; # Holds the links to all the images in the post
- TMP3=`mktemp /tmp/4chanc.XXXXXX`; # Holds the links to all the images you DON'T have
- WGET_CMD="wget -nv -nc -a $LOGFILE"; # holds the command+args for the 2nd wget, to pull an image
- echo "Grabbing the post at: $URL\n";
- wget -q -O "$TMP" "$URL";
- WEXIT=$?; # so we can check the exit status of wget for that 404
- # testing for an error from wget
- if [ $WEXIT -ne 0 ]; then
- # remove the temp files we created
- rm $TMP $TMP2;
- # wget exits with an 8 if the server
- # sent an error response code .. like a 404 maybe? ;-)
- if [ $WEXIT -eq 8 ]
- then
- echo "There's that 404 we're waiting for ... lets make like a tree and get out of here!";
- else
- if [ $WEXIT -gt 8 ]; then
- WEXIT=9;
- fi
- echo "Couldn't get it up ... sorry. Here's the error we're exiting with:\nERROR: ${WEXITS[$WEXIT]} ";
- fi
- exit $WEXIT;
- fi
- ##
- # Strip all the unique image URLs from the page and put them in TMP2
- ##
- egrep 'http://images.4chan.org/[a-z0-9]+/src/([0-9]*).(jpg|jpeg|png|gif)' "$TMP" -o | uniq > "$TMP2";
- ##
- # Check to see which ones are already downloaded and put "new" ones in TMP3
- ##
- echo "Looking at the images you have .. fap fap fap..\n";
- for file in `cat $TMP2`
- do
- fpath="$LOC/`echo $file | egrep -o '([0-9]*).(jpg|jpeg|png|gif)'`";
- if [ ! -e "$fpath" ]; then
- echo $file >> "$TMP3";
- fi
- done
- totalImages=`cat $TMP2 | wc -l | tr -d ' '`;
- newImages=`cat $TMP3 | wc -l | tr -d ' '`;
- imageDiff=$(( totalImages - newImages ));
- echo "You have $imageDiff of $totalImages images in this post.";
- if [[ "$newImages" -gt "0" ]]; then
- echo "Downloading $newImages images...";
- cat $TMP3 | xargs -P $THREADS -I _URL_ $WGET_CMD _URL_;
- SAVED=`cat $LOGFILE | wc -l | tr -d ' '`;
- echo "($SAVED/$newImages) images were successfully saved.\n";
- rm $LOGFILE;
- else
- echo "There are no new images to download.\n";
- fi
- fapTime=3;
- if (( ("$DELAY" - "$fapTime") < 0 )); then
- realFaptime=0;
- fapTime="$DELAY";
- else
- realFaptime=$(( DELAY - fapTime ));
- fi
- echo "I'm going to fap to those images.\nDon't worry, it only takes me $realFaptime seconds.\n";
- sleep $realFaptime;
- echo "Ahhhh… Hold on; lemme clean up my mess.";
- rm $TMP $TMP2 $TMP3;
- sleep $fapTime;
- done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement