mirror of
https://github.com/FAUSheppy/homelab_gamevault
synced 2025-12-06 06:51:36 +01:00
feat: basic background progress bars (downloads)
This commit is contained in:
@@ -5,6 +5,9 @@ import threading
|
||||
from db import db, Download
|
||||
from sqlalchemy import or_, and_
|
||||
|
||||
def _bytes_to_mb(size):
|
||||
return size / (1024*1024)
|
||||
|
||||
def add_to_download_queue(url, path):
|
||||
'''The download is added to the global queue and downloaded eventually'''
|
||||
#_download(url, path)
|
||||
@@ -38,7 +41,7 @@ def _download(url, path):
|
||||
|
||||
raise AssertionError("Non-200 Response for:", url, path, response.status_code, response.text)
|
||||
|
||||
def log_begin_download(path):
|
||||
def log_begin_download(path, local_path, url):
|
||||
|
||||
session = db.session()
|
||||
print("Current path", path)
|
||||
@@ -49,7 +52,7 @@ def log_begin_download(path):
|
||||
raise AssertionError("ERROR: {} is already downloading.".format(path))
|
||||
else:
|
||||
print("Adding to download log:", path)
|
||||
session.merge(Download(path=path, size=0, type="download", finished=False))
|
||||
session.merge(Download(path=path, size=-1, type="download", local_path=local_path, url=url, finished=False))
|
||||
session.commit()
|
||||
|
||||
db.close_session()
|
||||
@@ -57,12 +60,60 @@ def log_begin_download(path):
|
||||
def log_end_download(path):
|
||||
|
||||
session = db.session()
|
||||
path_exists = session.query(Download).filter(Download.path==path).first()
|
||||
if not path_exists:
|
||||
obj = session.query(Download).filter(Download.path==path).first()
|
||||
if not obj:
|
||||
raise AssertionError("ERROR: {} is not downloading/cannot remove.".format(path))
|
||||
else:
|
||||
print("Removing from download log:", path)
|
||||
session.merge(Download(path=path, size=0, type="download", finished=True))
|
||||
obj.finished = True
|
||||
session.merge(obj)
|
||||
session.commit()
|
||||
|
||||
db.close_session()
|
||||
db.close_session()
|
||||
|
||||
def get_download_size(path):
|
||||
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(Download.path==path).first()
|
||||
|
||||
if not obj :
|
||||
print("Warning: Download-Object does no longe exist in DB. Returning -1")
|
||||
return -1
|
||||
elif obj.size != -1:
|
||||
session.close()
|
||||
return obj.size
|
||||
|
||||
# query size #
|
||||
r = requests.get(obj.url, params={"path": path, "info": 1})
|
||||
r.raise_for_status()
|
||||
|
||||
size = r.json()["size"]
|
||||
obj.size = _bytes_to_mb(size)
|
||||
session.merge(obj)
|
||||
session.commit()
|
||||
session.close()
|
||||
|
||||
return size
|
||||
|
||||
def get_percent_filled(path):
|
||||
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(Download.path==path, Download.finished==False).first()
|
||||
size = _bytes_to_mb(os.stat(obj.local_path).st_size)
|
||||
total_size = get_download_size(obj.path)
|
||||
session.close()
|
||||
if total_size == 0:
|
||||
return 0
|
||||
return size / total_size * 100
|
||||
|
||||
def get_download(path=None):
|
||||
|
||||
session = db.session()
|
||||
if path:
|
||||
MIN_SIZE_PGBAR_LIMIT = 1024*1024*100 # 100mb
|
||||
downloads = session.query(Download).filter(Download.size>MIN_SIZE_PGBAR_LIMIT, Download.finished==False).all()
|
||||
else:
|
||||
downloads = session.query(Download).filter(Download.finished==False).all()
|
||||
|
||||
session.close()
|
||||
return downloads
|
||||
Reference in New Issue
Block a user