emkay443

Screenshot Imgur Upload Script

Sep 10th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.89 KB | None | 0 0
  1. #!/bin/bash
  2. # Screenshot Imgur Upload Script
  3. #
  4. # Takes a screenshot (desktop, active window, area) after a little pause
  5. # and uploads it to imgur (optionally) and copies the image's URL to the clipboard (optionally)
  6. #
  7. # Dependencies: scrot, curl, zenity, xclip, sox, libnotify-bin
  8. #
  9. # Author: Michael Koch (m<DOT>koch<AT>emkay443<DOT>de)
  10. # Version: 2014-10-21
  11. # License: GNU General Public License v3 (http://www.gnu.de/documents/gpl-3.0.en.html)
  12.  
  13. ###################
  14. ## CONFIGURATION ##
  15. ###################
  16.  
  17. # scrot binary file path
  18. scrot_cmd="/usr/bin/scrot"
  19.  
  20. # xclip binary file path
  21. clipboard_enabled=true
  22. xclip_cmd="/usr/bin/xclip"
  23.  
  24. # notify-send binary file path
  25. notification_enabled=true
  26. notification_cmd="/usr/bin/notify-send"
  27.  
  28. # Command to play the given sound file on success
  29. sound_enabled=true
  30. sound_cmd="/usr/bin/play"
  31. sound_file="/usr/share/sounds/freedesktop/stereo/complete.oga"
  32.  
  33. # Enable the following option if you want to have a symlink to your screenshots folder on your desktop
  34. link_on_desktop_enabled=false
  35.  
  36. # Screenshot file extension (must be compatible)
  37. file_extension="png"
  38. file_basedir="$HOME/Bilder/screenshots"
  39.  
  40. # Pause time in seconds before a screenshot is taken
  41. # Useful for taking screenshots of opened menues
  42. sleep_time="2"
  43.  
  44. # Disable the following option
  45. # if you don't want to paste the screenshot's URL to the specified upload log
  46. uploadlog_enabled=true
  47. uploadlog_file_name="imgur-upload.txt"
  48.  
  49. # Command line option names
  50. option_type_desktop="--desktop"
  51. option_type_window="--window"
  52. option_type_area="--area"
  53. option_no_upload="--no-upload"
  54. option_force_upload="--force-upload"
  55. option_no_save="--no-save"
  56.  
  57.  
  58. #########################################################################################
  59. # WARNING: You should be careful when changing any code below. Modify at your own risk! #
  60. #########################################################################################
  61.  
  62. ######################
  63. ## HELPER FUNCTIONS ##
  64. ######################
  65.  
  66. function printParmError {
  67.     echo -e "Usage: $0 <TYPE> [OPTIONS]"
  68.     echo -e "The screenshot's type can be one of the following:"
  69.     echo -e "$option_type_desktop\tTakes a screenshot of the full desktop"
  70.     echo -e "$option_type_window\tTakes a screenshot of the activated window"
  71.     echo -e "$option_type_area\t\tTakes a screenshot of a user-selected area"
  72.     echo
  73.     echo -e "These options are also possible:"
  74.     echo -e "$option_no_upload\tDon't ask for upload, just save to disk"
  75.     echo -e "$option_force_upload\tDon't ask for upload, always upload"
  76.     echo -e "$option_no_save\tDon't save the image on disk"
  77.     echo -e "\t(the above option doesn't make sense together with --no-upload)"
  78.     exit 1
  79. }
  80.  
  81. function doUpload {
  82.     $notification_cmd --hint=int:transient:1 "Upload" "The screenshot is being uploaded..."
  83.     # Try to upload the screenshot to imgur and put the results to the variable curl_response
  84.     curl_response=$(curl -F "image"=@"$file_path" -F "key"="5d317f0bee23b282473522e1aa68f621" http://imgur.com/api/upload.xml)
  85.     # Print error message if errors occur
  86.     if [[ "$curl_response" == *overload* ]]; then
  87.         $notification_cmd --hint=int:transient:1 "Imgur server overloaded" "The screenshot couldn't be uploaded\nThe servers are overloaded"
  88.     elif [[ "$curl_response" == *404* ]]; then
  89.         $notification_cmd --hint=int:transient:1 "Imgur Server not reachable" "The screenshot couldn't be uploaded\nThe servers are not reachable (error 404).\nCheck your network connection"
  90.     elif [[ "$curl_response" == *error* ]]; then
  91.         $notification_cmd --hint=int:transient:1 "Imgur Server not reachable" "The screenshot couldn't be uploaded\nThe servers are not reachable (unknown error).\nCheck your network connection"
  92.     else
  93.         # Strip unneccessary stuff and put the image's URL to the clipboard
  94.         curl_response=$(echo -e "$curl_response" | grep -Eo '<[a-z_]+>http[^<]+'|sed 's/^<.\|_./\U&/g;s/_/ /;s/<\(.*\)>/\1:\ /' | awk 'NR == 1' | sed -e 's/Original Image: //' | tr -d '\n')
  95.  
  96.         if [ $clipboard_enabled ]; then echo $curl_response | $xclip_cmd -sel clip; fi
  97.  
  98.         if [ $uploadlog_enabled ]; then
  99.             # If there is no upload log, create one
  100.             # and append the URL to it
  101.             if [[ ! -e "$uploadlog_path" ]]; then
  102.                 touch "$uploadlog_path"
  103.             fi
  104.             echo -e "$file\n\n$curl_response\n\n\n" > $uploadlog_path
  105.         fi
  106.        
  107.         # Notify the user about a successful upload
  108.         if [ $notification_enabled ]; then $notification_cmd --hint=int:transient:1 "Imgur Upload" "Upload was successful\n\nLink: <a href='$curl_response'>$curl_response</a>"; fi
  109.         if [ $sound_enabled ]; then $sound_cmd $sound_file; fi
  110.     fi
  111. }
  112.  
  113. ####################
  114. # HELPER VARIABLES #
  115. ####################
  116.  
  117. day="$(date '+%d')"
  118. month="$(date '+%m')"
  119. year="$(date '+%Y')"
  120. time="$(date '+%H-%M-%S')"
  121.  
  122. ##################
  123. ## MAIN PROGRAM ##
  124. ##################
  125.  
  126. if [ ! -z "$1" ]; then
  127.     # Check screenshot type
  128.     case $1 in
  129.         "$option_type_desktop")
  130.             screenshot_type="desktop"
  131.             ;;
  132.         "$option_type_window")
  133.             screenshot_type="window"
  134.             ;;
  135.         "$option_type_area")
  136.             screenshot_type="area"
  137.             ;;
  138.         *)
  139.             echo "Parameter error!"
  140.             printParmError
  141.             exit 1
  142.             ;;
  143.     esac
  144.  
  145.     # Check command-line options
  146.     for option in $*; do
  147.         if [[ $option == *$option_no_upload* ]]; then
  148.             no_upload=true
  149.         elif [[ $option == *$option_force_upload* ]]; then
  150.             force_upload=true
  151.         elif [[ $option == *$option_no_save* ]]; then
  152.             no_save=true
  153.         fi
  154.     done
  155.  
  156.     # File and folder management
  157.     if [ $no_save ]; then
  158.         echo a
  159.         file_path="/tmp/screenshot-$time.$file_extension"
  160.         uploadlog_path="/dev/null"
  161.     else
  162.         if $link_on_desktop_enabled; then
  163.             if [[ ! -e "$HOME/Desktop" ]]; then
  164.                 if [[ -e "$HOME/Arbeitsfläche" ]]; then
  165.                     ln -s "$HOME/Arbeitsfläche" "$HOME/Desktop"
  166.                 else
  167.                     mkdir "$HOME/Desktop"
  168.                 fi
  169.             fi
  170.             if [[ ! -e "$HOME/Desktop/screenshots" ]]; then
  171.                 ln -s "$basepath" "$HOME/Desktop/screenshots"
  172.             fi
  173.         fi
  174.  
  175.         file_dir="$file_basedir/$year/$month/$day"
  176.         if [[ ! -e "$file_dir" ]]; then
  177.             mkdir -p "$file_dir"
  178.         fi
  179.  
  180.         unlink "$file_basedir/This Month" 2> /dev/null
  181.         unlink "$file_basedir/Today" 2> /dev/null
  182.  
  183.         ln -s "$file_basedir/$year/$month" "$file_basedir/This Month"
  184.         ln -s "$file_basedir/$year/$month/$day" "$file_basedir/Today"
  185.  
  186.         file_path="$file_dir/screenshot-$time.$file_extension"
  187.         uploadlog_path="$file_dir/$uploadlog_file_name"
  188.     fi
  189.  
  190.     # Take the screenshot
  191.     sleep $sleep_time
  192.     case $screenshot_type in
  193.         "desktop")
  194.             $scrot_cmd -q 0 "$file_path"
  195.             ;;
  196.         "window")
  197.             $scrot_cmd -u -b -q 0 "$file_path"
  198.             ;;
  199.         "area")
  200.             $scrot_cmd -s -b -q 0 "$file_path"
  201.             ;;
  202.         *)
  203.             exit 1
  204.             ;;
  205.     esac
  206.  
  207.     # Upload screenshot
  208.     if [ ! $no_upload ]; then
  209.         if [ $force_upload ]; then
  210.             doUpload
  211.         elif zenity --question --text "Do you want to upload the screenshot to imgur?" --title "Question"; then
  212.             doUpload
  213.         fi
  214.     fi
  215.  
  216.     # Remove
  217.     if [ $no_save ]; then
  218.         rm "$file_path"
  219.     fi
  220.  
  221.     exit 0
  222. else
  223.     printParmError
  224. fi
Advertisement
Add Comment
Please, Sign In to add comment