update to pyload-ng
Andronaco Marco marco.andronaco@olivetti.com
Thu, 13 Jul 2023 15:05:43 +0200
2 files changed,
18 insertions(+),
23 deletions(-)
M
MyPyload.py
→
MyPyload.py
@@ -1,7 +1,7 @@
# My edited version of https://github.com/thammi/pyload-utils/blob/master/pyloadutils/pyload.py import json -from urllib.request import urlopen -from urllib.parse import urljoin, urlencode +import requests +from urllib.parse import urljoin, urlencode, quote_plus from dotenv import load_dotenv from os import getenv load_dotenv()@@ -10,27 +10,23 @@ PYLOAD_HOST = getenv("PYLOAD_HOST") or "http://localhost:8000/"
PYLOAD_USER = getenv("PYLOAD_USER") or "pyload" PYLOAD_PW = getenv("PYLOAD_PW") or "pyload" -class Pyload: +PYLOAD_API_URL = PYLOAD_HOST + 'api/' +PYLOAD_LOGIN_URL = urljoin(PYLOAD_API_URL, 'login') +PYLOAD_ADDPACKAGE_URL = urljoin(PYLOAD_API_URL, 'add_package') - def __init__(self): - self.url_base = urljoin(PYLOAD_HOST, 'api/') - self.session = self._call('login', {'username': PYLOAD_USER, 'password': PYLOAD_PW}, False) +QUOTES = '"{}"' - def _call(self, name, args={}, encode=True): - url = urljoin(self.url_base, name) - - if encode: - data = { k: json.dumps(v) for k, v in args.items() } - else: - data = args +class Pyload: - if hasattr(self, 'session'): - data['session'] = self.session + def __init__(self): + login_data = {'username': PYLOAD_USER, 'password': PYLOAD_PW} + self.session = requests.Session() + self.session.post(PYLOAD_LOGIN_URL, data=login_data) - post = urlencode(data).encode('utf-8') - return json.loads(urlopen(url, post).read().decode('utf-8')) + def addPackage(self, name: str, links: list, password: str = "", ): + #link_string = [quote_plus(x) for x in links] + name_string = QUOTES.format(name) + link_string = json.dumps(links) - def __getattr__(self, name): - def wrapper(**kargs): - return self._call(name, kargs) - return wrapper + request_data = { 'name': name_string, 'links': link_string } + return self.session.post(url=PYLOAD_ADDPACKAGE_URL, data=request_data)