Advertisement
Guest User

fileBoomUploader.sh

a guest
Jul 4th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.16 KB | None | 0 0
  1. #!/bin/bash
  2. # fileBoomUploader.sh
  3. version="2.014.07.04";
  4. ####################################################################################################
  5. # Uploads all files in a directory to FileBoom
  6. #
  7. # Dependencies:
  8. # 1. jq - Command-line JSON processor
  9. #    To install on Ubuntu:
  10. #      cd ~; git clone https://github.com/stedolan/jq.git; cd jq; autoreconf -i; ./configure; \
  11. #      make && make install;
  12. # 2. curl - client-side URL transfer library
  13. #    To install on Ubuntu:
  14. #      apt-get install libcurl3
  15. ####################################################################################################
  16. #                                      CONFIGURATION SETTINGS                                      #
  17. ####################################################################################################
  18. # Configure the following with your credentials between the " " marks.
  19. # user is in the form of the email address used for your FileBoom account.
  20. user=""
  21.  
  22. # FileBoom password must contain only alphanumeric characters (No special characters: !@#$%^&*)
  23. password=""
  24.  
  25. # In either the FileBoom, Keep2Share,MoneyPlatform.biz, or Publish2.me click tab entitled My Files.
  26. # Then open the FileBoom folder you wish to upload into in a new tab.
  27. # The new tab's URL bar displays the 13 character folder_id value, such as: ..."?folder=abcdefghijklm"
  28. # Insert this 13 character folder_id between the "" marks below.
  29. folder="";
  30.  
  31. # Enable/Disable scrambleChecksumHashValues (one of the following must be #commented out):
  32. scrambleChecksumHashValues="0"; # Enable md5 checksum hash scrambling of all files
  33. #scrambleChecksumHashValues="1"; # Disable md5 checksum hash scrambling of all files
  34.  
  35. # Specify the number of attempts to try to re-upload files if they fail to upload. (default=5)
  36. uploadRetryLimit=5;
  37. ####################################################################################################
  38. #                                              SCRIPT                                              #
  39. ####################################################################################################
  40. clear; yellow=$(tput setaf 3); green=$(tput setaf 2); red=$(tput setaf 1); cyan=$(tput setaf 6); textreset=$(tput sgr0); STARTTIME=$(date +%s);
  41. echo $cyan"################################################"${textreset};
  42. echo $cyan"# Bash File Upload For FileBoom v"$version" #"${textreset};
  43. echo $cyan"################################################"${textreset};
  44. echo $cyan"***********************************************************************************************************************************************************************************************"${textreset};
  45. url="http://keep2share.cc/api/v1/"
  46. ####################################################################################################
  47. # Check if script is already running in directory or failed to exit gracefully on last run
  48. if [[ -f FileBoomLinks.txt ]] || [[ -f .fileBoom.tmp ]]; then
  49.   echo $yellow"WARNING! "${textreset}$cyan"The file FileBoomLinks.txt and/or .fileBoom.tmp have been found in this directory which indicates that this script has"${textreset};
  50.   echo $cyan"either been run in this directory before or previously failed to complete running."${textreset};
  51.   echo $cyan"Would you like to:"${testreset};
  52.   echo $cyan" i - Ignore this warning (Script will append existing FileBoomLinks.txt with any new links it creates."${textreset};
  53.   echo $cyan" d - Delete the existing FileBoomLinks.txt/.fileBoom.tmp file and continue to run this script."${textreset};
  54.   echo $cyan" q - Quit this script altogether."${textreset};
  55.   read -r -p $yellow"Select (i/d/q):"${textreset} userSelection;
  56.   userSelection=${userSelection,,}
  57.   if [[ $userSelection =~ ^(i| ) ]]; then
  58.     echo $cyan"**************************************************************************************************************"${textreset};
  59.   fi
  60.   if [[ $userSelection =~ ^(d) ]]; then
  61.     echo $cyan"Removing FileBoomLinks.txt"${textreset};
  62.     rm FileBoomLinks.txt 2>/dev/null;
  63.     echo $cyan"**************************************************************************************************************"${textreset};
  64.   fi
  65.   if [[ $userSelection =~ ^(q) ]]; then
  66.     exit 0;
  67.   fi
  68. fi
  69. ####################################################################################################
  70. # File Upload Process
  71. for file in ./*; do
  72.   uploadSessionStatus="0";
  73.   fileExtension=`echo ${file: -4} | sed 's/.*[\.]//;'`;
  74.   fileName=`ls $file | sed 's/\.\///'`;
  75.   while [[ $uploadSessionStatus == "0" ]]; do
  76.     if [[ $fileExtension == "txt" ]] || [[ $fileExtension == "png" ]] || [[ $fileExtension == "jpeg" ]] || [[ $fileExtension == "jpg" ]] || [[ $fileExtension == "sh" ]]; then
  77.       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};
  78.       echo $cyan"$fileName"${textreset};
  79.       echo $cyan"***********************************************************************************************************************************************************************************************"${textreset};
  80.       uploadSessionStatus="1";
  81.       break;
  82.     fi
  83.     if [[ $fileExtension != "txt" ]] && [[ $fileExtension != "png" ]] && [[ $fileExtension != "jpeg" ]] && [[ $fileExtension != "jpg" ]] && [[ $fileExtension != "sh" ]]; then
  84.       cmd="curl -d '{\"username\":\"$user\",\"password\":\"$password\"}' "$url"login 2> .fileBoom.tmp"
  85.       resLogin=`eval $cmd`
  86.       function checkStatus(){
  87.         local json=$1
  88.         local action=$2
  89.         status=$(echo "$json" | jq -r ".status")
  90.         if [ "$status" == "success" ]; then
  91.           uploadSessionStatus=1;
  92.         else
  93.           uploadSessionStatus=0;
  94.         fi
  95.       }
  96.       auth_token=$(echo "$resLogin" | jq -r ".auth_token")
  97.       fileExtension=`echo ${file: -4} | sed 's/.*[\.]//;'`;
  98.       fileName=`ls $file | sed 's/\.\///'`;
  99.       #Scrambles the md5 checksum hash values for the being uploaded (only if enabled in the configuration settings above)
  100.       if [[ $scrambleChecksumHashValues == "0" ]]; then
  101.         originalMD5Checksum=`md5sum $file | awk {'print $1'}`;
  102.         echo $RANDOM >> "$file";
  103.         newMD5Checksum=`md5sum $file | awk {'print $1'}`;
  104.         echo $green"SUCCESS! "${textreset}$cyan"The checksum hash value for "$fileName" has been changed from "${textreset}$green$originalMD5Checksum${textreset}" to "$green$newMD5Checksum${textreset};
  105.       fi
  106.       fboomFileUploadAttempt=0;
  107.       while [[ $fboomFileUploadAttempt -lt $uploadRetryLimit ]]; do
  108.         cmd="curl -d '{\"username\":\"$user\",\"password\":\"$password\"}' "$url"login 2> .fileBoom.tmp"
  109.         resLogin=`eval $cmd`
  110.         checkStatus "$resLogin" "Authentacation"
  111.         auth_token=$(echo "$resLogin" | jq -r ".auth_token");
  112.         echo $cyan"Uploading $fileName..."${textreset};
  113.         cmd="curl -d '{\"auth_token\":\"$auth_token\"}' "$url"GetUploadFormData 2>.fileBoom.tmp";
  114.         resForm=`eval $cmd`;
  115.         checkStatus "$resForm" "Getting form data";
  116.         fileName=$file;
  117.         form_action=$(echo "$resForm" | jq -r ".form_action");
  118.         file_field=$(echo "$resForm" | jq -r ".file_field");
  119.         nodeName=$(echo "$resForm" | jq -r ".form_data.nodeName");
  120.         userId=$(echo "$resForm" | jq -r ".form_data.userId");
  121.         expires=$(echo "$resForm" | jq -r ".form_data.expires");
  122.         hmac=$(echo "$resForm" | jq -r ".form_data.hmac");
  123.         api_request=$(echo "$resForm" | jq -r ".form_data.api_request");
  124.         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> .fileBoom.tmp";
  125.         resUpload=`eval $cmd`;
  126.         checkStatus "$resUpload" "Uploading file to server";
  127.         fileName=`ls $file | sed 's/\.\///'`;
  128.         fileSize=`ls -lha $file | awk {'print $5'} | sed 's/K/\ KB/;s/M/\ MB/;s/G/\ GB/;';`;
  129.         fboomURL=`echo $resUpload | sed 's/.*user_file_id\"\:\"//;s/\".*//;s/^/http:\/\/FileBoom\.me\/file\//;'`;
  130.         fboomFileUploadStatus="${#fboomURL}";
  131.         fboomFileUploadAttempt=$(($fboomFileUploadAttempt +1));
  132.         fboomFileUploadAttemptTrailing=`echo $fboomFileUploadAttempt | rev | cut -c-1 | rev`;
  133.         if [[ $fboomFileUploadAttemptTrailing -eq 1 ]] && [[ $fboomFileUploadAttempt -lt 10 ]] || [[ $fboomFileUploadAttempt -gt 19 ]]; then
  134.           fboomFileUploadAttemptOrdinal=`echo $fboomFileUploadAttempt | sed 's/$/st/';`;
  135.         elif [[ $fboomFileUploadAttemptTrailing -eq 2 ]] && [[ $fboomFileUploadAttempt -lt 10 ]] || [[ $fboomFileUploadAttempt -gt 19 ]]; then
  136.           fboomFileUploadAttemptOrdinal=`echo $fboomFileUploadAttempt | sed 's/$/nd/';`;
  137.         elif [[ $fboomFileUploadAttemptTrailing -eq 3 ]] && [[ $fboomFileUploadAttempt -lt 10 ]] || [[ $fboomFileUploadAttempt -gt 19 ]]; then
  138.           fboomFileUploadAttemptOrdinal=`echo $fboomFileUploadAttempt | sed 's/$/rd/';`;
  139.         else
  140.           fboomFileUploadAttemptOrdinal=`echo $fboomFileUploadAttempt | sed 's/$/th/';`;
  141.         fi
  142.         if [[ $fboomFileUploadStatus -eq "37" ]]; then
  143.           echo $green"SUCCESS! "${textreset}$cyan$fileName" has been successfully uploaded on the $fboomFileUploadAttemptOrdinal attempt"${textreset};
  144.           fboomFileUploadAttempt=`echo $uploadRetryLimit`;
  145.           echo $cyan$fboomURL${textreset};
  146.           rm .fileBoom.tmp 2>/dev/null;
  147.           # Parse DDL, Filename, & Filesize into [BBCode]
  148.           echo "[B]FileBoom: [/B][URL="$fboomURL"]"$fileName" - "$fileSize"[/URL]" >> FileBoomLinks.txt;
  149.         fi
  150.         if [[ $fboomFileUploadStatus -ne "37" ]]; then
  151.           echo $red"FAILURE! "${textreset}$cyan$fileName" failed to upload "${textreset}$red$fileName${textreset}" on the "${textreset}$red$fboomFileUploadAttemptOrdinal" of "$fboomFileUploadAttempt${textreset}$cyan" attempts."${textreset};
  152.           echo $yellow"         Will re-attempt to upload "$fileName" in 15 seconds..."${textreset};
  153.           sleep 15s;
  154.           rm .fileBoom.tmp 2>/dev/null;
  155.         fi
  156.         echo $cyan"***********************************************************************************************************************************************************************************************"${textreset};
  157.       done;
  158.     fi
  159.   done;
  160. done;
  161. echo $cyan"***********************************************************************************************************************************************************************************************"${textreset};
  162. cat FileBoomLinks.txt;
  163. ####################################################################################################
  164. #Counts the number of links generated and display with number of files in current directory.
  165. numFiles=`ls ./* | grep -v \.txt$ | grep -v \.png$ | grep -v \.jpg$ | grep -v \.jpeg$ | grep -v \.sh$ | wc -l`;
  166. if [[ -f FileBoomLinks.txt ]]; then
  167.   numLinks=`cat ./FileBoomLinks.txt | wc -l`; ENDTIME=$(date +%s);
  168. fi
  169. if [[ ! -f FileBoomLinks.txt ]]; then
  170.   numLinks=0;
  171. fi;
  172. echo "Number of files in this directory: "$numFiles;
  173. echo "Total # FileBoom URLs generated: "$numLinks;
  174. echo "Number of seconds this script ran for: "$(($ENDTIME - $STARTTIME));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement