pcloud_download/main.py

72 lines
1.9 KiB
Python
Raw Normal View History

import hashlib
import logging
2020-11-18 15:29:39 +01:00
import os
import sys
import requests
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
PCLOUD_USER_EMAIL = os.getenv("PCLOUD_USER_EMAIL")
PCLOUD_USER_PASSWORD = os.getenv("PCLOUD_USER_PASSWORD")
2020-11-18 15:29:39 +01:00
PCLOUD_USER_REGION = os.getenv("PCLOUD_USER_REGION").lower()
def main():
client = PCloudClient()
logger.info(client.login())
class PCloudClient:
def __init__(self):
self.session = requests.Session()
2020-11-18 15:41:14 +01:00
self.auth = None
def login(self):
digest = self.get_digest()
logger.debug("digest %s", digest)
email_sha = self._sha1(PCLOUD_USER_EMAIL.lower())
logger.debug("email_sha: %s", email_sha)
password_digest = self._sha1(PCLOUD_USER_PASSWORD + email_sha + digest)
2020-11-18 15:41:14 +01:00
res = self._get(
2020-11-18 15:32:52 +01:00
"/userinfo",
params={
"getauth": 1,
"digest": digest,
"username": PCLOUD_USER_EMAIL,
"passworddigest": password_digest,
},
)
2020-11-18 15:41:14 +01:00
self.auth = res["auth"]
return res
def get_digest(self):
return self._get("/getdigest").get("digest")
@staticmethod
def _sha1(string):
return hashlib.sha1(string.encode("utf-8")).hexdigest()
def _get(self, path, *args, **kwargs):
2020-11-18 15:29:39 +01:00
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()
2020-11-18 15:29:39 +01:00
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"
2020-11-18 15:32:52 +01:00
raise ValueError(
"PCLOUD_USER_REGION doesn't contain a supported value. Supported values: [us, eu]."
)
2020-11-18 15:29:39 +01:00
if __name__ == "__main__":
main()