Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/local/bin/python # encoding: utf-8 *Send documents or webpage articles to a kindle device or app*
:Author: David Young
:Date Created: October 10, 2016 """ ################# GLOBAL IMPORTS ####################
""" *Send documents or webpage articles to a kindle device or app*
**Key Arguments:** - ``log`` -- logger - ``settings`` -- the settings dictionary - ``urlOrPath`` -- the url or path to the content source - ``title`` -- the title of the output document. I. False then use the title of the original source. Default *False* - ``header`` -- content to add before the article/book content in the resulting ebook. Default *False* - ``footer`` -- content to add at the end of the article/book content in the resulting ebook. Default *False*
**Usage:**
To send content from a webpage article straight to your kindle device or smart phone app, you will first need to populate the email settings with polyglot's settings file at ``~.config/polyglot/polyglot.yaml``, then use the following code:
.. code-block:: python
from polyglot import kindle sender = kindle( log=log, settings=settings, urlOrPath="http://www.thespacedoctor.co.uk/blog/2016/09/26/mysqlSucker-index.html", header='<a href="http://www.thespacedoctor.co.uk">thespacedoctor</a>', footer='<a href="http://www.thespacedoctor.co.uk">thespacedoctor</a>' ) success = sender.send()
Success is True or False depending on the success/failure of sending the email to the kindle email address(es). """ # Initialisation
self, log, settings, urlOrPath, title=False, header=False, footer=False ): self.log = log log.debug("instansiating a new 'ebook' object") self.settings = settings self.title = title self.header = header self.footer = footer self.urlOrPath = urlOrPath self.outputDirectory = "/tmp" self.format = "mobi"
# Initial Actions
return None
self): """*send the mobi book generated to kindle email address(es)*
**Return:** - ``success`` -- True or False depending on the success/failure of sending the email to the kindle email address(es). """ self.log.info('starting the ``send`` method')
if self.urlOrPath.split(".")[-1] == "docx": if self.title: pathToMobi = self.outputDirectory + "/" + self.title + ".docx" else: pathToMobi = self.outputDirectory + "/" + \ os.path.basename(self.urlOrPath) shutil.copyfile(self.urlOrPath, pathToMobi) else: pathToMobi = self.get() if not pathToMobi: return 404
# create MIME message
msg = MIMEMultipart() msg['From'] = self.settings["email"]["user_email"] msg['To'] = ", ".join(self.settings["email"]["kindle_emails"]) msg['Subject'] = 'Polyglot to Kindle' text = 'This email has been automatically sent by polyglot' msg.attach(MIMEText(text))
basename = os.path.basename(pathToMobi) print "Sending the book `%(pathToMobi)s` to Kindle device(s)" % locals() msg.attach(self.get_attachment(pathToMobi))
# convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue()
# send email try: mail_server = smtplib.SMTP_SSL(host=self.settings["email"]["smtp_server"], port=self.settings["email"]["smtp_port"]) mail_server.login(self.settings["email"]["smtp_login"], self.settings[ "email"]["smtp_password"]) mail_server.sendmail(self.settings["email"]["user_email"], ", ".join(self.settings[ "email"]["kindle_emails"]), msg) mail_server.close() except smtplib.SMTPException: os.remove(pathToMobi) self.log.error( 'Communication with your SMTP server failed. Maybe wrong connection details? Check exception details and your headjack settings file') return False
os.remove(pathToMobi)
self.log.info('completed the ``send`` method') return True
'''Get file as MIMEBase message'''
try: file_ = open(file_path, 'rb') attachment = MIMEBase('application', 'octet-stream') attachment.set_payload(file_.read()) file_.close() encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_path)) return attachment except IOError: traceback.print_exc() message = ('The requested file could not be read. Maybe wrong ' 'permissions?') print >> sys.stderr, message sys.exit(6)
# xt-class-method |