python Plex Pass Updatescript

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Plex Pass Updatescript相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3

# Updates Plex Media Server with Plex Pass
# Licensed under the MIT license - http://r15ch13.mit-license.org/

from bs4 import BeautifulSoup
import requests
import sys
import subprocess

USERNAME = ''
PASSWORD = ''
SYSTEM = '64' # 32 or 64
DOWNLOAD_DIR = '/tmp'


LOGIN_URL = 'https://plex.tv/users/sign_in'
PLEXPASS_URL = 'https://plex.tv/downloads?channel=plexpass'

def main():
    # Start a session so we can have persistant cookies
    session = requests.Session()
    token = fetchToken(session)
    url = ''

    print("Logging in as: %s" % USERNAME)
    if login(session, token):
        url = fetchDownloadUrl(session)
        newVersion = getVersionFromUrl(url)
        print("Online Version : %s" % newVersion.strip())
        currentVersion = getCurrentVersion()
        print("Current Version: %s" % currentVersion)

        if(currentVersion == newVersion):
            print("\nVersion %s of plexmediaserver already installed, skipping\n" % newVersion)
        else:
            updatePlex(url)

def updatePlex(url):
    filename = "%s/plexmediaserver_update.deb" % DOWNLOAD_DIR
    print("Downloading update...")
    wget = subprocess.check_output("wget -nv -O \"%s\" \"%s\"" % (filename, url), shell=True).decode("utf-8")
    print("Installing update...")
    dpkg = subprocess.check_output("dpkg -E -i \"%s\"" % filename, shell=True).decode("utf-8")
    rm = subprocess.check_output("rm \"%s\"" % filename, shell=True).decode("utf-8")
    if(wget != ""):
        print("wget: %s" % wget)
    if(dpkg != ""):
        print("dpkg: %s" % dpkg)

def getCurrentVersion():
    return subprocess.check_output("dpkg-query -s plexmediaserver 2>/dev/null | grep -Po 'Version: \K.*'", shell=True).strip().decode("utf-8")

def getVersionFromUrl(url):
    split = url.split("/")
    if(len(split) == 6):
        return split[4]
    return ""

def fetchDownloadUrl(session):
    # Try accessing a page that requires you to be logged in
    r = session.get(PLEXPASS_URL)
    if r.status_code == requests.codes.ok:
        soup = BeautifulSoup(r.text)

        plexpassButton = soup.find(class_='tog-left')
        if(len(plexpassButton) == 1):
            a = soup.find("a", attrs={"data-event-label": "Ubuntu%s" % SYSTEM})
            return a['href']
        else:
            print("Plex Pass Download not found. Check login credentials (or maybe Website Layout has changed)!")

    return False

def login(session, token):
    # This is the form data that the page sends when logging in
    loginData = {
        'utf8': '✓',
        'authenticity_token': token,
        'user[login]': USERNAME,
        'user[password]': PASSWORD,
        'user[remember_me]': '1',
        'commit': 'Sign in'
    }

    # Authenticate
    r = session.post(LOGIN_URL, data=loginData)

    return (r.status_code == requests.codes.ok)

def fetchToken(session):
    r = requests.get(LOGIN_URL)

    if r.status_code == requests.codes.ok:
        soup = BeautifulSoup(r.text)
        field = soup.find("input", attrs={"name": "authenticity_token"})
        return field['value']

if __name__ == '__main__':
    main()

以上是关于python Plex Pass Updatescript的主要内容,如果未能解决你的问题,请参考以下文章

python 这是我试图让Alexa返回Plex的On Deck和Recent Downloaded列表。它不是最漂亮的,但Plex的API不是最佳选择

如何在Ubuntu 20.04 LTS Focal Fossa上安装Plex Media Server

sh [Plex CLI]一个简单的bash脚本来控制您的Plex客户端#plex #cli #shell

Plex

将 Plex 与 Chromecast 与 RasPlex 结合使用?

sh Patch Plex