Skip to content

Instantly share code, notes, and snippets.

@tammymakesthings
Created October 14, 2010 21:32
Show Gist options
  • Save tammymakesthings/627098 to your computer and use it in GitHub Desktop.
Save tammymakesthings/627098 to your computer and use it in GitHub Desktop.
Download PDFs etc. and send them to your Kindle from the command line.
#!/bin/env bash
##############################################################################
# kcurl: Download one or more files and send them to the Kindle.
# Version 1.1, Tammy Cravit, tammy@tammycravit.com, 10/14/2010
#
# Requires that cURL and mutt be installed. The "home Wi-Fi" detection code
# only works on MacOS X 10.x (probably with x > 4), but I welcome patches.
#
# To use this script, you'll need to configure the Kindle e-mail name and
# home Wi-Fi SSID below.
##############################################################################
# CONFIGURE THESE OR THE SCRIPT WILL FAIL
KINDLE_EMAIL_NAME="configure_this" # The part before the @kindle.com
HOME_WIFI_SSID="my_wifi_ssid" # The name of your home Wi-Fi network
echo "************************************************************************"
echo "* kcurl: Download one or more files and send them to the Kindle. *"
echo "* Version 1.1, Tammy Cravit, tammy@tammycravit.com, 10/14/2010 *"
echo "************************************************************************"
echo ""
#=============================================================================
# Make sure we were given at least one URL to process.
#=============================================================================
if [ "$1" = "" ]
then
echo "Usage: `basename $0` <file> [file ...]"
echo " Download PDF files and send them to the Kindle."
echo ""
exit 254
fi
#=============================================================================
# If we're running on a Mac, try to detect if we're on our home Wi-Fi.
# Configure your home SSID(s) as appropriate in the case statement.
# If anyone wants to extend this code to other platforms besides the Mac,
# I welcome patches.
#=============================================================================
sys_type=`uname -s`
if [ "$sys_type" = "Darwin" ]
then
wifi_ssid="`airport -I | grep 'SSID' | grep -v 'BSSID' | cut -d":" -f2 | cut -d" " -f2`"
if [ "${wifi_ssid}" = "${HOME_WIFI_SSID}" ]
then
echo "- Detected known Wi-Fi, so Kindle is probably on Wi-Fi too."
echo " Using free.kindle.com address"
KINDLE_EMAIL="${KINDLE_EMAIL_NAME}@free.kindle.com"
else
echo "- Detected unknown Wi-Fi, so Kindle is probably not on Wi-Fi."
echo " Using (non-free) kindle.com address"
KINDLE_EMAIL="${KINDLE_EMAIL_NAME}@kindle.com"
fi
else
echo "- Wi-Fi detection not enabled on ${sys_type} systems."
echo " Using the free kindle.com address and hoping for the best."
KINDLE_EMAIL="${KINDLE_EMAIL_NAME}@free.kindle.com"
fi
echo ""
#=============================================================================
# Download the files and e-mail them.
#=============================================================================
for url in $*
do
file_name=`basename "${url}"`
echo -n "Downloading ${url} to Kindle..."
curl -s -O "${url}"
if [ "$?" -ne "0" ]
then
echo "FAILED!"
else
mutt -s "${url}" -a "${file_name}" -- "${KINDLE_EMAIL}" < /dev/null
if [ "$?" -ne "0" ]
then
echo "downloaded but could not be e-mailed."
else
rm "${file_name}"
echo "done."
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment