Add switch between EU and US APIs

This commit is contained in:
Gabriel Augendre 2020-11-18 15:29:39 +01:00
parent 49226de42d
commit 2edf24fef9
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

13
main.py
View file

@ -1,5 +1,6 @@
import hashlib
import logging
import os
import sys
import requests
@ -10,6 +11,7 @@ logger = logging.getLogger(__name__)
PCLOUD_USER_EMAIL = os.getenv("PCLOUD_USER_EMAIL")
PCLOUD_USER_PASSWORD = os.getenv("PCLOUD_USER_PASSWORD")
PCLOUD_USER_REGION = os.getenv("PCLOUD_USER_REGION").lower()
def main():
@ -28,7 +30,6 @@ class PCloudClient:
logger.debug("email_sha: %s", email_sha)
password_digest = self._sha1(PCLOUD_USER_PASSWORD + email_sha + digest)
return self._get("/userinfo", params={"getauth": 1, "digest": digest, "username": PCLOUD_USER_EMAIL, "passworddigest": password_digest})
# return self._get("/userinfo", params={"getauth": 1, "username": PCLOUD_USER_EMAIL, "password": PCLOUD_USER_PASSWORD})
def get_digest(self):
return self._get("/getdigest").get("digest")
@ -39,13 +40,21 @@ class PCloudClient:
return hashlib.sha1(string.encode("utf-8")).hexdigest()
def _get(self, path, *args, **kwargs):
url = "https://api.pcloud.com{}".format(path)
base_url = self._get_base_url()
url = base_url + path
logger.debug("args: %s", args)
logger.debug("kwargs: %s", kwargs)
res = self.session.get(url, *args, **kwargs)
res.raise_for_status()
return res.json()
def _get_base_url(self):
if PCLOUD_USER_REGION == "us":
return "https://api.pcloud.com"
elif PCLOUD_USER_REGION == "eu":
return "https://eapi.pcloud.com"
raise ValueError("PCLOUD_USER_REGION doesn't contain a supported value. Supported values: [us, eu].")
if __name__ == "__main__":
main()