-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpage2kindle.py
executable file
·123 lines (80 loc) · 3.2 KB
/
webpage2kindle.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
import os,os.path,sys,pdfkit,requests,re
import getpass
import pynotify
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
#IMPORTANT--Make sure your gmail settings' access to 'less-secure apps' is allowed.
#IMPORTANT--Make sure that the email you use here is added as trusted address in amazon kindle settings
def URL_to_PDF( URL, filename, pdfobj ):
'''Convert to pdf if URL given is not the direct link to pdf. Save on disk.'''
res = requests.get(URL)
#Validating URL
res.raise_for_status()
#If URL is a direct Link to a pdf file, then save directly on DISK.
if pdfobj.search(URL):
with open(filename,'wb') as f:
f.write(res.content)
f.close()
#Convert to pdf and save on DISK using pdfkit.
else:
pdfkit.from_url( URL, filename )
def email_to_kindle( URL, filename, from_address, to_address ):
'''Sending the saved PDF on disk, to the configured kindle email address.'''
#Do not want to configure password in a unencrypted file.
print "Enter your password for account:", from_address, " (Will not be saved)"
password = getpass.getpass()
print 'Adding webpage to kindle...'
msg = MIMEMultipart()
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = "Kindle Document by Webpage2Kindle."
body = "The document named" + filename + "from" + URL + "has been added to this Kindle account:" + to_address + "."
#Text added to email body.
msg.attach(MIMEText(body, 'plain'))
#PDF Object.
attachment = open(filename, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
#Add PDF to email header.
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
#Attach the header to email.
msg.attach(part)
#Starting Gmail server.
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
#LogIn Attempt to the from_address email.
server.login(from_address, password)
text = msg.as_string()
#Push the email.
server.sendmail(from_address, to_address, text)
#Quit the server after sending email.
server.quit()
#Deleting the saved PDF file from DISK.
os.remove(filename)
return True
if __name__ == '__main__':
filename = sys.argv[2]
URL = sys.argv[1]
#The email added to trusted email in Kindle settings.
from_address = "YOUR_TRUSTED_GMAIL"
#The Kindle email.
to_address = "YOUR_KINDLE_EMAIL"
pdfobj = re.compile(r'.pdf$')
httpobj = re.compile(r'^http')
#Add '.pdf' to file at the end, if not supplied as cmd line arguments e.g. 'thefirstbook' to 'thefirstbook.pdf'
if not pdfobj.search(filename):
filename += '.pdf'
#Add connection adapter to the URL if not present in cmd line argument e.g. 'www.google.com' to 'http://www.google.com'.
if not httpobj.search( URL ):
URL = 'http://' + URL
URL_to_PDF( URL, filename, pdfobj )
if email_to_kindle( URL, filename, from_address, to_address ):
print "The document has been added to the kindle."
pynotify.init('mraduldubey')
notify = pynotify.Notification('webpage2kindle','The document has been added to the kindle.',os.path.join(os.getcwd(),'kindle.png'))
notify.show()