forked from zenwarr/vk-dialogue-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
65 lines (47 loc) · 1.61 KB
/
utils.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
import datetime
import html
import os
def fmt_time(secs):
return str(datetime.timedelta(seconds=secs))
def fmt_timestamp(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
def fmt_date_diff(diff, add_sign=False):
units = [('s', 60), ('m', 60), ('h', 24), ('d', 0)]
cur = diff
result = ''
for unit in units:
rem = cur % unit[1] if unit[1] > 0 else cur
new_result = '{0}{1} {2}'.format(rem, unit[0], result)
if unit[1] == 0 or cur <= 0:
if rem != 0:
result = new_result
break
result = new_result
cur = (cur - rem) // unit[1]
if add_sign:
result = ('+' if diff > 0 else '-') + result
return result
def fmt_size(size):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(size) < 1024.0:
return "%3.1f%sB" % (size, unit)
size /= 1024.0
return "%.1f%sB" % (size, 'Yi')
def esc(text):
return html.escape(text)
def guess_image_ext(content_type):
return {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif'
}.get(content_type.lower(), 'jpg')
def has_downloaded_image(download_dir, filename):
def is_downloaded_image(existing_file):
return os.path.splitext(existing_file)[0] == filename
try:
downloaded_file = next(f for f in os.listdir(download_dir) if is_downloaded_image(f))
if os.stat(os.path.join(download_dir, downloaded_file)).st_size > 0:
return downloaded_file
return None
except StopIteration:
return None