forked from JamieMagee/reddit2kindle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
124 lines (95 loc) · 3.87 KB
/
util.py
File metadata and controls
124 lines (95 loc) · 3.87 KB
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
import os
import re
import smtplib
from configparser import ConfigParser
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import praw
import requests
from markdown import markdown
import json
# combine these 2 functions
# clean up comment vs comment_id usage - doing a double GET right now
def to_html_numbers(comment_id, op):
comment = get_item(comment_id)
result = markdown(comment['text']) + \
('<footer class="op">' if comment['by'] == op else '<footer>') + \
comment['by'] + '</footer>'
if 'kids' not in comment:
return result
children = []
for reply_id in comment['kids']:
reply_result = '<li>' + to_html_numbers(reply_id, op) + '</li>'
children.append(reply_result)
result += '<ol>' + ''.join(children) + '</ol>'
return result
# fix this and combine with above
def to_html_quotes(comment_id, op):
result = markdown(comment['text']) + \
('<footer class="op">' if comment['by'] == op else '<footer>') + \
comment['by'] + '</footer>'
children = ['<blockquote>' + to_html_numbers(reply_id, op) + '</blockquote>' for reply_id in comment['kids'] if
'kids' in comment]
if children:
result += ''.join(children)
return result
def get_comments(comment_ids, comments_style, op):
out = '<ol>' if comments_style == 'numbers' else ''
for comment_id in comment_ids:
comment = get_item(comment_id)
if comment['by'] is not None:
if comments_style == 'numbers':
out += '<li>' + to_html_numbers(comment_id, op) + '</li>'
else:
out += '<blockquote>' + to_html_quotes(comment_id, op) + '</blockquote>'
return out + ('</ol>' if comments_style == 'numbers' else '')
def get_auth():
if os.path.isfile(os.path.join(os.path.dirname(__file__), 'settings.cfg')):
config = ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'settings.cfg'))
username = config.get('auth', 'username')
password = config.get('auth', 'password')
else:
username = os.environ['SMTP_USERNAME']
password = os.environ['SMTP_PASSWORD']
return username, password
def get_smtp():
if os.path.isfile(os.path.join(os.path.dirname(__file__), 'settings.cfg')):
config = ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'settings.cfg'))
server = config.get('smtp', 'server')
port = config.get('smtp', 'port')
else:
server = os.environ['SMTP_SERVER']
port = os.environ['SMTP_PORT']
return server, port
def send_email(to, kindle_address, attachment, title):
msg = MIMEMultipart()
msg['From'] = 'hn2kindle@gmail.com'
if kindle_address == 'free':
msg['To'] = to + '@free.kindle.com'
else:
msg['To'] = to + '@kindle.com'
title = "".join(c for c in title if c.isalnum() or c.isspace()).rstrip()
msg['Subject'] = title
attach = MIMEText(attachment.encode('iso-8859-1', 'xmlcharrefreplace'), 'html', 'iso-8859-1')
attach.add_header('Content-Disposition', 'attachment',
filename=title + '.html')
msg.attach(attach)
s = smtplib.SMTP(get_smtp()[0], get_smtp()[1])
s.starttls()
s.login(get_auth()[0], get_auth()[1])
s.send_message(msg)
s.quit()
def validate_request_post(values):
if values['submission'] is '':
return 'You need to put a URL in!'
if values['email'] is '':
return 'How am I supposed to send it to you without an email address?'
if values['kindle_address'] not in ['free', 'normal']:
return 'Which kindle address do you want me to send to?'
return None
def get_item(item_id):
url = 'https://hacker-news.firebaseio.com/v0/item/' + str(item_id) + '.json'
response = requests.get(url)
return json.loads(response.text)