lwl5219
1/30/2018 - 7:20 AM

Kindle Push

# Command line utility to send attachments to your kindle
# It supports sending a PDF in both regular form and with the convert option
# and sending to the regular (@kindle.com) or free Kindle mail (@free.kindle.com)

# The only argument it has is the name of the file to send. It will detect if
# it is a pdf and treat it accordingly.

## Preferences
# If true, send one version with pdf as-is, another with 'convert' subject
# else, just send the pdf version
SEND_PDF_BOTH_VERSIONS=true

# if true, sends file to @free.kindle.com instead of @kindle.com
SEND_TO_FREE_MAIL=false

## Configuration. Change the base username to fit your needs
BASE_USERNAME="your_user_name"
BASE_KINDLE_MAIL="$BASE_USERNAME@kindle.com"
FREE_KINDLE_MAIL="$BASE_USERNAME@free.kindle.com"

## Helper functions
# Prints confirmation message
_confirmation() {
    echo "$1 sent to $2 with subject \"$3\""
}

# Prints error message
_error() {
    echo "There was an error sending $1 to $2 with subject \"$3\""
}

#Sends an email
# $1 is the path to the attachment
# $2 is the recipient of the email (i.e. To: example@kindle.com)
# $3 OPTIONAL The subject of the email
_send() {
    local attachment=$1
    local mail=$2
    if ! [[ $3 ]]; then
        subject="kindle cli"
    else
        subject=$3
    fi
    # Send mail
    mutt -s $subject -a $attachment -- $mail < /dev/null
    if [[ $? -ne 0 ]]; then
        _error $attachment $mail $subject
    else
        _confirmation $attachment $mail $subject
    fi

}
# $1 is the name of the attachment file
# $2 is the subject of the mail. You shold set this to 'convert'
_send_mail() {
    if $SEND_TO_FREE_MAIL; then
        _send $1 $FREE_KINDLE_MAIL $2
    else
        _send $1 $BASE_KINDLE_MAIL $2
    fi
}

## Main script
# If mutt not found then exit
command -v mutt >/dev/null 2>&1 || { echo >&2 "Mutt is required but it was not found. Please install it"; exit 1; }

# TODO In order for this to work you need to add the hostname to your
# list of trusted senders. However, I haven't found a way to print the whole
# hostname (e.g. abc@x-laptop.localhost)
host_name=$(hostname)
echo "Don't forget to add $host_name to your list of approved e-mail address on Amazon"

filename=$1
# Neat, found on http://stackoverflow.com/a/965072
extension="${filename##*.}"
if [[ "$extension" -eq "pdf" ]] && [[ SEND_PDF_BOTH_VERSIONS ]]; then
    _send_mail $1
    _send_mail $1 "convert"
    exit 0
fi
_send_mail $1
exit 0
#!/usr/bin/env python
#encoding=utf-8

# fork from https://github.com/c4pt0r/kindlepush/blob/master/kindlepush.py

import os
import urllib2, urllib
import smtplib,mimetypes
from ConfigParser import ConfigParser
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import json
import redis

#smtp info
smtp_user = None
smtp_pass = None
smtp_host = None
smtp_port = 25

#kindle info
kindle_account = None

#diffbot api
diffbot_url = "http://www.diffbot.com/api/article?token=9c56b2167b47042697870229163118e5&url="

def get_text_of_link(url):
    fp = urllib2.urlopen(diffbot_url + urllib.quote(url))
    content = fp.read()
    p = json.loads(content)
    return p

def load_config(config_file):
    global smtp_host, smtp_pass, smtp_user, smtp_port, kindle_account
    cf = ConfigParser()
    cf.read(config_file)
    try:
        smtp_user = cf.get("smtp", "user")
        smtp_pass = cf.get("smtp", "password")
        smtp_host = cf.get("smtp", "host")
        #kindle_account = cf.get("kindle", "user")
    except:
        print "load config error"

def send_to_kindle(to, title, content):
    smtp = smtplib.SMTP()
    smtp.connect(smtp_host)
    smtp.login(smtp_user, smtp_pass)

    msg = MIMEMultipart()
    msg['From'] = smtp_user
    msg['To'] = to
    msg['Subject'] = 'kindle push email'
    txt = MIMEText("from c4pt0r")
    msg.attach(txt)

    doc = MIMEText(content, 'plain', 'utf-8')
    doc['Content-Type'] = 'application/octet-stream'
    doc.add_header('Content-Disposition', 'attachment', filename = title + '.txt')
    msg.attach(doc)
    smtp.sendmail(smtp_user, to , msg.as_string())

load_config("./config")
#get_text_of_link("http://www.douban.com/group/topic/27252494/")
#send_to_kindle("c4pt0r@kindle.com","save to kindle", "hello world")
if __name__ == '__main__':
    r = redis.Redis("localhost", port = 6379, db = 0)
    while True:
        _, request = r.brpop("push_tasks")
        request = json.loads(request)
        print 'new task!', request
        t = get_text_of_link(request['url'])
        if request['title'] == '@':
            title = t['title']
        else:
            title = request['title']
        content = t['text']
        send_to_kindle(request['sendto'], title, content)
        print 'done'
        print '---------------'