Page 2 of 2

Re: please help creating an update script

Posted: November 10th, 2023, 3:20 pm
by safihre
You should investigate how to do this in powershell, I'm sure it's possible from what I've seen my developers at my day job do with powershell.
Otherwise you can always try to ask Chatgpt, I've seen it spit out a bit convoluted but still functional code. I'm sure you can ask it "write me a powershell script to get the version number from this string".

Or you want Python script?

Re: please help creating an update script

Posted: November 10th, 2023, 3:29 pm
by sander
Chatgpt ... cool ... let's try:

I typed:

write a python script that gets the latest version from https://api.github.com/repos/sabnzbd/sa ... ses/latest

and Chatgpt yielded:

Code: Select all

import requests

def get_latest_version():
    url = 'https://api.github.com/repos/sabnzbd/sabnzbd/releases/latest'

    try:
        response = requests.get(url)
        response.raise_for_status()  # Raise an HTTPError for bad requests
        data = response.json()

        # Extract the latest version from the response
        latest_version = data['tag_name']
        return latest_version

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

if __name__ == "__main__":
    latest_version = get_latest_version()

    if latest_version:
        print(f"The latest version of Sabnzbd is: {latest_version}")
    else:
        print("Failed to retrieve the latest version.")
which says

Code: Select all

$ python3 get-sab-version-2.py 
The latest version of sabnzbd is: 4.1.0

Cool

Re: please help creating an update script

Posted: November 10th, 2023, 3:42 pm
by Droyellis
yes i would much prefer python, id use powershell if needed tho.

that chatgpt was pretty cool. much appreciate the effort. your first code seemed pretty good at displaying latest versions, its the downloading latest/future releases is what im after

Re: please help creating an update script

Posted: November 10th, 2023, 7:20 pm
by Droyellis
I liked the first python code that shows the releases, how would I make python download newer releases?

Re: please help creating an update script

Posted: November 10th, 2023, 11:32 pm
by Droyellis
ok this is my code so far:

Code: Select all

old code removed
it is working by way of targeting the 5th asset in the list of releases, if that changes for some reason its gunna break and id have to fix it.

Re: please help creating an update script

Posted: November 11th, 2023, 2:23 am
by safihre
You could loop over release_info['assets'] to find the right value, but most likely it will stay number 5 for a long time to come.

Re: please help creating an update script

Posted: November 11th, 2023, 2:33 am
by sander
Or loop over it, and fetch the one with "win32-bin.zip" or "win64-bin.zip" in it

Re: please help creating an update script

Posted: November 11th, 2023, 4:51 am
by Droyellis
I'd like to somehow match it to *win64-bin.zip

I'm not sure how to do that though

Re: please help creating an update script

Posted: November 11th, 2023, 6:28 am
by safihre
We helped you quite a long way, you can ask Chatgpt again or maybe look through a bit of Python tutorials about for loops.
Python is really a nice language to learn programming and makes it easy to understand 😊

Re: please help creating an update script

Posted: November 11th, 2023, 5:25 pm
by Droyellis
thank you.

this code will download the latest *win46-bin.zip, ignoring version number:

Code: Select all

import os
import requests
from fnmatch import fnmatch
from zipfile import ZipFile
import glob
import shutil

os.system("taskkill /f /im SABnzbd.exe")

def download_file(url, local_path, headers=None):
    with requests.get(url, stream=True, headers=headers) as response:
        response.raise_for_status()
        with open(local_path, "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    file.write(chunk)

def extract_zip(zip_file, output_dir):
    with ZipFile(zip_file, 'r') as zip_ref:
        zip_ref.extractall(output_dir)

def download_sabnzbd_asset(version, filename_pattern, token=None, output_dir="."):
    api_url = "https://api.github.com/repos/sabnzbd/sabnzbd/releases/latest"

    headers = {"Authorization": f"Bearer {token}"} if token else {}
    response = requests.get(api_url, headers=headers)

    if response.status_code == 200:
        release_info = response.json()
        assets = release_info["assets"]

        for asset in assets:
            if fnmatch(asset["name"], filename_pattern):
                download_url = asset["browser_download_url"]
                output_path = os.path.join(output_dir, asset["name"])
                download_file(download_url, output_path, headers=headers)
                print(f"Downloaded {asset['name']} to {output_path}")

                # Extract the downloaded zip file
                extract_zip(output_path, output_dir)
                print(f"Extracted {asset['name']} to {output_dir}")

                # Delete the downloaded zip file
                os.remove(output_path)
                print(f"Deleted {asset['name']}")

def rename_extracted_folder(output_dir):
    extracted_folders = glob.glob(os.path.join(output_dir, "SABnzbd*"))
    
    for folder in extracted_folders:
        if os.path.isdir(folder):
            new_name = os.path.join(output_dir, "SABnzbd")
            os.rename(folder, new_name)
            print(f"Renamed {folder} to {new_name}")

if __name__ == "__main__":
    version = ""  # Replace with the actual SABnzbd version
    filename_pattern = "*win64-bin.zip"  # Replace with the actual filename pattern
    token = ""  # Replace with your GitHub token if the repository is private

    # Change the output directory here
    output_directory = r'E:\Server\SABnzbdData\update'
    
    download_sabnzbd_asset(version, filename_pattern, token, output_dir=output_directory)
    rename_extracted_folder(output_directory)

    # Delete the folder E:\Server\SABnzbd
    folder_to_delete = r'E:\Server\SABnzbd'
    shutil.rmtree(folder_to_delete, ignore_errors=True)
    print(f"Deleted {folder_to_delete}")

    # Move the renamed folder to E:\Server
    renamed_folder = os.path.join(output_directory, "SABnzbd")
    destination_folder = r'E:\Server'
    shutil.move(renamed_folder, destination_folder)
    print(f"Moved {renamed_folder} to {destination_folder}")

os.startfile(r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\SABnzbd.lnk")