This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
/
main.py
125 lines (95 loc) · 3.51 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
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
from __future__ import unicode_literals
import env
import secrets
import requests
import json
from jinja2 import Template
from os.path import basename, join, abspath
from smtplib import SMTP
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 tempfile
import subprocess as sp
from shutil import rmtree
from flask import Flask, jsonify, request
import codecs
import logging
logging.basicConfig(level=env.LOG_LEVEL, filename=env.LOG_FILE,
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
datefmt='%Y%m%d-%H:%M%p')
log = logging.getLogger('kindlestack')
with open('template.html') as f:
template = Template(f.read())
def get_data(site, question_id):
params = {'site': site, 'filter': '!*KelRYy2aYCTvWZj'}
if hasattr(secrets, 'STACKEXCHANGE_API_KEY'):
params['key'] = secrets.STACKEXCHANGE_API_KEY
response = requests.get(
'https://api.stackexchange.com/2.0/questions/' + question_id,
params=params)
response.raise_for_status()
return json.loads(response.text)
def send_mail(send_to, subject, text, files=[], server='localhost',
username=None, password=None):
send_from = 'kindlestack@sharats.me'
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(f, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % basename(f))
msg.attach(part)
smtp = SMTP(server)
if username is not None:
smtp.login(str(username), str(password))
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
def answer_sort_key(ans):
return ans['score'] + (10 if ans['is_accepted'] else 0)
def send_to_kindle(site, question_id, email):
data = get_data(site, question_id)['items'][0]
if 'answers' in data:
data['answers'].sort(key=answer_sort_key, reverse=True)
else:
data['answers'] = []
tloc = tempfile.mkdtemp(prefix='tmp-kindle-stack-')
with codecs.open(join(tloc, 'rendered.html'), 'w', 'utf-8') as f:
f.write(template.render(data))
sp.call([abspath(env.KINDLEGEN), 'rendered.html', '-o',
'kindle-stacked.mobi'], cwd=tloc)
send_mail([email], 'kindle-stack: ' + data['title'], '-none-',
files=[join(tloc, 'kindle-stacked.mobi')],
server=secrets.SMTP_HOST,
username=secrets.SMTP_USER, password=secrets.SMTP_PASS)
rmtree(tloc)
def make_webapp():
app = Flask(__name__)
with open('index.html') as f:
index_content = f.read()
@app.route('/')
def index():
return index_content
@app.route('/send', methods=['POST'])
def send():
args = request.args if request.method == 'GET' else request.form
site, question, email = args['site'], args['question'], args['email']
log.info('send from ' + site + '/' + question + ' to ' + email)
send_to_kindle(site, question, email)
return jsonify(ok=True)
return app
if __name__ == '__main__':
app = make_webapp()
app.debug = True
app.run(host='localhost', port=5004)