Advertisement
Guest User

keep2ShareUploader.sh

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