Add switch between EU and US APIs
This commit is contained in:
parent
49226de42d
commit
2edf24fef9
1 changed files with 11 additions and 2 deletions
13
main.py
13
main.py
|
@ -1,5 +1,6 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -10,6 +11,7 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
PCLOUD_USER_EMAIL = os.getenv("PCLOUD_USER_EMAIL")
|
PCLOUD_USER_EMAIL = os.getenv("PCLOUD_USER_EMAIL")
|
||||||
PCLOUD_USER_PASSWORD = os.getenv("PCLOUD_USER_PASSWORD")
|
PCLOUD_USER_PASSWORD = os.getenv("PCLOUD_USER_PASSWORD")
|
||||||
|
PCLOUD_USER_REGION = os.getenv("PCLOUD_USER_REGION").lower()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -28,7 +30,6 @@ class PCloudClient:
|
||||||
logger.debug("email_sha: %s", email_sha)
|
logger.debug("email_sha: %s", email_sha)
|
||||||
password_digest = self._sha1(PCLOUD_USER_PASSWORD + email_sha + digest)
|
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, "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):
|
def get_digest(self):
|
||||||
return self._get("/getdigest").get("digest")
|
return self._get("/getdigest").get("digest")
|
||||||
|
@ -39,13 +40,21 @@ class PCloudClient:
|
||||||
return hashlib.sha1(string.encode("utf-8")).hexdigest()
|
return hashlib.sha1(string.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
def _get(self, path, *args, **kwargs):
|
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("args: %s", args)
|
||||||
logger.debug("kwargs: %s", kwargs)
|
logger.debug("kwargs: %s", kwargs)
|
||||||
res = self.session.get(url, *args, **kwargs)
|
res = self.session.get(url, *args, **kwargs)
|
||||||
res.raise_for_status()
|
res.raise_for_status()
|
||||||
return res.json()
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue