-
Notifications
You must be signed in to change notification settings - Fork 5
/
html2mobi.py
52 lines (43 loc) · 1.5 KB
/
html2mobi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from readability import Document
import re
from subprocess import call
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
# Get the html
response = requests.get("http://URL/TO/CONVERT")
# Clean up the html using readability
doc = Document(response.text)
# Use the webpage title as base file name
file_name = re.sub(r'[^a-zA-Z0-9]+', '-', doc.title())
# Write the html response to local file
f = open(file_name + '.html', 'w')
f.write(doc.summary())
f.close()
# Convert the local html file to .mobi
call(["./kindlegen", file_name + '.html'])
# Send the document as email attachment
msg = MIMEMultipart()
send_from = msg['From'] = '[email protected]'
send_to = msg['To'] = '[email protected]' # Can be 'Send to Kindle' email
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = file_name + ".mobi"
# Attache email body
msg.attach(MIMEText('Want to write a customized email boddy? Then put it here.'))
# Attach the .mobi file
fp = open(file_name + ".mobi", "rb")
mobi_file = MIMEApplication(fp.read())
fp.close()
encoders.encode_base64(mobi_file)
mobi_file.add_header('Content-Disposition', 'attachment; filename="%s"' % file_name + '.mobi')
msg.attach(mobi_file)
# Send the email
smtp = smtplib.SMTP("smtp_server:port")
smtp.starttls()
smtp.login('smtp_username', 'smtp_password')
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()