mirror of
https://github.com/KRTirtho/spotube.git
synced 2026-05-06 15:24:36 +00:00
Enhance update_flathub_version.py with download progress
Refactor script to download and update Flathub version with progress indication and backup functionality.
This commit is contained in:
parent
2cc8d2620d
commit
70a27226d6
@ -4,29 +4,60 @@ import hashlib
|
||||
import sys
|
||||
import requests
|
||||
import yaml
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
REPO = "KRTirtho/spotube"
|
||||
YAML_FILENAME = "com.github.KRTirtho.Spotube.yml"
|
||||
|
||||
config = None
|
||||
with open(YAML_FILENAME, mode="r", encoding="utf-8") as input:
|
||||
config = yaml.safe_load(input)
|
||||
|
||||
# Requires the 2nd VERSION argument to be passed
|
||||
version = sys.argv[1:][0]
|
||||
def download_and_update(version):
|
||||
with open(YAML_FILENAME, mode="r", encoding="utf-8") as input_file:
|
||||
config = yaml.safe_load(input_file)
|
||||
|
||||
tar_url = f"https://github.com/{REPO}/releases/download/v{version}/spotube-linux-{version}-x86_64.tar.xz"
|
||||
tar_sha256 = hashlib.sha256()
|
||||
|
||||
tar_url = f"https://github.com/{REPO}/releases/download/v{version}/spotube-linux-{version}-x86_64.tar.xz"
|
||||
tar_sha256 = hashlib.sha256()
|
||||
print(f"Downloading file {tar_url} to generete sha256 sum")
|
||||
tar = requests.get(tar_url)
|
||||
for chunk in tar.iter_content():
|
||||
print(f"Downloading: {tar_url}")
|
||||
|
||||
try:
|
||||
response = requests.get(tar_url, stream=True, timeout=10)
|
||||
response.raise_for_status()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Download failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
total_size = int(response.headers.get("content-length", 0))
|
||||
downloaded = 0
|
||||
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
tar_sha256.update(chunk)
|
||||
downloaded += len(chunk)
|
||||
if total_size:
|
||||
percent = (downloaded / total_size) * 100
|
||||
print(f"\rProgress: {percent:.2f}%", end="")
|
||||
|
||||
tar_source = config["modules"][-1]["sources"][0]
|
||||
tar_source["url"] = tar_url
|
||||
tar_source["sha256"] = tar_sha256.hexdigest()
|
||||
print("\nDownload complete.")
|
||||
|
||||
with open(YAML_FILENAME, mode="w", encoding="utf-8") as output:
|
||||
yaml.safe_dump(config, output, sort_keys=False)
|
||||
tar_source = config["modules"][-1]["sources"][0]
|
||||
tar_source["url"] = tar_url
|
||||
tar_source["sha256"] = tar_sha256.hexdigest()
|
||||
|
||||
backup_name = f"{YAML_FILENAME}.{datetime.now().strftime('%Y%m%d_%H%M%S')}.bak"
|
||||
os.rename(YAML_FILENAME, backup_name)
|
||||
print(f"Backup created: {backup_name}")
|
||||
|
||||
with open(YAML_FILENAME, mode="w", encoding="utf-8") as output_file:
|
||||
yaml.safe_dump(config, output_file, sort_keys=False)
|
||||
|
||||
print("YAML file updated successfully.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python script.py <version>")
|
||||
sys.exit(1)
|
||||
|
||||
version_arg = sys.argv[1]
|
||||
download_and_update(version_arg)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user