-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynologyFileStaionApiFunctions.py
413 lines (372 loc) · 14.8 KB
/
synologyFileStaionApiFunctions.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/python
from datetime import datetime
import json
import requests
import urllib2
import urllib3, certifi
#Specify variables
strUsername=''
strPassword=''
strFileServer='https://xxx.xxx.xxx:5001'
strShare='IBCollection'
#Initialize the urllib3 module for requests without getting SSL warings
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
################################################################################
### Calculate current school year
def currentSchoolYear ():
intYear=datetime.now().year
intMonth=datetime.now().month
if intMonth < 7:
schoolYear=str (intYear - 1) + '-' + str (intYear)
else:
schoolYear=str (intYear) + '-' + str (intYear + 1)
return schoolYear
################################################################################
### Get current date
def currentDate ():
return datetime.today().strftime('%Y-%m-%d')
################################################################################
### get info of auth module using urllib2 module
def synologyInfoUrllib2 ():
strApiUrl = strFileServer + '/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.FileStation'
data = urllib2.urlopen(strApiUrl).read()
print data
### get info of auth module using requests module
def synologyInfoRequests ():
payload = {
'api': 'SYNO.API.Info',
'version': '1',
'method': 'query',
'query': 'SYNO.API.Auth,SYNO.FileStation'
}
strApiUrl = strFileServer + '/webapi/query.cgi'
data = requests.get(strApiUrl, payload).content
print data
### get info of auth module using urllib3 module
def synologyInfoUrllib3 ():
strApiUrl = strFileServer + '/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.FileStation'
r = http.request('GET', strApiUrl)
jsonData = json.loads(r.data)
print jsonData
################################################################################
### log in to synology filestaitiom and get session sid using urllib2 module
def synologyLogin ():
strApiUrl = strFileServer + '/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=' + strUsername + '&passwd=' + strPassword + '&session=FileStation'
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
strSID = jsonData["data"]["sid"]
print "Your session id is: " + strSID
return strSID
### log in to synology filestaitiom and get session sid using requests module
def synologyLoginRequests ():
payload = {
'api': 'SYNO.API.Auth',
'version': '3',
'method': 'login',
'account': strUsername,
'passwd': strPassword,
'session': 'FileStation'
}
strApiUrl = strFileServer + '/webapi/auth.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
strSID = jsonData["data"]["sid"]
print "Your session id is: " + strSID
return strSID
### log in to synology filestaitiom and get session sid using urllib3 module
def synologyLoginUrllib3 ():
strApiUrl = strFileServer + '/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=' + strUsername + '&passwd=' + strPassword + '&session=FileStation'
jsonData = json.loads(http.request('GET', strApiUrl).data)
strSID = jsonData["data"]["sid"]
print "Your session id is: " + strSID
return strSID
################################################################################
### log out of synology filestaitiom using urllib2 module
def synologyLogoutUrllib2 (strSID):
strApiUrl = strFileServer + '/webapi/auth.cgi?api=SYNO.API.Auth&version=1&method=logout&session=FileStation&_sid=' + strSID
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
strLogout = str (jsonData["success"])
print "Logged out of Filestation successful: " + strLogout
### log out of synology filestaitiom using requests module
def synologyLogoutRequests (strSID):
payload = {
'api': 'SYNO.API.Auth',
'version': '1',
'method': 'logout',
'session': 'FileStation',
'_sid': strSID
}
strApiUrl = strFileServer + '/webapi/auth.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
strLogout = str (jsonData["success"])
print "Logged out of Filestation successful: " + strLogout
### log out of synology filestaitiom using urllib3 module
def synologyLogoutUrllib3 (strSID):
payload = {
'api': 'SYNO.API.Auth',
'version': '1',
'method': 'logout',
'session': 'FileStation',
'_sid': strSID
}
strApiUrl = strFileServer + '/webapi/auth.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
strLogout = str (jsonData["success"])
print "Logged out of Filestation successful: " + strLogout
################################################################################
### list FilseStation share using urllib2 module
def synologyListSharesUrllib2 (strSID):
strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.List&version=2&method=list_share&_sid=' + strSID
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
for share in jsonData["data"]["shares"]:
print share["name"]
### list FilseStation share using requests module
def synologyListSharesRequests (strSID):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list_share',
'_sid': strSID
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
for share in jsonData["data"]["shares"]:
print share["name"]
### list FilseStation share using urllib3 module
def synologyListSharesUrllib3 (strSID):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list_share',
'_sid': strSID
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
for share in jsonData["data"]["shares"]:
print share["name"]
################################################################################
## list FilseStation folder in folder path using urllib2 module
def synologyListFoldersUrllib2 (strSID, strPath):
strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.List&version=2&method=list&_sid=' + strSID + '&folder_path=/' + strPath
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
for folders in jsonData["data"]["files"]:
if folders["isdir"] == True:
print folders["path"]
## list FilseStation folder in folder path using requests module
def synologyListFoldersRequests (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list',
'_sid': strSID,
'folder_path': '/'+ strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
for folders in jsonData["data"]["files"]:
if folders["isdir"] == True:
print folders["path"]
## list FilseStation folder in folder path using urllib3 module
def synologyListFoldersUrllib3 (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list',
'_sid': strSID,
'folder_path': '/'+ strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
for folders in jsonData["data"]["files"]:
if folders["isdir"] == True:
print folders["path"]
################################################################################
## list FilseStation files in folder path using urllib2 module
def synologyListFilesUrllib2 (strSID, strPath):
strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.List&version=2&method=list&_sid=' + strSID + '&folder_path=/' + strPath
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
intCount = 0
for files in jsonData["data"]["files"]:
if files["isdir"] == False:
intCount+=1
print files["name"]
print 'Files in Folder: ' + str (intCount)
## list FilseStation files in folder path using requests module
def synologyListFilesRequests (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list',
'_sid': strSID,
'folder_path': '/'+ strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
intCount = 0
for files in jsonData["data"]["files"]:
if files["isdir"] == False:
intCount+=1
print files["name"]
print 'Files in Folder: ' + str (intCount)
## list FilseStation files in folder path using urllib3 module
def synologyListFilesUrllib3 (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.List',
'version': '2',
'method': 'list',
'_sid': strSID,
'folder_path': '/'+ strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
intCount = 0
for files in jsonData["data"]["files"]:
if files["isdir"] == False:
intCount+=1
print files["name"]
print 'Files in Folder: ' + str (intCount)
################################################################################
#create folder on Filestation share
def synologyCreateFolderUrllib2 (strSID, strPath, strName):
strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.CreateFolder&version=2&method=create&_sid=' + strSID + '&folder_path=/'+ strPath + '&name=' + strName
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
strSucess = str (jsonData["success"])
print 'Creation of folder /' + strPath + '/' + strName + ' successful: ' + strSucess
#create folder on Filestation share using requests module
def synologyCreateFolderRequests (strSID, strPath, strName):
payload = {
'api': 'SYNO.FileStation.CreateFolder',
'version': '2',
'method': 'create',
'_sid': strSID,
'folder_path': '/' + strPath,
'name': strName
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
strSucess = str (jsonData["success"])
print 'Creation of folder /' + strPath + '/' + strName + ' successful: ' + strSucess
#create folder on Filestation share using urllib3 module
def synologyCreateFolderUrllib3 (strSID, strPath, strName):
payload = {
'api': 'SYNO.FileStation.CreateFolder',
'version': '2',
'method': 'create',
'_sid': strSID,
'folder_path': '/' + strPath,
'name': strName
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
strSucess = str (jsonData["success"])
print 'Creation of folder /' + strPath + '/' + strName + ' successful: ' + strSucess
################################################################################
#delete Foler on Filestation share
def synologyDeleteFolderUrllib2 (strSID, strPath):
strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.Delete&version=2&method=start&_sid=' + strSID + '&path=/' + strPath
jsonData = json.loads(urllib2.urlopen(strApiUrl).read())
strSucess = str (jsonData["success"])
print 'Deletion of folder ' + strPath + ' successful: ' + strSucess
#delete Foler on Filestation share using requests module
def synologyDeleteFolderRequests (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.Delete',
'version': '2',
'method': 'start',
'_sid': strSID,
'path': '/' + strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(requests.get(strApiUrl, payload).content)
strSucess = str (jsonData["success"])
print 'Deletion of folder ' + strPath + ' successful: ' + strSucess
#delete Foler on Filestation share using requests module
def synologyDeleteFolderUrllib3 (strSID, strPath):
payload = {
'api': 'SYNO.FileStation.Delete',
'version': '2',
'method': 'start',
'_sid': strSID,
'path': '/' + strPath
}
strApiUrl = strFileServer + '/webapi/entry.cgi'
jsonData = json.loads(http.request('GET', strApiUrl, payload).data)
strSucess = str (jsonData["success"])
print 'Deletion of folder ' + strPath + ' successful: ' + strSucess
################################################################################
### upload file to Filestation
#def synologyUploadFile (strFileName, strFilePath, strNetworkPath, boolOverwrite, boolCreatePrent):
# strApiUrl = strFileServer + '/webapi/entry.cgi?api=SYNO.FileStation.Upload&version=2&method=upload&_sid=' + strSID
# print strFileName, strFilePath, strNetworkPath, boolOverwrite, boolCreatPrent
### Currently not working
def synologyUploadFile(strDestPath, strFilePath, sid, create_parents=True, overwrite=True):
api_name = 'SYNO.FileStation.Upload'
filename = os.path.basename(strFilePath)
session = requests.session()
with open(strFilePath, 'rb') as payload:
url = strFileServer + '/webapi/entry.cgi' + '?api=' + api_name + '&version=2&method=upload&_sid=' + sid
args = {
'path': strDestPath,
'create_parents': create_parents,
'overwrite': overwrite,
}
files = {'file': (filename, payload, 'application/octet-stream')}
print url
print args
r = session.post(url, data=args, files=files)
print "Uploading file: " + strFilePath + ' to ' + strDestPath
if r.status_code is 200 and r.json()['success']:
return 'Upload Complete'
else:
print r.status_code, r.json()
return r.status_code, r.json()
# POST /webapi/entry.cgi
# Content-Length:20326728
# Content-type: multipart/form-data, boundary=AaB03x
# --AaB03x
# content-disposition: form-data; name="api"
# SYNO.FileStation.Upload
# --AaB03x
# content-disposition: form-data; name="version"
# 2
# --AaB03x
# content-disposition: form-data; name="method"
# upload
# --AaB03x
# content-disposition: form-data; name="path"
# /upload/test
# --AaB03x
# content-disposition: form-data; name="create_parents"
# true
# --AaB03x
# content-disposition: form-data; name="file"; filename="file1.txt"
# Content-Type: application/octet-stream
# ... contents of file1.txt ...
# --AaB03x--
########################################
schoolYear = currentSchoolYear ()
strDate = currentDate ()
#synologyInfoUrllib2 ()
#synologyInfoRequests()
#synologyInfoUrllib3 ()
#strSID = synologyLoginUrllib2 ()
#strSID = synologyLoginRequests()
strSID = synologyLoginUrllib3 ()
#synologyListSharesUrllib2 (strSID)
#synologyListSharesRequests(strSID)
#synologyListSharesUrllib3 (strSID)
#synologyListFoldersUrllib2 (strSID, strShare + '/' + schoolYear)
#synologyListFoldersRequests (strSID, strShare + '/' + schoolYear)
#synologyListFoldersUrllib3 (strSID, strShare + '/' + schoolYear)
#synologyListFilesUrllib2 (strSID, strShare + '/' + schoolYear + '/' + strDate)
#synologyListFilesUrllib2 (strSID, strShare + '/' + schoolYear + '/2019-01-27')
#synologyListFilesRequests (strSID, strShare + '/' + schoolYear + '/2019-01-27')
#synologyListFilesUrllib3 (strSID, strShare + '/' + schoolYear + '/2019-01-27')
#synologyCreateFolderUrllib2 (strSID, strShare, 'Test')
#synologyCreateFolderRequests (strSID, strShare, 'Test')
#synologyCreateFolderUrllib3 (strSID, strShare, 'Test')
#synologyDeleteFolderUrllib2 (strSID, strShare + '/Test')
#synologyDeleteFolderRequests (strSID, strShare + '/Test')
#synologyDeleteFolderUrllib3 (strSID, strShare + '/Test')
#synologyUploadFile (strShare + '/' + schoolYear + '/' + strDate, '~/Desktop/test_file.txt', strSID, True, True)
#synologyLogoutUrllib2 (strSID)
#synologyLogoutRequests(strSID)
synologyLogoutUrllib3 (strSID)