Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # keep2ShareUploader.sh
- version="2.014.07.04";
- ####################################################################################################
- # Uploads all files in a directory to Keep2Share
- #
- # Dependencies:
- # 1. jq - Command-line JSON processor
- # To install on Ubuntu:
- # cd ~; git clone https://github.com/stedolan/jq.git; cd jq; autoreconf -i; ./configure; make && make install;
- # 2. curl - client-side URL transfer library
- # To install on Ubuntu:
- # apt-get install libcurl3
- ####################################################################################################
- # CONFIGURATION SETTINGS #
- ####################################################################################################
- # Configure the following with your credentials between the " " marks.
- # user is in the form of the email address used for your Keep2Share account.
- user=""
- # Keep2Share password must contain only alphanumeric characters (No special characters: !@#$%^&*)
- password=""
- # In either the FileBoom, Keep2Share, MoneyPlatform.biz, or Publish2.me click tab entitled My Files.
- # Then open the Keep2Share folder you wish to upload into in a new tab.
- # The new tab's URL bar displays the 13 character folder_id value, such as: ..."?folder=abcdefghijklm"
- # Insert this 13 character folder_id between the "" marks below.
- folder="";
- # Enable/Disable scrambleChecksumHashValues (one of the following must be #commented out):
- scrambleChecksumHashValues="0"; # Enable md5 checksum hash scrambling of all files
- #scrambleChecksumHashValues="1"; # Disable md5 checksum hash scrambling of all files
- ####################################################################################################
- # SCRIPT #
- ####################################################################################################
- clear; yellow=$(tput setaf 3); green=$(tput setaf 2); red=$(tput setaf 1); cyan=$(tput setaf 6); textreset=$(tput sgr0); STARTTIME=$(date +%s);
- echo $cyan"################################################"${textreset};
- echo $cyan"# Bash File Upload For Keep2Share v"$version" #"${textreset};
- echo $cyan"################################################"${textreset};
- echo $cyan"***************************************************************************************************************"${textreset};
- url="http://keep2share.cc/api/v1/"
- tempError="outputCurl.txt"
- ####################################################################################################
- # Check if script is already running in directory or failed to exit gracefully on last run
- if [[ -f .keep2Share.tmp ]]; then
- echo $red"FAILURE! "${textreset}$cyan"Script is either currently running or did not exit gracefully on last run."${textreset};
- read -r -p $cyan"Would you like to clear the keep2ShareUploader.sh temp files and try again? (y/n):"${textreset} continue;
- continue=${continue,,}
- if [[ $continue =~ ^(yes|y| ) ]]; then
- clear;
- echo $cyan"Removing .keep2Share.tmp"${textreset};
- rm .keep2Share.tmp 2>/dev/null;
- echo $green"SUCCESS! "${textreset}$cyan"All keep2ShareUploader.sh temp files have been purged, resuming script now..."${textreset};
- echo $green"**************************************************************************************************************"${textreset};
- else
- exit 0;
- fi
- fi
- if [[ -f Keep2ShareLinks.txt ]]; then
- echo $yellow"WARNING! "${textreset}$cyan"The file Keep2ShareLinks.txt was found in this directory which indicates that this script has already"${textreset};
- echo $cyan"been run in this directory before."${textreset};
- echo $cyan"Would you like to:"${testreset};
- echo $cyan" i - Ignore this warning (Script will append existing Keep2ShareLinks.txt with any new links it creates."${textreset};
- echo $cyan" d - Delete the existing Keep2ShareLinks.txt file and continue to run this script."${textreset};
- echo $cyan" q - Quit this script altogether."${textreset};
- read -r -p $yellow"Select (i/d/q):"${textreset} userSelection;
- userSelection=${userSelection,,}
- if [[ $userSelection =~ ^(i| ) ]]; then
- echo $cyan"**************************************************************************************************************"${textreset};
- fi
- if [[ $userSelection =~ ^(d) ]]; then
- echo $cyan"Removing Keep2ShareLinks.txt"${textreset};
- rm Keep2ShareLinks.txt 2>/dev/null;
- echo $cyan"**************************************************************************************************************"${textreset};
- fi
- if [[ $userSelection =~ ^(q) ]]; then
- exit 0;
- fi
- fi
- ####################################################################################################
- # File Upload Process
- for file in ./*; do
- while [[ $uploadSuccess == "0" ]]; do
- if [[ $fileExtension == "txt" ]] || [[ $fileExtension == "png" ]] || [[ $fileExtension == "jpeg" ]] || [[ $fileExtension == "jpg" ]] || [[ $fileExtension == "sh" ]]; then
- echo $yellow"SKIPPED! "${textreset}$cyan"The following file has been skipped as the file's extension is either .txt, .png, .jpg, .jpeg, or .sh:"${textreset};
- echo $cyan"$fileName"${textreset};
- echo $cyan"***************************************************************************************************************"${textreset};
- uploadSuccess=1;
- fi
- if [[ $fileExtension != "txt" ]] && [[ $fileExtension != "png" ]] && [[ $fileExtension != "jpeg" ]] && [[ $fileExtension != "jpg" ]] && [[ $fileExtension != "sh" ]]; then
- # Check status from server response, if status isn't success script terminates.
- function checkStatus(){
- local json=$1
- local action=$2
- status=$(echo "$json" | jq -r ".status")
- if [ "$status" == "success" ]; then
- uploadCheckStatus="0";
- else
- uploadCheckStatus="1";
- fi
- }
- if [[ $uploadCheckStatus == "0" ]]; then
- :
- fi
- if [[ $uploadCheckStatus == "1" ]]; then
- echo -e $red"FAILURE! "${textreset}$cyan"Upload session failed to be created, re-attempting now."${textreset};
- echo "[message] :" $(echo "$json" | jq ".message")
- echo "[code] :" $(echo "$json" | jq ".code")
- echo "Curl execution output:"
- cat "$tempError"
- rm "$tempError"
- exit 1
- fi
- # Authentication into Keep2Share Account
- cmd="curl -d '{\"username\":\"$user\",\"password\":\"$password\"}' "$url"login 2> .keep2Share.tmp"
- # Saves Keep2Share's server response
- resLogin=`eval $cmd`
- # Look for an authorization status
- checkStatus "$resLogin" "Authentacation"
- # Get authorization token
- auth_token=$(echo "$resLogin" | jq -r ".auth_token")
- # Start the upload process
- fileExtension=`echo ${file: -4} | sed 's/.*[\.]//;'`;
- fileName=`ls $file | sed 's/\.\///'`;
- uploadSuccess="0";
- echo $cyan"Uploading $fileName..."${textreset};
- #Scrambles the md5 checksum hash values for the being uploaded (only if enabled in the configuration settings above)
- if [[ $scrambleChecksumHashValues == "0" ]]; then
- echo $RANDOM >> "$file";
- fi
- cmd="curl -d '{\"auth_token\":\"$auth_token\"}' "$url"GetUploadFormData 2>.keep2Share.tmp";
- resForm=`eval $cmd`;
- checkStatus "$resForm" "Getting form data";
- fileName=$file;
- form_action=$(echo "$resForm" | jq -r ".form_action");
- file_field=$(echo "$resForm" | jq -r ".file_field");
- nodeName=$(echo "$resForm" | jq -r ".form_data.nodeName");
- userId=$(echo "$resForm" | jq -r ".form_data.userId");
- expires=$(echo "$resForm" | jq -r ".form_data.expires");
- hmac=$(echo "$resForm" | jq -r ".form_data.hmac");
- api_request=$(echo "$resForm" | jq -r ".form_data.api_request");
- cmd="curl -F '$file_field=@$fileName' -F 'hmac=$hmac' -F 'expires=$expires' -F 'userId=$userId' -F 'nodeName=$nodeName' -F 'api_request=$api_request' -F 'parent_id=$folder' "$form_action" 2> .keep2Share.tmp";
- resUpload=`eval $cmd`;
- checkStatus "$resUpload" "Uploading file to server";
- fileName=`ls $file | sed 's/\.\///'`;
- fileSize=`ls -lha $file | awk {'print $5'} | sed 's/K/\ KB/;s/M/\ MB/;s/G/\ GB/;';`;
- k2sURL=`echo $resUpload | sed 's/.*user_file_id\"\:\"//;s/\".*//;s/^/http:\/\/keep2share\.cc\/file\//;'`;
- k2sFileUploadStatus="${#k2sURL}";
- echo $k2sFileUploadStatus;
- if [[ $k2sFileUploadStatus -eq "39" ]]; then
- echo $green"SUCCESS! "${textreset}$cyan$fileName" has been successfully uploaded:"${textreset};
- echo $cyan$k2sURL ${textreset};
- uploadSuccess="1";
- rm "$tempError" .keep2Share.tmp 2> /dev/null;
- fi
- if [[ $k2sFileUploadStatus -ne "39" ]]; then
- echo $red"FAILURE! "${textreset}$cyan$fileName" failed to upload, re-attempting in 15 seconds..."${textreset};
- uploadSuccess="0";
- sleep 15s;
- rm "$tempError" .keep2Share.tmp 2> /dev/null;
- fi
- echo $cyan"***************************************************************************************************************"${textreset};
- # Parse DDL, Filename, & Filesize into [BBCode]
- echo "[B]Keep2Share: [/B][URL="$k2sURL"]"$fileName" - "$fileSize"[/URL]" >> Keep2ShareLinks.txt;
- fi
- done;
- done;
- echo $cyan"***************************************************************************************************************"${textreset};
- cat Keep2ShareLinks.txt;
- ####################################################################################################
- #Counts the number of links generated and display with number of files in current directory.
- numFiles=`ls ./* | grep -v \.txt$ | grep -v \.png$ | grep -v \.jpg$ | grep -v \.jpeg$ | grep -v \.sh$ | wc -l`;
- if [[ -f Keep2ShareLinks.txt ]]; then
- numLinks=`cat ./Keep2ShareLinks.txt | wc -l`; ENDTIME=$(date +%s);
- fi
- if [[ ! -f Keep2ShareLinks.txt ]]; then
- numLinks=0;
- fi;
- echo "Number of files in this directory: "$numFiles;
- echo "Total # Keep2Share URLs generated: "$numLinks;
- echo "Number of seconds this script ran for: "$(($ENDTIME - $STARTTIME));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement