-
-
Save filipnbc/6da6c99685e13fdccae977e5de6413c9 to your computer and use it in GitHub Desktop.
Screenshot (Linux or Mac) > upload to Google Drive > shorten w/ goog.l, bit.ly, or coinhive > copy to clipboard > notify
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
#!/bin/bash | |
############################################################################################################################# | |
# Title: # | |
# screenshot (Linux or Mac) > upload to Google Drive > shorten w/ goog.l, bit.ly, or coinhive > copy to clipboard > notify # | |
############################################################################################################################# | |
################################################## | |
# Linux requirements: # | |
# 1. https://github.com/prasmussen/gdrive # | |
# (run 'gdrive about') # | |
# 2. gnome-screenshot, shutter, or scrot # | |
# 3. bit.ly account/api key OR a google API key # | |
# or coinhive api key and hosted and the # | |
# associated script (coinhive_ss.php) # | |
# 4. xclip # | |
# 5. libnotify # | |
# 6. curl # | |
################################################## | |
################################################## | |
# Mac requirements: # | |
# 1. homebrew: https://brew.sh # | |
# 2. run: # | |
# brew update # | |
# brew install curl # | |
# brew install gdrive # | |
# brew install terminal-notifier # | |
# (use banner! https://goo.gl/sXKG3r) # | |
# gdrive about # | |
# (https://github.com/prasmussen/gdrive) # | |
# 3. bit.ly account/api key OR a Google API key # | |
# # | |
# How to use (install/keyboard shortcut): # | |
# https://goo.gl/oor4y4 # | |
# Note: # | |
# After automator/shortcut you may need # | |
# to launch script for the first time from # | |
# Finder > Services ('Finder' or any # | |
# current open app). https://goo.gl/WyxY2f # | |
# (Blame Appple: https://goo.gl/MAS8gZ) # | |
################################################## | |
################################################################################ | |
# To do: # | |
# 1. Convert to Python # | |
# 2. Windows? # | |
# 3. Check for dependencies / install automatically # | |
# 4. Set config options via command line (no file editing) # | |
# 5. Replace gdrive # | |
# Upload/Drive API: https://github.com/google/google-api-python-client # | |
# Oauth: https://github.com/google/oauth2client # | |
# 6. For Mac, try this: https://goo.gl/M56BCz # | |
# Also this: https://www.igeeksblog.com/add-arrows-to-a-screenshot-on-mac/ # | |
################################################################################ | |
################################################## | |
# Inspiration: # | |
# https://gist.github.com/treyhunner/892492 # | |
# https://gist.github.com/wafflesnatcha/3694648 # | |
# a whole lotta https://google.com # | |
################################################## | |
########## | |
# CONFIG # | |
########## | |
# Set type | |
SHORT_URL_TYPE="google" # google, bitly, or coinhive | |
# Google URL shortener | |
# https://developers.google.com/url-shortener/v1/getting_started | |
GOOGLE_API_KEY=xxx | |
# bit.ly URL shortener | |
# https://support.bitly.com/hc/en-us/articles/231140388-How-do-I-find-my-API-key- | |
BITLY_USERNAME=xxx | |
BITLY_API_KEY=xxx | |
# coinhive URL shortener | |
# https://coinhive.com/documentation/http-api#link-create | |
COINHIVE_FILE=https://anthonybaldwin.me/tools/coinhive_ss.php # location of file | |
NUM_HASHES=256 # num hashes that must be solve before redirect e.g., 256,512,1024,1280,etc. | |
# Directories / file names.. Can edit, but don't need to (at least on my machine) | |
DIRECTORY="$HOME/tmp_screenshot/" | |
FILE_NAME=`date +"%Y-%m-%d_%H:%M:%S"`.png | |
DRIVE_FOLDER_NAME="Screenshots" | |
######### | |
# DO IT # | |
######### | |
function goo.gl() # https://gist.github.com/wafflesnatcha/3694648 | |
{ | |
[[ ! $1 ]] && { echo -e "Usage: goo.gl [URL]\n\nShorten a URL using the Google URL Shortener service (http://goo.gl)."; return; } | |
curl -qsSL -m10 --connect-timeout 10 \ | |
"https://www.googleapis.com/urlshortener/v1/url?key=${GOOGLE_API_KEY}" \ | |
-H 'Content-Type: application/json' \ | |
-d '{"longUrl":"'${1//\"/\\\"}'"}' | | |
perl -ne 'if(m/^\s*"id":\s*"(.*)",?$/i) { print $1 }' | |
} | |
function bit.ly() # https://gist.github.com/treyhunner/892492 | |
{ | |
ESCAPED_URL="$(echo $1 | sed 's,:,%3A,g;s,/,%2F,g')" | |
BITLY_API_CALL="http://api.bit.ly/v3/shorten?login=$BITLY_USERNAME&apiKey=$BITLY_API_KEY&longUrl=$ESCAPED_URL&format=txt" | |
echo $(curl -s -S $BITLY_API_CALL) | |
} | |
function coinhive() # To do: Do the curl here.. | |
{ | |
COINHIVE_API_CALL="$COINHIVE_FILE?numhashes=$NUM_HASHES&url=$1" | |
echo $(curl -s -S $COINHIVE_API_CALL) | |
} | |
function notify() | |
{ | |
TITLE="Something went wrong!" | |
NOTIFICATION="Try again." | |
if [ ! -z $1 ] | |
then | |
TITLE="Screenshot added!" | |
NOTIFICATION="Screenshot link copied to clipboard: $1" | |
fi | |
if [ $(which notify-send) ] | |
then | |
notify-send "$TITLE" "$NOTIFICATION" | |
elif [ "$(uname)" == "Darwin" ] | |
then | |
osascript -e 'display notification "'$NOTIFICATION'" with title "'$TITLE'"' | |
fi | |
} | |
# Make folder if we need to > navigate there | |
mkdir -p ${DIRECTORY} | |
echo "Attempting to create directory: $DIRECTORY" | |
cd "$DIRECTORY" | |
# Take screenshot using what is installed.. knapshot is a pain, sorry | |
sleep 0.2 # blame scrot | |
if [ $(which gnome-screenshot) ] | |
then | |
# gnome | |
echo "Using gnome-screenshot." | |
gnome-screenshot -af ${FILE_NAME} | |
elif [ $(which shutter) ] | |
then | |
# shutter | |
echo "Using shutter." | |
shutter -e -s -o ${FILE_NAME} | |
elif [ $(which scrot) ] | |
then | |
# scrot | |
echo "Using scrot." | |
scrot --select ${FILE_NAME} | |
elif [ $(which screencapture) ] | |
then | |
# screencapture / mac default | |
echo "Using screencapture." | |
screencapture -s ${FILE_NAME} | |
fi | |
# Create directory in Google Drive if it does not exist... | |
DRIVE_FOLDER_CHECK="$(gdrive list --no-header --query "name = '${DRIVE_FOLDER_NAME}'")" | |
if [ "$DRIVE_FOLDER_CHECK" = "" ] | |
then | |
echo "Attempting to create Google Drive folder \"${DRIVE_FOLDER_NAME}\"." | |
CREATE="$(gdrive mkdir "$DRIVE_FOLDER_NAME")" | |
DRIVE_FOLDER_ID="$(echo $CREATE | awk '{print $2}')" | |
echo "Google Drive folder ID: $DRIVE_FOLDER_ID" | |
else | |
echo "Google Drive folder \"${DRIVE_FOLDER_NAME}\" already exists." | |
DRIVE_FOLDER_ID="$(echo $DRIVE_FOLDER_CHECK | awk '{print $1}')" | |
echo "Google Drive folder ID: $DRIVE_FOLDER_ID" | |
fi | |
# Upload | |
echo "Uploading file: $FILE_NAME" | |
DRIVE_UPLOAD="$(gdrive upload -p ${DRIVE_FOLDER_ID} --share ${FILE_NAME})" | |
# Get uploaded file ID | |
DRIVE_FILE_ID="$(echo $DRIVE_UPLOAD | awk '{print $4}')" | |
echo "Uploaded file ID: $DRIVE_FILE_ID" | |
# Make sure file ID has 28 chars... or something went wrong | |
# This is 33 now... | |
if [ "${#DRIVE_FILE_ID}" == "33" ] | |
then | |
URL="https://drive.google.com/open?id=$DRIVE_FILE_ID" | |
echo "Drive URL is: $URL" | |
if [ "$SHORT_URL_TYPE" == "google" ] | |
then | |
echo "Selected method is Google." | |
SHORT_URL="$(goo.gl ${URL})" | |
elif [ "$SHORT_URL_TYPE" == "bitly" ] | |
then | |
echo "Selected method is bit.ly." | |
SHORT_URL="$(bit.ly ${URL})" | |
elif [ "$SHORT_URL_TYPE" == "coinhive" ] | |
then | |
echo "Selected method is coinhive." | |
SHORT_URL="$(coinhive ${URL})" | |
fi | |
# Copy shortened URL to clipboard | |
if [ $(which xclip) ] # linux | |
then | |
echo $SHORT_URL | tr -d '\n' | xclip -sel clip | |
elif [ "$(uname)" == "Darwin" ] # mac | |
then | |
echo $SHORT_URL | tr -d '\n' | pbcopy | |
fi | |
# Display message to user based on result | |
if [[ "$SHORT_URL" == *"http"* ]] | |
then | |
echo "Shortened URL is: $SHORT_URL" | |
notify "${SHORT_URL}" | |
else | |
echo "No shortened URL. Try again." | |
notify | |
fi | |
else | |
notify | |
fi | |
# Clean up | |
echo "Cleaning up." | |
rm -r ${FILE_NAME} | |
cd .. | |
rm -r ${DIRECTORY} | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment