forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
343 lines (268 loc) · 9.39 KB
/
functions.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from flask import Flask, render_template, session, request, redirect, flash, url_for, Blueprint, g
import urllib.parse
import random
import json
import re
import jinja2
import html
import config
import os.path
import os
from markdown import markdown
from bleach import clean
from datetime import datetime, timedelta
from math import log
import time
legal_chars = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
def thumb_exists(tid):
if os.path.exists('static/thumbnails/thumb-%s.PNG' % str(tid)):
if int(os.stat('static/thumbnails/thumb-%s.PNG' % str(tid)).st_size) > 10:
return True
return False
def rstring(length1, length2=False):
if length2 == False:
length2 = length1
return ''.join([random.choice(legal_chars) for n in range(random.randint(length1, length2))])
def verify_username(username):
if len(username) > 20 or len(username) < 1:
return False
for c in username:
if c not in legal_chars:
return False
return True
def verify_subname(subname):
if len(subname) > 30 or len(subname) < 1:
return False
for c in subname:
if c not in legal_chars:
return False
return True
def convert_ied(url):
url = url.lower()
url = url.replace(' ', '_')
url = url.replace('/', '')
url = urllib.parse.quote(url)[:40]
url = url.replace('%', '')
return url
def post_url_parse(url):
return urllib.parse.urlparse(url).netloc
def get_time():
from datetime import datetime
return datetime.now()
def time_ago(dt):
diff = get_time() - dt
if diff.days > 0:
return str(int(diff.days)) + 'd'#ays ago'
if diff.seconds < 60:
return str(int(diff.seconds)) + 's'#econds ago'
if diff.seconds > 60 and diff.seconds < 3600:
return str(int(diff.seconds / 60)) + 'm'#inutes ago'
if diff.seconds > 3600 and diff.seconds < 86400:
return str(int((diff.seconds / 60) / 60)) + 'h'#ours ago'
# horribly ineffecient function lol
# like no seriously, this is really, really bad
# will change after release
def create_id_tree(comments, parent_id=None):
tree = {}
if not parent_id:
for i in [c for c in comments if c.parent_id == None]:
tree[i.id] = {}
else:
tree[int(parent_id)] = {}
paths = {0:['tree']}
depth = 0
limit = 50
while len(comments) > 0:
paths[depth+1] = []
if depth == 0:
for z in tree.keys():
paths[1].append('tree[' + str(z) + ']')
comments = [a for a in comments if a.id != z]
else:
for p in paths[depth-1]:
for com in [c for c in comments]:
try:
exec(p + '[' + str(com.parent_id ) + ']')
exec_txt = p + '[' + str(com.parent_id ) + '][' + str(com.id) + '] = {}'
exec(exec_txt)
paths[depth].append(p + '[' + str(com.parent_id ) + ']')
comments.remove(com)
except KeyError as e:
pass
depth += 1
if depth >= limit:
break
return tree
def recursive_items(dictionary):
for key, value in dictionary.items():
if type(value) is dict:
yield from recursive_items(value)
else:
yield (key, value)
def comment_structure(comments, tree):
new = {}
for k, v in tree.items():
cobj = next((x for x in comments if x.id == k), None)
new[cobj] = comment_structure(comments, v)
return new
# i hate math
def split_link(sst, s):
new_s = []
sindex = s.find(sst[0])
new_s.append(s[:sindex-1])
new_s.append(s[sindex:sindex + len(sst[0])])
sindex = s.find(sst[1])
new_s.append(s[(sindex):sindex+len(sst[1])])
new_s.append(s[sindex+len(sst[1])+1:])
return new_s
def get_tag_count(text):
tag_count = len(re.findall('<[a-zA-Z0-9]*>[^<>"\']*<\/[a-zA-Z0-9]*>', text))
tag_count += text.count('<br>')
if text.find('\n') != -1:
tag_count += text.count('\n')
elif text.find('\r\n') != -1:
tag_count += text.count('\r\n')
return tag_count
def pseudo_markup(text):
# preserve more than 1 newline
text_len = len(text)
mtext = text.splitlines()
# if text is too long, too many tagss, etc
text = html.escape(text).replace('<br>', '').replace('\n', '<br>')
max_escaped_len = 40000
max_tag_count = 500
current_tag_count = 0
current_len = 0
for i in range(0, len(mtext)):
mtext[i] = clean(markdown(mtext[i]), strip=True)
regstrs = ['<[^liu].*>.*<\/[^liu]>', '^<[^liu].*>', '<\/[^liu].*>$']
reg2 = ['<li>.*<\/li>', '<ul>.*<\/ul>', '<code>.*<\/code>', '<blockquote>.*<\/blockquote>']
addbr = True
addbr2 = True
for reg in regstrs:
if len(re.findall(reg, mtext[i])) != 0:
addbr2 = True
for r2 in reg2:
if len(re.findall(r2, mtext[i])) != 0:
addbr2 = False
else:
addbr = True
if addbr and addbr2:
if len(re.findall('^https?:\/\/.*.*$', mtext[i])) == 0:
mtext[i] = mtext[i] + '<br>'
current_len += len(mtext[i])
current_tag_count += get_tag_count(mtext[i])
if current_len >= max_escaped_len or current_tag_count >= max_tag_count:
return text
# code tags
start, end = 0, 0
found = False
for i in range(len(mtext)):
if mtext[i].find('```') != -1:
#
if not found:
startindex = i
start = mtext[i].find('```')
mtext[i] = mtext[i].replace('```','')
found = True
else:
end = mtext[i].find('```')
mtext[startindex] = mtext[startindex][0:start] + '<pre class="inline-code"><code class="inner-code">' + mtext[startindex][start:]
mtext[i] = mtext[i][0:end] + '</code></pre>' + mtext[i][end+3:]
current_len += len(mtext[startindex])
current_len += len(mtext[i])
found = False
start = 0
end = 0
current_len += len(mtext[i])
current_tag_count += get_tag_count(mtext[i])
if current_len >= max_escaped_len or current_tag_count >= max_tag_count:
return text
for i in range(len(mtext)):
# different variations of possible links, space at start, no
# space, etc
links = re.findall(' https?:\/\/.*\.\S+ ', mtext[i])
if len(links) > 0:
for link in links:
mtext[i] = mtext[i].replace(link, ' <a href="%s">%s</a> ' % (html.escape(link).replace(' ', ''), html.escape(link).replace(' ', '')))
links = re.findall('^https?:\/\/.*\.\S+ ', mtext[i])
if len(links) > 0:
for link in links:
mtext[i] = mtext[i].replace(link, '<a href="%s">%s</a> ' % (html.escape(link).replace(' ', ''), html.escape(link).replace(' ', '')))
links = re.findall('^https?:\/\/.*\.\S+$', mtext[i])
if len(links) > 0:
for link in links:
mtext[i] = mtext[i].replace(link, '<a href="%s">%s</a>' % (html.escape(link).replace(' ', ''), html.escape(link).replace(' ', '')))
links = re.findall(' https?:\/\/.*\.\S+$', mtext[i])
if len(links) > 0:
for link in links:
mtext[i] = mtext[i].replace(link, '<a href="%s"> %s</a>' % (html.escape(link).replace(' ', ''), html.escape(link).replace(' ', '')))
links = re.findall(' https?:\/\/.*\.\S+$', mtext[i])
if len(links) > 0:
for link in links:
mtext[i] = mtext[i].replace(link, '<a href="%s"> %s</a>' % (html.escape(link).replace(' ', ''), html.escape(link).replace(' ', '')))
if mtext[i].find('<br>') == -1:
if len(re.findall('$<a> href=".*"<br>', mtext[i])) == 0:
mtext[i] += '<br>'
current_len += len(mtext[i])
current_tag_count += get_tag_count(mtext[i])
if current_len >= max_escaped_len or current_tag_count >= max_tag_count:
return text
mtext = '\n'.join([x for x in mtext])
repstrings = ['</blockquote>']
for i in range(len(mtext)):
if i > 0:
past = mtext[i-1]
for rep in repstrings:
if past.find(rep) == -1:
pass
else:
mtext[i].replace('<br>', '')
mtext = mtext.replace('\n<div class="inline-code"><code>\n', '<div class="inline-code"><code>')
mtext = mtext.replace('<code>\n', '<code>')
mtext = mtext.replace('\n</code></div>\n', '</code></div>')
#mtext = mtext.replace('\n\n</code>', '</code>')
mtext = mtext.replace('\n</code>', '</code>')
#mtext = mtext.replace('</code>\n<code>', '</code><code>')
#mtext = mtext.replace('</code>\n', '</code>')
#mtext = mtext.replace('\n<code>', '\n </code>')
mtext = mtext.replace('<code>', '<code> ')
mtext = mtext.replace('</blockquote>\n<br>', '</blockquote>\n')
mtext = mtext.replace('</blockquote><br>', '</blockquote>')
mtext = mtext.replace('</pre><br>', '</pre>')
mtext = mtext.replace('<br>' +'</a>', '')
mtext = mtext.replace('<br>">', '">')
mtext = mtext.replace('<br>">', '">')
mtext = mtext.replace('&lt;br&gt;', '')
mtext = mtext.replace('</a></a>', '')
mtext = mtext.replace('</a>">', '">')
mtext = mtext.replace('</a></a>', '</a>')
if mtext == '':
return '<br>'
return mtext
epoch = datetime(1970, 1, 1)
def epoch_seconds(date):
td = date - epoch
return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
def score(ups, downs):
return ups - downs
def hot(ups, downs, date):
s = score(ups, downs)
order = log(max(abs(s), 1), 10)
sign = 1 if s > 0 else -1 if s < 0 else 0
seconds = epoch_seconds(date) - 1134028003
return round(sign * order + seconds / 45000, 7)
def get_youtube_vid_id(url):
if url == None:
return False
if url.find('youtube.com/watch?v=') != -1:
return url.split('=')[1]
if url.find('youtube.com/v/') != -1:
url = url.split('/v/')[1]
if url.find('?') == -1:
return url.split('?')[0]
else:
return url
if url.find('youtu.be/') != -1:
return url.split('.be/')[1]
return False