all repos — sunstroke @ dbe85ea6d39ca7b2037ecf8c439b7d5d8f97e939

MyPyload.py (view raw)

 1# My edited version of https://github.com/thammi/pyload-utils/blob/master/pyloadutils/pyload.py
 2import json
 3from urllib.request import urlopen
 4from urllib.parse import urljoin, urlencode
 5from dotenv import load_dotenv
 6from os import getenv
 7load_dotenv()
 8
 9PYLOAD_HOST = getenv("PYLOAD_HOST") or "http://localhost:8000/"
10PYLOAD_USER = getenv("PYLOAD_USER") or "pyload"
11PYLOAD_PW = getenv("PYLOAD_PW") or "pyload"
12
13class Pyload:
14
15    def __init__(self):
16        self.url_base = urljoin(PYLOAD_HOST, 'api/')
17        self.session = self._call('login', {'username': PYLOAD_USER, 'password': PYLOAD_PW}, False)
18
19    def _call(self, name, args={}, encode=True):
20        url = urljoin(self.url_base, name)
21
22        if encode:
23            data = { k: json.dumps(v) for k, v in args.items() }
24        else:
25            data = args
26
27        if hasattr(self, 'session'):
28            data['session'] = self.session
29
30        post = urlencode(data).encode('utf-8')
31        return json.loads(urlopen(url, post).read().decode('utf-8'))
32
33    def __getattr__(self, name):
34        def wrapper(**kargs):
35            return self._call(name, kargs)
36        return wrapper