Created
March 16, 2012 17:47
-
-
Save pelletier/2051430 to your computer and use it in GitHub Desktop.
Standalone Ruby script to take a screenshot and send the file to CloudApp under Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Requirements: | |
# - X | |
# - Ruby | |
# - notify-send (libnotify) | |
# - XSel | |
# - Import | |
require 'date' | |
require 'net/http' | |
$CLOUDAPP_USER = "YOUR CLOUDAPP USER NAME" | |
$CLOUDAPP_PASS = "YOUR CLOUDAPP PASSWORD" | |
# Generate a file name. | |
$FNAME = Time.now.to_s.gsub(':', '-').gsub(' ', '-').gsub('+','') + ".png" | |
# Function to send a notification using libnotify | |
def notify(message) | |
%x{notify-send "[Cloudshot] #{message}"} | |
end | |
# Create the file. | |
%x{import #{$FNAME}} | |
# Ask for the S3 info. | |
response = %x{curl http://my.cl.ly/items/new --digest -u "#{$CLOUDAPP_USER}:#{$CLOUDAPP_PASS}" -G -H "Accept: application/json"} | |
# Check that we haven't reached the limit. | |
/\"uploads_remaining\":(?<uploads_count>\d+)/ =~ response | |
if uploads_count.to_i == 0 | |
notify("Daily limit hit!") | |
Process.exit | |
end | |
# Let's construct the S3 request. | |
response.gsub! "${filename}", $FNAME | |
/\"url\":\"(?<url>.+?)\"/ =~ response | |
/\"params\":{(?<params_raw>.+?)}/ =~ response | |
curl_cmd = ['curl'] | |
params_raw.split(',').each do |param_raw| | |
/\"(?<key>.+?)\":\"(?<val>.+)\"/ =~ param_raw | |
curl_cmd << "-F \"#{key}=#{val}\"" | |
end | |
# Add the file. | |
curl_cmd << "-F \"file=@#{$FNAME}\"" | |
curl_cmd << url | |
curl_cmd << '-v' | |
curl_cmd << '-X POST' | |
curl_cmd << '2> /tmp/drop && cat /tmp/drop' | |
# Finally upload the file. | |
response = %x{#{curl_cmd.join(' ')}} | |
# Extract the ping URL. | |
/< Location: (?<follow>.+)/ =~ response | |
# Ping CloudApp. | |
response = %x{curl "#{follow.chomp}" --digest -u "#{$CLOUDAPP_USER}:#{$CLOUDAPP_PASS}" -G -H "Accept: application/json" -v} | |
# Grab the short URL. | |
/"url":"(?<final_url>.+?)"/ =~ response | |
# Copy to the clipboard | |
%x{echo -n "#{final_url}" | xsel -i -b} | |
# We are so proud. | |
notify("New URL copied to the clipboard") | |
# Clean. | |
%x{rm -R #{$FNAME}} | |
%x{rm -R /tmp/drop} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if I want to take a screenshot of an iframe included html is it possible with this script?