summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--MyPyload.py38
-rw-r--r--main.py3
2 files changed, 18 insertions, 23 deletions
diff --git a/MyPyload.py b/MyPyload.py
index 2653758..3015ac6 100644
--- a/MyPyload.py
+++ b/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:
-
-    def __init__(self):
-        self.url_base = urljoin(PYLOAD_HOST, 'api/')
-        self.session = self._call('login', {'username': PYLOAD_USER, 'password': PYLOAD_PW}, False)
+PYLOAD_API_URL = PYLOAD_HOST + 'api/'
+PYLOAD_LOGIN_URL = urljoin(PYLOAD_API_URL, 'login')
+PYLOAD_ADDPACKAGE_URL = urljoin(PYLOAD_API_URL, 'add_package')
 
-    def _call(self, name, args={}, encode=True):
-        url = urljoin(self.url_base, name)
+QUOTES = '"{}"'
 
-        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)
diff --git a/main.py b/main.py
index 3e060ec..a7b8f9d 100644
--- a/main.py
+++ b/main.py
@@ -28,8 +28,7 @@ def filter_links(links, hosts):
         if get_host(link) == host:
             return link
     return filter_links(links, hosts)
-        
-        
+
 def get_sorted_links(dictionary):
     hosts = scroll_list(HOST_PREFERENCE)
     return [ filter_links(links, hosts) for _, links in dictionary.items() ]