Skip to content

Commit 82ae49a

Browse files
committed
Re implement upload progress after rebase
Signed-off-by: lzzy12 <[email protected]>
1 parent 19b6f91 commit 82ae49a

17 files changed

+243
-157
lines changed

bot/__init__.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import threading
44
import os
55
from dotenv import load_dotenv
6-
from telegram import Bot
76
import telegram.ext as tg
87
import time
98

@@ -20,6 +19,7 @@
2019

2120
Interval = []
2221

22+
2323
def getConfig(name: str):
2424
return os.environ[name]
2525

@@ -41,7 +41,6 @@ def getConfig(name: str):
4141
)
4242
)
4343

44-
4544
DOWNLOAD_DIR = None
4645
BOT_TOKEN = None
4746

@@ -74,8 +73,6 @@ def getConfig(name: str):
7473
LOGGER.error("One or more env variables missing! Exiting now")
7574
exit(1)
7675

77-
bot = Bot(BOT_TOKEN)
7876
updater = tg.Updater(token=BOT_TOKEN)
77+
bot = updater.bot
7978
dispatcher = updater.dispatcher
80-
81-

bot/helper/ext_utils/bot_utils.py

+27-20
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import re
44
import threading
55
import time
6-
import math
76

87
LOGGER = logging.getLogger(__name__)
98

109
MAGNET_REGEX = r"magnet:\?xt=urn:btih:[a-zA-Z0-9]*"
1110

1211
URL_REGEX = r"(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+"
1312

13+
1414
class MirrorStatus:
1515
STATUS_UPLOADING = "Uploading"
1616
STATUS_DOWNLOADING = "Downloading"
@@ -25,23 +25,25 @@ class MirrorStatus:
2525

2626
SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
2727

28-
class setInterval :
29-
def __init__(self,interval,action) :
30-
self.interval=interval
31-
self.action=action
32-
self.stopEvent=threading.Event()
33-
thread=threading.Thread(target=self.__setInterval)
28+
29+
class setInterval:
30+
def __init__(self, interval, action):
31+
self.interval = interval
32+
self.action = action
33+
self.stopEvent = threading.Event()
34+
thread = threading.Thread(target=self.__setInterval)
3435
thread.start()
3536

36-
def __setInterval(self) :
37-
nextTime=time.time()+self.interval
38-
while not self.stopEvent.wait(nextTime-time.time()) :
39-
nextTime+=self.interval
37+
def __setInterval(self):
38+
nextTime = time.time() + self.interval
39+
while not self.stopEvent.wait(nextTime - time.time()):
40+
nextTime += self.interval
4041
self.action()
4142

42-
def cancel(self) :
43+
def cancel(self):
4344
self.stopEvent.set()
4445

46+
4547
def get_readable_file_size(size_in_bytes) -> str:
4648
index = 0
4749
while size_in_bytes >= 1024:
@@ -65,10 +67,10 @@ def get_download_status_list():
6567

6668
def get_progress_bar_string(status):
6769
if status.status() == MirrorStatus.STATUS_UPLOADING:
68-
completed = status.upload_helper.uploaded_bytes / 8
70+
completed = status.obj.uploaded_bytes / 8
6971
else:
7072
completed = status.download().completed_length / 8
71-
total = status.download().total_length / 8
73+
total = status.size_raw() / 8
7274
if total == 0:
7375
p = 0
7476
else:
@@ -99,6 +101,7 @@ def get_download_str():
99101
result += (status.progress() + status.speed() + status.status())
100102
return result
101103

104+
102105
def get_readable_message():
103106
with download_dict_lock:
104107
msg = ""
@@ -110,11 +113,15 @@ def get_readable_message():
110113
msg += "Archiving\n\n"
111114
elif download.status() == MirrorStatus.STATUS_WAITING:
112115
msg += "Queued\n\n"
113-
else:
114-
msg += f"<code>{get_progress_bar_string(download)} {download.progress()}</code> of {get_readable_file_size(download.download().total_length)}" \
115-
f" at {get_readable_file_size(download.download().download_speed)}ps, ETA: {download.eta()}\n\n"
116+
elif download.status() == MirrorStatus.STATUS_DOWNLOADING:
117+
msg += "Downloading"
118+
if download.status() != MirrorStatus.STATUS_ARCHIVING:
119+
msg += f"<code>{get_progress_bar_string(download)} {download.progress()}</code> of " \
120+
f"{download.size()}" \
121+
f" at {download.speed()}ps, ETA: {download.eta()}\n\n"
116122
return msg
117123

