forked from manc88/mail-cloud-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailcloudapi.py
229 lines (203 loc) · 5.98 KB
/
mailcloudapi.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import json
import requests
from urllib import parse
import mcsettings as URL
class Cloud():
"""main class with mail.ru cloud APIs"""
def __init__(self,email="",password=""):
self.__password__ = password
self.email = email
self.response = None
self.authorized = False
self.token = ""
self.cookies = {}
self.loader = ""
def __auth__(self):
values = {"Login":self.email, "Password":self.__password__}
self.response = requests.get(URL.AUTH, params = values)
if self.response.status_code == requests.codes.ok:
self.authorized = True
self.cookies = dict(self.response.cookies)
else:
self.authorized = False
def __get_token__(self):
"""update token"""
values = {"email":self.email}
self.response = requests.get(URL.TOKEN, params = values, cookies = self.cookies)
self.token = json.loads(self.response.text)["body"]["token"].strip()
if self.token:
return True
else:
return False
def login(self):
"""login cloud to start work """
self.__auth__()
self.__get_token__()
def logout(self):
self.response = requests.post(URL.LOGOUT,cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
self.authorized = False
self.cookies = {}
self.token = ""
else:
self.authorized = True
def __get_loader__(self):
"""get the load server """
self.response = requests.get(URL.DISPATCHER)
self.loader = self.response.text.split(" ")[0]
def __load_file__(self,filepath = ""):
"""load file to cloclo, and return dict with params"""
path = [i for i in filepath.split('/') if i]
filename = path.pop()
with open(filepath, 'rb') as f:
fd = {'file': f}
values = {"Content_Type":"multipart/form-data", "Content" : {"file":filename}}
self.response = requests.post(self.loader, params = values, files = fd, cookies = self.cookies)
ls = self.response.text.split(";")
if self.response.status_code == requests.codes.ok and ls:
return {"name":ls[1].strip(),
"hash":ls[0].strip(),
"size":ls[2].strip()
}
else:
return None
def __link_file__(self,file_params,cloud_path):
"""Finaly add link of loaded file into cloud """
if self.add_folder(cloud_path):
body = parse.urlencode({
"folder":cloud_path,
"files": json.dumps([{
"name":file_params["name"],
"size":file_params["size"],
"hash":file_params["hash"],
}]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.ADDFILE,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False
else:
return False
def add_folder(self,full_folder_name = ""):
"""Generate full path to file every time, even if exist """
ls = full_folder_name.split('/')
cls = [item for item in ls if item]
parpath = ""
for foldername, parent in self.__gen_parents__(cls):
parpath+="/"+parent
body = parse.urlencode({
"add": json.dumps([{
"folder":parpath,
"name": foldername,
}]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.ADDFOLDER,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False
def __gen_parents__(self, ls):
f=[]
par = "/"
for i in ls:
if par == "/":
f.append((i,par))
par = i
else:
f.append((i,par))
par = i
return f
def add_file(self,local_path,cloud_path):
"""Load file into cloud
-- local_path = path on the local machine
-- cloud_path = path you want to load in cloud , if not exists will be created
"""
self.__get_loader__()
params = self.__load_file__(local_path)
self.__link_file__(params,cloud_path)
def share(self,filename_with_path=""):
"""share the file from cloud """
body = parse.urlencode({
"ids": json.dumps([filename_with_path]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.SHARE,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return self.response.json()["body"][0]["url"]["get"]
else:
return None
def unshare(self, filename_with_path=""):
"""unshare the file """
body = parse.urlencode({
"ids": json.dumps([filename_with_path]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.UNSHARE,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False
def remove(self, full_path=""):
"""Remove files, folders all of them, use CAREFULL """
body = parse.urlencode({
"ids": json.dumps([full_path]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.REMOVE,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False
def move(self, current_path="", target_path=""):
"""Move file or folder in cloud """
body = parse.urlencode({
"folder": target_path,
"ids": json.dumps([current_path]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.MOVE,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False
def rename(self, current_name="", target_name=""):
"""Rename file or folder in cloud
-- current_name = full path + name
-- target_name = only name of file
"""
body = parse.urlencode({
"rename": json.dumps([{
"id": current_name,
"name":target_name,
}]),
"api":1,
"email": self.email,
"storage":"home",
"token": self.token,
})
self.response = requests.post(URL.RENAME,data = body, cookies = self.cookies)
if self.response.status_code == requests.codes.ok:
return True
else:
return False