-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcloud_api.py
78 lines (67 loc) · 2.31 KB
/
pcloud_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
""" API that provides access to pCloud storage """
import json
import requests
ENDPOINT_API = 'https://api.pcloud.com/'
class AuthenticationError(Exception):
""" Authentication Error """
pass
class pCloudApi:
""" pCloud API """
def __init__(self, username, password):
self.username = username
self.password = password
self.auth = None
self.session = requests.Session()
def login(self, method='GET'):
""" Login to pCloud """
method_api = 'userinfo'
params = dict(
getauth=1,
logout=1,
username=self.username,
password=self.password
)
request = self._prepare_request(method_api, params, method)
response = self._send_request(request)
if response['result']:
raise AuthenticationError('Cannot authenticate')
else:
self.auth = response['auth']
print('Success: You have logged in.')
def logout(self, method='GET'):
""" Logout from the pCloud """
method_api = 'logout'
params = dict(
auth=self.auth
)
request = self._prepare_request(method_api, params, method)
response = self._send_request(request)
if response['auth_deleted']:
print('Success: You have logged out.')
else:
print('Error: Token was not invalidated.')
def create_folder(self, path, method='GET'):
""" Create folder """
method_api = 'createfolder'
params = dict(
auth=self.auth,
path=path
)
request = self._prepare_request(method_api, params, method)
response = self._send_request(request)
if response['result']:
print('Error: %s' % response['error'])
else:
print('Success: %s was created.' % path)
def _prepare_request(self, method_api, params, method):
""" Prepare request to send """
url = ENDPOINT_API + method_api
prepped = requests.Request(method, url, params=params).prepare()
return prepped
def _send_request(self, request):
""" Send request """
data = {'result': 1}
response = self.session.send(request)
if response.status_code == 200:
data = json.loads(response.text)
return data