124+
118125
def get_readable_time(seconds: int) -> str:
119126
result = ''
120127
(days, remainder) = divmod(seconds, 86400)
@@ -135,14 +142,14 @@ def get_readable_time(seconds: int) -> str:
135142

136143

137144
def is_url(url: str):
138-
url = re.findall(URL_REGEX,url)
145+
url = re.findall(URL_REGEX, url)
139146
if url:
140147
return True
141-
return False
148+
return False
142149

143150

144151
def is_magnet(url: str):
145-
magnet = re.findall(MAGNET_REGEX,url)
152+
magnet = re.findall(MAGNET_REGEX, url)
146153
if magnet:
147154
return True
148155
return False

bot/helper/ext_utils/fs_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66
import mimetypes
77

8+
89
def clean_download(path: str):
910
if os.path.exists(path):
1011
LOGGER.info(f"Cleaning download: {path}")
@@ -41,4 +42,3 @@ def get_mime_type(file_path):
4142
mime_type = mimetypes.guess_type(file_path)[0]
4243
mime_type = mime_type if mime_type else "text/plain"
4344
return mime_type
44-

bot/helper/mirror_utils/download_utils/__init__.py

Whitespace-only changes.

bot/helper/mirror_utils/aria2_download.py renamed to bot/helper/mirror_utils/download_utils/aria2_download.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from bot import DOWNLOAD_STATUS_UPDATE_INTERVAL, aria2, Interval
1+
from bot import aria2
22
from bot.helper.ext_utils.bot_utils import *
33
from .download_helper import DownloadHelper
4-
from .download_status import DownloadStatus
4+
from bot.helper.mirror_utils.status_utils.aria_download_status import AriaDownloadStatus
55
from bot.helper.telegram_helper.message_utils import *
66
import threading
77
from aria2p import API
88

9+
910
class AriaDownloadHelper(DownloadHelper):
1011

1112
def __init__(self, listener):
@@ -26,11 +27,11 @@ def __onDownloadComplete(self, api: API, gid):
2627
if self.gid == gid:
2728
if api.get_download(gid).followed_by_ids:
2829
self.gid = api.get_download(gid).followed_by_ids[0]
29-
download_dict[self._listener.uid] = DownloadStatus(self.gid,self._listener)
30+
download_dict[self._listener.uid] = AriaDownloadStatus(self.gid, self._listener)
3031
update_all_messages()
3132
LOGGER.info(f'Changed gid from {gid} to {self.gid}')
3233
else:
33-
self._listener.onDownloadComplete()
34+
self._listener.onDownloadComplete()
3435

3536
def __onDownloadPause(self, api, gid):
3637
if self.gid == gid:
@@ -56,14 +57,13 @@ def add_download(self, link: str, path):
5657
download = aria2.add_uris([link], {'dir': path})
5758
self.gid = download.gid
5859
with download_dict_lock:
59-
download_dict[self._listener.uid] = DownloadStatus(self.gid,self._listener)
60+
download_dict[self._listener.uid] = AriaDownloadStatus(self.gid, self._listener)
6061
if download.error_message:
6162
self._listener.onDownloadError(download.error_message)
62-
return
63+
return
6364
LOGGER.info(f"Started: {self.gid} DIR:{download.dir} ")
6465
aria2.listen_to_notifications(threaded=True, on_download_start=self.__onDownloadStarted,
65-
on_download_error=self.__onDownloadError,
66-
on_download_pause=self.__onDownloadPause,
67-
on_download_stop=self.__onDownloadStopped,
68-
on_download_complete=self.__onDownloadComplete)
69-
66+
on_download_error=self.__onDownloadError,
67+
on_download_pause=self.__onDownloadPause,
68+
on_download_stop=self.__onDownloadStopped,
69+
on_download_complete=self.__onDownloadComplete)

bot/helper/mirror_utils/status_utils/__init__.py

Whitespace-only changes.

bot/helper/mirror_utils/download_status.py renamed to bot/helper/mirror_utils/status_utils/aria_download_status.py

+5-29
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
from bot import aria2, DOWNLOAD_DIR
22
from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus, get_readable_time
3+
from .download_status import DownloadStatus
34

45

56
def get_download(gid):
67
return aria2.get_download(gid)
78

89

9-
class DownloadStatus:
10+
class AriaDownloadStatus(DownloadStatus):
1011

1112
def __init__(self, gid, listener):
13+
super().__init__()
1214
self.upload_name = None
1315
self.is_archiving = False
1416
self.__gid = gid
1517
self.__download = get_download(gid)
1618
self.__uid = listener.uid
1719
self._listener = listener
18-
self.upload_helper = None
1920
self.last = None
2021
self.is_waiting = False
2122

@@ -28,35 +29,20 @@ def progress(self):
2829
:return: returns progress in percentage
2930
"""
3031
self.__update()
31-
if self.upload_helper is not None:
32-
return f'{round(self.upload_progress(), 2)}%'
3332
return self.__download.progress_string()
3433

35-
def upload_progress(self):
36-
return self.upload_helper.uploaded_bytes / self.download().total_length * 100
37-
38-
def __size(self):
34+
def size_raw(self):
3935
"""
4036
Gets total size of the mirror file/folder
4137
:return: total size of mirror
4238
"""
4339
return self.download().total_length
4440

45-
def __upload_speed(self):
46-
"""
47-
:return: Upload speed in Bytes/Seconds
48-
"""
49-
return self.upload_helper.speed()
50-
5141
def speed(self):
5242
self.__update()
53-
if self.upload_helper is not None:
54-
return f'{get_readable_file_size(self.__upload_speed())}/s'
5543
return self.__download.download_speed_string()
5644

5745
def name(self):
58-
if self.upload_name is not None:
59-
return self.upload_name
6046
self.__update()
6147
return self.__download.name
6248

@@ -68,24 +54,14 @@ def size(self):
6854

6955
def eta(self):
7056
self.__update()
71-
if self.upload_helper is not None:
72-
try:
73-
seconds = (self.__size() - self.upload_helper.uploaded_bytes) / self.__upload_speed()
74-
return f'{get_readable_time(seconds)}'
75-
except ZeroDivisionError:
76-
return '-'
7757
return self.__download.eta_string()
7858

7959
def status(self):
8060
self.__update()
81-
if self.is_archiving:
82-
status = MirrorStatus.STATUS_ARCHIVING
83-
elif self.download().is_waiting:
61+
if self.download().is_waiting:
8462
status = MirrorStatus.STATUS_WAITING
8563
elif self.download().is_paused:
8664
status = MirrorStatus.STATUS_CANCELLED
87-
elif self.upload_helper is not None:
88-
status = MirrorStatus.STATUS_UPLOADING
8965
elif self.__download.has_failed:
9066
status = MirrorStatus.STATUS_FAILED
9167
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generic status class. All other status classes must inherit this class
2+
3+
4+
class DownloadStatus:
5+
6+
def progress(self):
7+
"""
8+
Calculates the progress of the mirror (upload or download)
9+
:return: progress in percentage
10+
"""
11+
raise NotImplementedError
12+
13+
def speed(self):
14+
""":return: speed in bytes per second"""
15+
raise NotImplementedError
16+
17+
def name(self):
18+
""":return name of file/directory being processed"""
19+
raise NotImplementedError
20+
21+
def path(self):
22+
""":return path of the file/directory"""
23+
raise NotImplementedError
24+
25+
def size(self):
26+
""":return Size of file folder"""
27+
raise NotImplementedError
28+
29+
def eta(self):
30+
""":return ETA of the process to complete"""
31+
raise NotImplementedError
32+
33+
def status(self):
34+
""":return String describing what is the object of this class will be tracking (upload/download/something
35+
else) """
36+
raise NotImplementedError

bot/helper/mirror_utils/listeners.py renamed to bot/helper/mirror_utils/status_utils/listeners.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def onDownloadComplete(self):
1717
def onDownloadError(self, error: str):
1818
raise NotImplementedError
1919

20-
def onUploadStarted(self, progress_status_list: list, index: int):
20+
def onUploadStarted(self):
2121
raise NotImplementedError
2222

23-
def onUploadProgress(self, progress: list, index: int):
23+
def onUploadProgress(self):
2424
raise NotImplementedError
2525

26-
def onUploadComplete(self, link: str, progress_status_list: list, index: int):
26+
def onUploadComplete(self, link: str):
2727
raise NotImplementedError
2828

29-
def onUploadError(self, error: str, progress_status: list, index: int):
30-
raise NotImplementedError
29+
def onUploadError(self, error: str):
30+
raise NotImplementedError
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from .download_status import DownloadStatus
2+
from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus
3+
4+
5+
class TarStatus(DownloadStatus):
6+
def __init__(self, name, path, size):
7+
self.__name = name
8+
self.__path = path
9+
self.__size = size
10+
11+
def progress(self):
12+
return '0'
13+
14+
def speed(self):
15+
return '0'
16+
17+
def name(self):
18+
return self.__name
19+
20+
def path(self):
21+
return self.__path
22+
23+
def size(self):
24+
return get_readable_file_size(self.__size)
25+
26+
def eta(self):
27+
return '0s'
28+
29+
def status(self):
30+
return MirrorStatus.STATUS_ARCHIVING
31+

0 commit comments

Comments
 (0)