-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·129 lines (107 loc) · 3.76 KB
/
main.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
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
import re, datetime, time
import smtplib
import codecs
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os, sys, subprocess
from util import *
from config import *
def attachFilesToMessages(msg, files):
for f in files:
part = MIMEBase('application', "octet-stream")
content = open(f,"rb").read()
print type(content)
part.set_payload(content)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(f))
msg.attach(part)
def createMessage(to, subject):
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
return msg
def sendfiletokindle(files):
import smtplib
server = smtplib.SMTP( "smtp.gmail.com" )
server.ehlo()
server.starttls()
server.ehlo()
server.login(FROM, FROM_PASSWORD)
msg = createMessage([KINDLE_MAIL], "file-update")
attachFilesToMessages(msg, files)
print("sending %s to kindle" % files)
server.sendmail(FROM, [KINDLE_MAIL], msg.as_string())
server.close()
def smart_truncate(content, length=30, suffix='...'):
if len(content) <= length:
return content
else:
print "truncating " + content
return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
def get_immediate_subdirectories(dir):
return [name for name in os.listdir(dir)
if os.path.isdir(os.path.join(dir, name))]
def main():
x = None
env = os.environ
if "DISPLAY" not in env:
print "starting temporary Xserver"
x = subprocess.Popen("exec Xvfb :2 -screen 0 800x600x24", shell=True)
env["DISPLAY"] = "localhost:2"
files = []
conn = sqlite3.connect(os.path.join(FILESTORAGE, "items.db"))
cursor = conn.cursor()
items = cursor.execute("SELECT name FROM feeds").fetchall()
print items
for item in items:
[folder] = item
head = """<html>
<head>
<title>%s - %s</title>
</head>
<body>
""" % (folder, time.strftime("%Y-%m-%d %H.%M.%S"))
tail = "</body>\n</html>"
index = "<ul>"
content = ""
i = 0
obj = cursor.execute("SELECT id, title, date, content FROM items WHERE feedname = ? AND read='FALSE'", (folder, )).fetchall()
#obj = [ name for name in os.listdir(folder) if name.endswith(".json") ]
if len(obj) == 0:
continue
for itemfile in obj:
[i, itemtitle, itemdate, itemcontent] = itemfile
index += "<li>%s: <a href=\"#i%d\">%s</a></li>\n" % (itemdate, i, itemtitle)
content += "<a name=\"i%d\"><h1>%s</h1></a>\n" % (i, itemtitle)
content += itemcontent
content += "<br/><hr/><br/>"
cursor.execute("UPDATE items SET read='TRUE' WHERE id=?", (i, ))
index += "</ul>"
fname = "%s-%s.html" % (folder, time.strftime("%Y-%m-%d_%H.%M.%S"))
fname = os.path.join(FILESTORAGE, fname)
f = codecs.open(fname, 'w', encoding='utf-8')
f.write(head)
f.write(index)
f.write(content)
f.write(tail)
f.close()
mobiname = re.sub(".html$", ".mobi", fname)
subprocess.call( ["ebook-convert", fname, mobiname] , env = env)
files.append(mobiname)
conn.commit()
if x != None:
x.terminate()
if len(files) == 0:
return
sendfiletokindle(files)
if __name__ == "__main__":
main()