please help creating an update script

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
User avatar
safihre
Administrator
Administrator
Posts: 5580
Joined: April 30th, 2015, 7:35 am
Contact:

Re: please help creating an update script

Post 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?
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: please help creating an update script

Post 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
Droyellis
Newbie
Newbie
Posts: 18
Joined: March 21st, 2022, 3:36 pm

Re: please help creating an update script

Post 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
Droyellis
Newbie
Newbie
Posts: 18
Joined: March 21st, 2022, 3:36 pm

Re: please help creating an update script

Post by Droyellis »

I liked the first python code that shows the releases, how would I make python download newer releases?
Droyellis
Newbie
Newbie
Posts: 18
Joined: March 21st, 2022, 3:36 pm

Re: please help creating an update script

Post 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.
Last edited by Droyellis on November 11th, 2023, 7:16 pm, edited 1 time in total.
User avatar
safihre
Administrator
Administrator
Posts: 5580
Joined: April 30th, 2015, 7:35 am
Contact:

Re: please help creating an update script

Post 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.
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: please help creating an update script

Post by sander »

Or loop over it, and fetch the one with "win32-bin.zip" or "win64-bin.zip" in it
Droyellis
Newbie
Newbie
Posts: 18
Joined: March 21st, 2022, 3:36 pm

Re: please help creating an update script

Post by Droyellis »

I'd like to somehow match it to *win64-bin.zip

I'm not sure how to do that though
User avatar
safihre
Administrator
Administrator
Posts: 5580
Joined: April 30th, 2015, 7:35 am
Contact:

Re: please help creating an update script

Post 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 😊
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
Droyellis
Newbie
Newbie
Posts: 18
Joined: March 21st, 2022, 3:36 pm

Re: please help creating an update script

Post 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")
Post Reply