forked from FanFicDev/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
353 lines (285 loc) · 8.21 KB
/
util.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
344
345
346
347
348
349
350
351
352
353
import os
import re
import math
import random
import datetime
import dateutil.parser
import time
import PyQt5.QtCore
from typing import List, Union
from schema import OilTimestamp
defaultLogFile = 'hermes.log'
defaultLogDir = './'
rippleCharset = 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'
urlIdCharset = 'abcdefghijklmnopqrstuvwxyz23456789'
def unslurp(text: str, fname: str, path: str = defaultLogDir) -> None:
if not os.path.isdir(path):
os.makedirs(path)
fname = os.path.join(path, fname)
with open(fname, 'w') as f:
f.write(text)
def getNumberLength(num: int) -> int:
return int(math.ceil(math.log(num) / math.log(10)))
def formatNumber(num: int) -> str:
b = '{}'.format(num)
b = '{:>{}}'.format(b, int((len(b) + 2) / 3) * 3)
n = ''
for i in range(int(len(b) / 3)):
n += b[i * 3:(i + 1) * 3] + ','
return n[:-1].lstrip()
def urlTitle(title: str) -> str:
ut = ''
for char in title:
if char.isalnum():
ut += char
elif len(ut) == 0 or ut[-1] != '-':
ut += '-'
return ut.rstrip('-')
def randomString(length: int = 2, charset: str = None) -> str:
charset = rippleCharset if charset is None else charset
res = ''
for i in range(length):
res += random.choice(charset)
return res
def subsequenceMatch(seq: str, needle: str) -> bool:
nl, sl = len(needle), len(seq)
if nl > sl:
return False
if nl == sl:
return needle == seq
if nl == 0:
return True
if nl == 1:
return seq.find(needle) != -1
nextStart = seq.find(needle[0])
if nextStart == -1:
return False
return subsequenceMatch(seq[nextStart + 1:], needle[1:])
def dtToUnix(dt: datetime.datetime) -> int:
return int(dt.strftime('%s'))
_writtenMonths = [
# full
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
# abbreviated
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
def isWrittenDate(val: str) -> bool:
for wm in _writtenMonths:
if val.find(wm) >= 0:
return True
return False
def parseDateAsUnix(
updated: Union[OilTimestamp, str, int],
fetched: Union[OilTimestamp, int],
defaultYear: int = None
) -> int:
if isinstance(updated, OilTimestamp):
return updated.toUTS()
if isinstance(fetched, OilTimestamp):
fetched = fetched.toUTS()
currentYear = datetime.datetime.now().year
if defaultYear is not None:
currentYear = defaultYear
if isinstance(updated, int):
return updated
if isinstance(updated, str):
updated = updated.strip()
if re.match('^\d+$', updated):
return int(updated)
if updated.endswith('ago'):
updated = updated[:-len('ago')]
updated = updated.strip()
if re.match('^\d+m$', updated):
return fetched - (60 * int(updated[:-1]))
if re.match('^\d+h$', updated):
return fetched - (60 * 60 * int(updated[:-1]))
if re.match('^just', updated):
return fetched
slashedParts = updated.split('/')
if len(slashedParts) == 2:
fdate = ('{}/{}/{}'.format(currentYear, slashedParts[0], slashedParts[1]))
dt = (dateutil.parser.parse(fdate))
uts = dtToUnix(dt)
return uts
if len(slashedParts) == 3:
dt = dateutil.parser.parse(updated)
uts = dtToUnix(dt)
return uts
dashedParts = updated.split('-')
if len(dashedParts) == 3:
dt = dateutil.parser.parse(updated)
uts = dtToUnix(dt)
return uts
dottedParts = updated.split('.')
if (
len(dottedParts) == 3 and dottedParts[0].isnumeric()
and dottedParts[1].isnumeric() and dottedParts[2].isnumeric()
):
dt = dateutil.parser.parse(updated)
uts = dtToUnix(dt)
return uts
if isWrittenDate(updated):
dt = dateutil.parser.parse(updated)
uts = dtToUnix(dt)
return uts
logMessage('error parsing date: {}'.format(updated))
raise Exception('error parsing date: {}'.format(updated))
def logMessage(msg: str, fname: str = None, logDir: str = None) -> None:
if fname is None:
fname = defaultLogFile
if logDir is None:
logDir = defaultLogDir
if not msg.endswith('\n'):
msg += '\n'
lname = os.path.join(logDir, fname)
with open(lname, 'a+') as f:
f.write(str(int(time.time())) + '|' + msg)
# TODO this is a hacky workaround for shared log files
try:
os.chmod(lname, 0o666) # rw-rw-rw-
except:
pass
# generic word wrapping algorithm
# TODO should we be using the textwrap module instead? >_>
def wrapText(text: str, width: int) -> List[str]:
if len(text) == 0:
return [' ' * width]
lines: List[str] = []
while len(text) > width:
# find nearest space to break on
if len(lines) > 0:
text = text.strip()
col = text.rfind(' ', 0, width - 1) # reserve space for hyphenated words
col = max(col, text.rfind('-', 0, width - 1))
# if it's the first line and we find the indent, don't count it
# TODO: other size indents
if len(lines) == 0 and col == 0:
col = -1
# ran off start => very long word, hyphenate
if col == -1:
col = width - 1
lines += [text[0:col] + '-']
text = text[col:]
continue
# append up to word break into lines
if col < len(text) and text[col] == '-':
lines += ['{line:{width}}'.format(line=text[0:col + 1], width=width)]
else:
lines += ['{line:{width}}'.format(line=text[0:col], width=width)]
# remove processed text sans space
text = text[col + 1:]
if len(lines) > 0:
text = text.strip()
# if there's anything left, it's a line by itself
if len(text) > 0:
lines += ['{line:{width}}'.format(line=text, width=width)]
return lines
spaceSqeeezeRe = None
ellipseSqueezeRe = None
ellipseSpaceRe = None
# convert unicode to ascii for display
def filterUnicode(line: str) -> str:
global spaceSqeeezeRe, ellipseSqueezeRe, ellipseSpaceRe
if spaceSqeeezeRe is None:
spaceSqeeezeRe = re.compile('\s{2,}')
if ellipseSqueezeRe is None:
ellipseSqueezeRe = re.compile('(…\s*){2,}')
if ellipseSpaceRe is None:
punctuation = '"”?\\.\'\\)_*'
ellipseSpaceRe = re.compile('…([^ {}])'.format(punctuation))
# remove some unicode characters
# ['“', '”'] ['‘', '’'] "…"
for d in ['–', '—', '-', '', '―']:
line = line.replace(d, '-')
for rm in ['■']:
line = line.replace(rm, '')
for apo in ['ʼ', 'ʻ']:
line = line.replace(apo, "'")
for dq in ['❝', '❞']:
line = line.replace(dq, '"')
# move bold/italic past dots for ellipses squeeze
#line = re.sub('([_*]+)(\.+)', '\\2\\1')
# convert ellipses to unicode ellipses
line = line.replace('...', '…')
line = line.replace('. . .', '…')
# squeeze extra ellipses
line = ellipseSqueezeRe.sub('…', line)
# remove extra space before punctuation
line = line.replace(' …', '…')
line = line.replace(' ,', ',')
# squeeze strings of repeat spaces
line = spaceSqeeezeRe.sub(' ', line)
# make sure ellipses are followed by a space or punctuation
line = ellipseSpaceRe.sub('… \\1', line)
return line
def filterEmptyTags(line: str) -> str:
# remove empty open/close italics and bolds
line = line.replace('_ _', ' ')
line = line.replace('__', '')
line = line.replace('* *', ' ')
line = line.replace('**', '')
line = line.replace('*"*', '"')
return line
# layout a list of strings so each piece is equidistant from the others
def equiPad(strs: List[str], width: int) -> str:
totalWidth = sum([len(s) for s in strs])
if totalWidth >= width:
return ''.join(strs)
if len(strs) == 1:
return '{:<{}}'.format(strs[0], width)
# TODO: when not divisible...
padded = (' ' * int((width - totalWidth) / (len(strs) - 1))).join(strs)
return '{:{}}'.format(padded, width) # ensure right length for non-divisble
def cleanChapterTitle(title: str, cid: int) -> str:
title = title.strip()
prefixes = [
'{}-'.format(cid),
'{}.'.format(cid),
'chapter {}'.format(cid),
'chapter {}'.format(cid + 1),
'chapter {}'.format(cid - 1),
':',
'-',
]
foundAny = True
while foundAny:
foundAny = False
for pref in prefixes:
if title.lower().startswith(pref):
foundAny = True
title = title[len(pref):].strip()
return title
def compress(s: bytes) -> bytes:
qb = PyQt5.QtCore.QByteArray(s)
res = PyQt5.QtCore.qCompress(qb, 9)
return res.data()
def decompress(b: bytes) -> bytes:
qb = PyQt5.QtCore.QByteArray(b)
res = PyQt5.QtCore.qUncompress(qb)
return res.data()
def decodeCloudFlareEmail(email: str) -> str:
octets = [int(email[i:i + 2], 16) for i in range(0, len(email), 2)]
key, ebytes = octets[0], octets[1:]
return ''.join([chr(o ^ key) for o in ebytes])