Last active
February 21, 2024 11:29
-
-
Save dadatuputi/883613b2550601076b5e3b1012c505fd to your computer and use it in GitHub Desktop.
Script to automatically upgrade Emby on Debian or Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from bs4 import BeautifulSoup | |
import requests | |
import json | |
import subprocess | |
import sys | |
import certifi | |
def log_error(e): | |
""" | |
It is always a good idea to log errors. | |
This function just prints them, but you can | |
make it do anything. | |
""" | |
print(e) | |
def cmp_version(local, remote): | |
"""Compare two version strings for sorting""" | |
local = local.split('.') | |
remote = remote.split('.') | |
for i,_ in enumerate(local): | |
octet1 = int(local[i]) | |
octet2 = int(remote[i]) | |
if octet1 < octet2: | |
return 1 | |
elif octet1 > octet2: | |
return -1 | |
else: | |
continue | |
return 0 | |
def download(url, dest="/tmp/emby.deb"): | |
print(f"Downloading {url}") | |
req_file = requests.get(url, stream=True) | |
with open(dest, 'wb') as fd: | |
for chunk in req_file.iter_content(chunk_size=128): | |
fd.write(chunk) | |
return dest | |
def get_local_version(): | |
# Get version of emby on system | |
try: | |
cmd = 'dpkg-query -s emby-server | grep Version | awk -F":" \'{print $2; exit}\'' | |
cp = subprocess.run(['/bin/bash', '-o', 'pipefail', '-c', cmd], check=True, capture_output=True) | |
version = cp.stdout.strip() | |
except subprocess.CalledProcessError as e: | |
# emby not installed | |
return 0 | |
def get_remote_json(): | |
# Get latest release json | |
try: | |
req_json = requests.get('https://api.github.com/repos/MediaBrowser/Emby.Releases/releases/latest') | |
data = json.loads(req_json.text) | |
if req_json.status_code != 200 or not data: | |
raise Exception("couldn't get valid json response from github") | |
for asset in data['assets']: | |
release_name = asset['name'] | |
if release_name.startswith('emby-server-deb_'): | |
if release_name.endswith('_amd64.deb'): | |
release_name = release_name.replace('emby-server-deb_', '') | |
version = release_name.replace('_amd64.deb', '') | |
return (version, asset) | |
raise Exception(f"no suitable release candidate found: {data}") | |
except Exception as e: | |
log_error(e) | |
sys.exit(1) | |
def install(deb): | |
print(subprocess.getoutput(f'dpkg -i {deb}')) | |
if __name__ == "__main__": | |
# Get installed version | |
local_version = get_local_version() | |
remote_version, remote_json = get_remote_json() | |
if not local_version: | |
print(f"Emby not installed, installing {remote_version} from github") | |
# Install Emby | |
install(download(remote_json['browser_download_url'])) | |
else: | |
print(f"Installed Emby Version: {local_version}") | |
if cmp_version(local_version, remote_version): | |
print(f"Remote version {remote_version} is newer, installing") | |
# Install Emby | |
install(download(remote_json['browser_download_url'])) | |
else: | |
print("Latest version already installed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment