Advertisement
emkay443

Pastebin.com upload script

Aug 26th, 2013
6,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.84 KB | None | 0 0
  1. #!/bin/bash
  2. # Upload a given file to Pastebin.com
  3. # Can upload to a Pastebin.com account or anonymously (default)
  4. # and even put the paste URL into your clipboard!
  5. #
  6. # Dependencies: curl (for uploading the file to pastebin)
  7. # Optional: xclip (for putting the paste URL into the clipboard),
  8. #           sox (for sound playing capability),
  9. #           libnotify-bin (for notifications)
  10. #
  11. # Author: Michael Koch (Emkay443) (m<DOT>koch<AT>emkay443<DOT>de)
  12. # Version: 2013-08-27
  13. # License: Copyleft
  14.  
  15. #################
  16. # CONFIGURATION #
  17. #################
  18.  
  19. # Change this to your personal Pastebin.com Developer API Key
  20. # which can be found at http://pastebin.com/api#1
  21. api_dev_key="0123456789abcdef0123456789abcdef"
  22.  
  23. # Change this to your Pastebin.com user name and password
  24. # and set use_login to true if you want to upload the paste to your own Pastebin.com account
  25. use_login=false
  26. api_user_name="username"
  27. api_user_password="topsecret"
  28.  
  29. # Paste expiration time (N = never, 10M = 10 minutes, 1H = 1 hour, 1D = 1 day, 1W = 1 week, 2W = 2 weeks, 1M = 1 month)
  30. api_paste_expire_date="N"
  31.  
  32. # Paste privacy (0 = public, 1 = unlisted, 2 = private)
  33. api_paste_private="1"
  34.  
  35. # Enable playing the given sound file (using sox' play command) on a successful paste
  36. sound_success_enabled=true
  37. sound_success_file="/usr/share/sounds/freedesktop/stereo/complete.oga"
  38.  
  39. # Enable notification (using libnotify) on a successful paste
  40. notification_enabled=true
  41.  
  42. # Enable copying the paste's URL to the clipboard (using xclip)
  43. clipboard_enabled=true
  44.  
  45. # Enable opening a browser window to your paste on a successful paste
  46. open_browser_enabled=true
  47.  
  48. #########################################################################################
  49. # WARNING: You should be careful when changing any code below. Modify at your own risk! #
  50. #########################################################################################
  51.  
  52. # If there is a first parameter and it is a file, run the program.
  53. if [ ! -z "$1" ] && [ -f "$*" ]; then
  54.  
  55.     ######################
  56.     ## HELPER FUNCTIONS ##
  57.     ######################
  58.  
  59.     function get_api_format {
  60.         if [ ! -z $1 ]; then
  61.             mime_type="$(mimetype -b $1)"
  62.             if [[ $mime_type == *x-shellscript* ]]; then
  63.                 echo bash
  64.             elif [[ $mime_type == *x-c* ]]; then
  65.                 echo c
  66.             elif [[ $mime_type == *x-c++src* ]]; then
  67.                 echo cpp
  68.             elif [[ $mime_type == *x-php* ]]; then
  69.                 echo php
  70.             elif [[ $mime_type == *x-python* ]]; then
  71.                 echo python
  72.             elif [[ $mime_type == *x-haskell* ]]; then
  73.                 echo haskell
  74.             elif [[ $mime_type == *x-html* ]]; then
  75.                 echo html5
  76.             elif [[ $mime_type == *x-java* ]]; then
  77.                 echo java
  78.             elif [[ $mime_type == *javascript* ]]; then
  79.                 echo javascript
  80.             elif [[ $mime_type == *x-lua* ]]; then
  81.                 echo lua
  82.             elif [[ $mime_type == *x-pascal* ]]; then
  83.                 echo pascal
  84.             elif [[ $mime_type == *x-perl* ]]; then
  85.                 echo perl
  86.             elif [[ $mime_type == *x-cobol* ]]; then
  87.                 echo cobol
  88.             elif [[ $mime_type == *css* ]]; then
  89.                 echo css
  90.             elif [[ $mime_type == *sql* ]]; then
  91.                 echo sql
  92.             elif [[ $mime_type == *xml* ]]; then
  93.                 echo xml
  94.             elif [[ $mime_type == *yaml* ]]; then
  95.                 echo yaml
  96.             elif [[ $mime_type == *x-wine-extension-ini* ]]; then
  97.                 echo ini
  98.             elif [[ $mime_type == *x-matlab* ]]; then
  99.                 echo matlab
  100.             else
  101.                 echo text
  102.             fi
  103.         fi
  104.     }
  105.  
  106.     function get_api_user_key {
  107.         if $use_login; then
  108.             curl_data="api_dev_key=$api_dev_key&api_user_name=$api_user_name&api_user_password=$api_user_password"
  109.             echo "&api_user_key="$(curl --data "$curl_data" http://pastebin.com/api/api_login.php)         
  110.         fi
  111.     }
  112.  
  113.     ##################
  114.     ## MAIN PROGRAM ##
  115.     ##################
  116.  
  117.     paste_file="$1"
  118.     api_paste_name="$(basename $paste_file)"
  119.     api_post_url="http://pastebin.com/api/api_post.php"
  120.     api_option="paste"
  121.     api_user_key="$(get_api_user_key)"
  122.     api_paste_format="$(get_api_format $paste_file)"
  123.     curl_data="api_option=paste&api_dev_key=$api_dev_key&api_paste_name=$api_paste_name&api_paste_format=$api_paste_format&api_paste_expire_date=$api_paste_expire_date&api_paste_private=$api_paste_private$api_user_key"
  124.  
  125.     paste_url=$(curl --data "$curl_data" --data-urlencode "api_paste_code@$paste_file" $api_post_url)
  126.  
  127.     if [[ $paste_url == *Bad\ API\ request* ]]; then
  128.         echo "Bad API request!"
  129.         if $notification_enabled; then notify-send "Pastebin Upload" "You Pastebin Upload was not successful!\n\nReason: $paste_url"; fi
  130.     else
  131.         if $clipboard_enabled; then echo $paste_url | xclip -sel clip; fi
  132.         if $notification_enabled; then notify-send "Pastebin Upload" "You Pastebin Upload was successful!\n\nURL: <a href='$paste_url'>$paste_url</a>"; fi
  133.         if $sound_success_enabled; then play $sound_success_file; fi
  134.         if $open_browser_enabled; then x-www-browser $paste_url; fi
  135.     fi
  136.  
  137. else
  138.     echo "This program requires one parameter - the file that you want to paste!"
  139.     exit 1
  140. fi
  141. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement