state object and ui thread

This commit is contained in:
Paulo Afonso Pinheiro 2020-04-21 16:18:58 -03:00
parent b8a53233e5
commit 30169758b9

View file

@ -13,7 +13,59 @@ from collections import deque
import subprocess
import platform
import datetime
import threading
def human_readable_size(size, decimal_places=2):
for unit in ['B','KiB','MiB','GiB','TiB']:
if size < 1024.0:
break
size /= 1024.0
return f"{size:.{decimal_places}f}{unit}"
def clear_screen():
if platform.system() == "Windows":
subprocess.Popen("cls", shell=True).communicate()
else:
print("\033c", end="")
class RatioSpoofState():
def __init__(self, torrent_name, percent_complete, download_speed, upload_speed, announce_rate, current_downloaded, current_uploaded):
self.torrent_name = torrent_name
self.percent_complete = percent_complete
self.download_speed = download_speed
self.upload_speed = upload_speed
self.announce_rate = announce_rate
self.announce_history_deq = deque(maxlen=10)
self.deq_count = 0
self.announce_current_timer = self.announce_rate
self.add_announce(current_downloaded, current_uploaded)
threading.Thread(target = (lambda: self.__print_state())).start()
def add_announce(self, current_downloaded, current_uploaded):
self.deq_count +=1
self.announce_history_deq.append({'count': self.deq_count, 'downloaded':current_downloaded,'uploaded':current_uploaded })
def __decrease_timer(self):
self.announce_current_timer = self.announce_current_timer - 1 if self.announce_current_timer > 0 else 0
def reset_timer(self, new_announce_rate = None):
if new_announce_rate != None:
self.announce_rate = new_announce_rate
self.announce_current_timer = self.announce_rate
def __print_state(self):
while True:
print(f"""
###########################################################################
Torrent: {self.torrent_name} - {self.percent_complete}%
download_speed: {self.download_speed}KB/s
upload_speed: {self.upload_speed}KB/s
###########################################################################
""")
for item in list(self.announce_history_deq)[:len(self.announce_history_deq)-1]:
print(f'#{item["count"]} downloaded: {human_readable_size(item["downloaded"])} | uploaded: {human_readable_size(item["uploaded"])} | announced')
print(f'#{self.announce_history_deq[-1]["count"]} downloaded: {human_readable_size(self.announce_history_deq[-1]["downloaded"])} | uploaded: {human_readable_size(self.announce_history_deq[-1]["uploaded"])} | next announce in :{str(datetime.timedelta(seconds=self.announce_current_timer))}')
self.__decrease_timer()
clear_screen()
time.sleep(1)
def t_total_size(data):
@ -50,27 +102,6 @@ def find_approx_current(b_total_size, piece_size, percent):
current_approx = int(total / piece_size) * piece_size
return current_approx
def print_header(torrent_name, percent_complete, download_speed, upload_speed):
sys.stdout.write(f"""
###########################################################################
Torrent: {torrent_name} - {percent_complete}%
download_speed: {download_speed}KB/s
upload_speed: {upload_speed}KB/s
###########################################################################
"""
)
def print_body(d: deque, time):
for item in list(d)[:len(d)-1]:
print(f'#{item["count"]} downloaded: {item["downloaded"]} | uploaded: {item["uploaded"]} | announced')
print(f'#{d[-1]["count"]} downloaded: {d[-1]["downloaded"]} | uploaded: {d[-1]["uploaded"]} | next announce in :{str(datetime.timedelta(seconds=time))}')
def clear_screen():
if platform.system() == "Windows":
subprocess.Popen("cls", shell=True).communicate()
else:
print("\033c", end="")
def read_file(f, args_downalod, args_upload):
raw_data = f.read()
result = bencode_parser.decode(raw_data)
@ -78,20 +109,18 @@ def read_file(f, args_downalod, args_upload):
total_size = t_total_size(result)
current_downloaded = find_approx_current(total_size,piece_size,args_downalod[0])
current_uploaded = find_approx_current(total_size,piece_size,args_upload[0])
delay_announce =18000
deq = deque(maxlen=10)
count = 1
state = RatioSpoofState(result['info']['name'],args_downalod[0],args_downalod[1],args_upload[1],1800, current_downloaded, current_uploaded)
while True:
time.sleep(0.5)
""" while True:
if(current_downloaded < total_size):
current_downloaded = next_announce_total_b(args_downalod[1],current_downloaded, piece_size, delay_announce, total_size)
current_uploaded = next_announce_total_b(args_upload[1],current_uploaded, piece_size, delay_announce)
deq.append({'count': count, 'downloaded':current_downloaded,'uploaded':current_uploaded })
count +=1
clear_screen()
print_header(result['info']['name'], args_downalod[0], args_downalod[1], args_upload[1])
print_body(deq, delay_announce)
delay_announce = delay_announce - 1 if delay_announce > 0 else 0
time.sleep(1)
current_downloaded = next_announce_total_b(args_downalod[1],current_downloaded, piece_size, state.announce_rate, total_size)
current_uploaded = next_announce_total_b(args_upload[1],current_uploaded, piece_size, state.announce_rate)
state.add_announce(current_downloaded,current_uploaded)
"""