-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
67 lines (56 loc) · 1.82 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
import os
from enum import Enum
from urllib.parse import urlparse
class Status(Enum):
active = '正在下载'
waiting = '等待下载'
paused = '暂停'
error = '错误'
complete = '完成'
removed = '已删除'
def getEmByName(key):
for v in Status:
if v.name == key:
return v.value
def hum_convert(value):
units = ["B", "KB", "MB", "GB", "TB", "PB"]
size = 1024.0
for i in range(len(units)):
if (value / size) < 1:
return "%.2f%s" % (value, units[i])
value = value / size
def byte2Readable(size):
'''
auth: [email protected] ;科达柯大侠
递归实现,精确为最大单位值 + 小数点后三位
'''
def strofsize(integer, remainder, level):
if integer >= 1024:
remainder = integer % 1024
integer //= 1024
level += 1
return strofsize(integer, remainder, level)
else:
return integer, remainder, level
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
integer, remainder, level = strofsize(size, 0, 0)
if level + 1 > len(units):
level = -1
return ('{}.{:>03d}{}'.format(integer, remainder, units[level]))
def progress(total_length, completed_length):
if total_length != 0:
return '{:.2f}%'.format(completed_length / total_length * 100)
else:
return "0"
def getFileName(task):
if task.__contains__('bittorrent'):
if task['bittorrent'].__contains__('info'):
# bt下载
return task['bittorrent']['info']['name']
# bt元信息
return task['files'][0]['path']
filename = task['files'][0]['path'].split('/')[-1]
if filename == '':
pa = urlparse(task['files'][0]['uris'][0]['uri'])
filename = os.path.basename(pa.path)
return filename