summary refs log tree commit diff stats
path: root/MyPyload.py
diff options
context:
space:
mode:
authorAndronaco Marco <marco.andronaco@olivetti.com>2023-07-12 13:02:46 +0200
committerAndronaco Marco <marco.andronaco@olivetti.com>2023-07-12 13:02:46 +0200
commit585855a8728f87cc5383329bd227f6d6ba840aff (patch)
tree634aa7868a614e45eeab6772a2a5ddf367e74a91 /MyPyload.py
parent733a0a23988fb074c93e6c398d8b9142ee180b29 (diff)
downloadsunstroke-585855a8728f87cc5383329bd227f6d6ba840aff.tar.gz
working version
Diffstat (limited to 'MyPyload.py')
-rw-r--r--MyPyload.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/MyPyload.py b/MyPyload.py
new file mode 100644
index 0000000..2653758
--- /dev/null
+++ b/MyPyload.py
@@ -0,0 +1,36 @@
+# 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
+from dotenv import load_dotenv
+from os import getenv
+load_dotenv()
+
+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:
+
+    def __init__(self):
+        self.url_base = urljoin(PYLOAD_HOST, 'api/')
+        self.session = self._call('login', {'username': PYLOAD_USER, 'password': PYLOAD_PW}, False)
+
+    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
+
+        if hasattr(self, 'session'):
+            data['session'] = self.session
+
+        post = urlencode(data).encode('utf-8')
+        return json.loads(urlopen(url, post).read().decode('utf-8'))
+
+    def __getattr__(self, name):
+        def wrapper(**kargs):
+            return self._call(name, kargs)
+        return wrapper