suredream
7/1/2016 - 2:30 AM

require KindleGen under user folder

require KindleGen under user folder

#! /usr/bin/python
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText

def send(fileToSend = "sendBook.py"):

	emailfrom = "jun.xiong1981@gmail.com"
	emailto = "jun.xiong1981@free.kindle.com"
	#emailto = "jun.xiong1981_2@kindle.com"
	
	username = "jun.xiong1981"
	password = "rebornJun@0303"

	msg = MIMEMultipart()
	msg["From"] = emailfrom
	msg["To"] = emailto
	msg["Subject"] = "Ebook to Kindle"
	msg.preamble = "Ebook to Kindle"

	ctype, encoding = mimetypes.guess_type(fileToSend)
	if ctype is None or encoding is not None:
	    ctype = "application/octet-stream"

	maintype, subtype = ctype.split("/", 1)


	fp = open(fileToSend, "rb")
	attachment = MIMEBase(maintype, subtype)
	attachment.set_payload(fp.read())
	fp.close()
	encoders.encode_base64(attachment)
	attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
	msg.attach(attachment)

	server = smtplib.SMTP("smtp.gmail.com:587")
	server.starttls()
	server.ehlo()
	server.login(username,password)
	print 'SendToKindle:', fileToSend
	server.sendmail(emailfrom, emailto, msg.as_string())
	server.quit()

import sys, os
from os import listdir
from os.path import isfile, join
mypath = '/Users/jxiong1/Downloads/'
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
files = [join(mypath, f) for f in files if f.endswith('.epub') or f.endswith('.mobi') or f.endswith('.txt')]
for f in files:
	# print f
	file = f
	if f.endswith('epub'):
		file = f.replace('.epub','.mobi')
		cmd = './kindlegen "%s"' % f
		# print cmd
		os.system(cmd)
		os.system('rm "%s"' % f)
	if f.endswith('txt'):
		file = f.replace('.txt','.mobi')
		cmd = './kindlegen "%s"' % f
		# print cmd
		os.system(cmd)
		os.system('rm "%s"' % f)
	try:
		send(fileToSend=file)
		os.system('rm "%s"' % file)
	except:
		pass