-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanup-DownloadStation.py
More file actions
76 lines (68 loc) · 3.15 KB
/
Cleanup-DownloadStation.py
File metadata and controls
76 lines (68 loc) · 3.15 KB
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
import requests
import time
import json
class SynologyDownloadStation:
def __init__(self, nas_ip, user_id, password):
self.nas_ip = nas_ip
self.user_id = user_id
self.password = password
self.session_id = None
self.two_weeks_ago = time.time() - 2 * 7 * 24 * 60 * 60 # Two weeks in seconds
def log_into_download_station(self):
print("Logging into Download Station")
url = f"http://{self.nas_ip}/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account={self.user_id}&passwd={self.password}&session=DownloadStation&format=cookie"
response = requests.get(url)
data = response.json()
if data.get('success'):
self.session_id = data['data']['sid']
else:
raise Exception("Failed to log in")
def log_out_of_download_station(self):
if self.session_id:
print("Logging out")
url = f"http://{self.nas_ip}/webapi/auth.cgi?api=SYNO.API.Auth&version=1&method=logout&session=DownloadStation"
requests.get(url, cookies={'id': self.session_id})
self.session_id = None
def get_download_tasks(self):
print("Getting download tasks")
url = f"http://{self.nas_ip}/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=1&method=list&additional=detail,file"
response = requests.get(url, cookies={'id': self.session_id})
data = response.json()
if data.get('success'):
return data['data']['tasks']
else:
return []
def delete_download_task(self, task_id):
url = f"http://{self.nas_ip}/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=1&method=delete&id={task_id}"
response = requests.get(url, cookies={'id': self.session_id})
data = response.json()
return data.get('success', False)
def clean_up_download_tasks(self):
tasks = self.get_download_tasks()
tasks_to_keep = []
for task in tasks:
create_time = task['additional']['detail'].get('create_time')
transfer = task['additional'].get('transfer')
if transfer is not None:
downloaded = transfer.get('size_downloaded') or 0
else:
downloaded = 0
print(f"Created: {create_time}. \"{task['title']}\", Status: {task['status']}. Downloaded: {downloaded}")
if task['status'] == 'error' or (create_time is not None and create_time < self.two_weeks_ago and downloaded == 0):
if self.delete_download_task(task['id']):
print(f"Deleted task ID: {task['id']} for \"{task['title']}\"")
else:
raise Exception(f"Failed to delete task ID: {task['id']}")
else:
tasks_to_keep.append(task)
return tasks_to_keep
if __name__ == "__main__":
nas_ip = "127.0.0.1:5916"
user_id = "Download_Station"
password = "tCGbA90WB!S9"
ds = SynologyDownloadStation(nas_ip, user_id, password)
try:
ds.log_into_download_station()
ds.clean_up_download_tasks()
finally:
ds.log_out_of_download_station()