AnhP commited on
Commit
d0a85df
·
verified ·
1 Parent(s): 420a69d

Upload 2 files

Browse files
Files changed (2) hide show
  1. py/clean.py +40 -0
  2. py/sync.py +70 -0
py/clean.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import threading
3
+
4
+ from googleapiclient.discovery import build
5
+
6
+
7
+ class Clean:
8
+ def __init__(self, every=300):
9
+ self.service = build('drive', 'v3')
10
+ self.every = every
11
+ self.trash_cleanup_thread = None
12
+
13
+ def delete(self):
14
+ page_token = None
15
+
16
+ while 1:
17
+ response = self.service.files().list(q="trashed=true", spaces='drive', fields="nextPageToken, files(id, name)", pageToken=page_token).execute()
18
+
19
+ for file in response.get('files', []):
20
+ if file['name'].startswith("G_") or file['name'].startswith("D_"):
21
+ try:
22
+ self.service.files().delete(fileId=file['id']).execute()
23
+ except Exception as e:
24
+ raise RuntimeError(e)
25
+
26
+ page_token = response.get('nextPageToken', None)
27
+ if page_token is None: break
28
+
29
+ def clean(self):
30
+ while 1:
31
+ self.delete()
32
+ time.sleep(self.every)
33
+
34
+ def start(self):
35
+ self.trash_cleanup_thread = threading.Thread(target=self.clean)
36
+ self.trash_cleanup_thread.daemon = True
37
+ self.trash_cleanup_thread.start()
38
+
39
+ def stop(self):
40
+ if self.trash_cleanup_thread: self.trash_cleanup_thread.join()
py/sync.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import threading
3
+ import subprocess
4
+
5
+ class Channel:
6
+ def __init__(self, source, destination, sync_deletions=False, every=60, exclude = None):
7
+ self.source = source
8
+ self.destination = destination
9
+ self.event = threading.Event()
10
+ self.syncing_thread = threading.Thread(target=self._sync, args=())
11
+ self.sync_deletions = sync_deletions
12
+ self.every = every
13
+
14
+ if not exclude: exclude = []
15
+ if isinstance(exclude, str): exclude = [exclude]
16
+
17
+ self.exclude = exclude
18
+ self.command = ['rsync', '-aP']
19
+
20
+ def alive(self):
21
+ if self.syncing_thread.is_alive(): return True
22
+ else: return False
23
+
24
+ def _sync(self):
25
+ command = self.command
26
+
27
+ for exclusion in self.exclude:
28
+ command.append(f'--exclude={exclusion}')
29
+
30
+ command.extend([f'{self.source}/', f'{self.destination}/'])
31
+
32
+ if self.sync_deletions: command.append('--delete')
33
+
34
+ while not self.event.is_set():
35
+ subprocess.run(command)
36
+ time.sleep(self.every)
37
+
38
+ def copy(self):
39
+ command = self.command
40
+
41
+ for exclusion in self.exclude:
42
+ command.append(f'--exclude={exclusion}')
43
+
44
+ command.extend([f'{self.source}/', f'{self.destination}/'])
45
+
46
+ if self.sync_deletions: command.append('--delete')
47
+ subprocess.run(command)
48
+
49
+ return True
50
+
51
+ def start(self):
52
+ if self.syncing_thread.is_alive():
53
+ self.event.set()
54
+ self.syncing_thread.join()
55
+
56
+ if self.event.is_set(): self.event.clear()
57
+ if self.syncing_thread._started.is_set(): self.syncing_thread = threading.Thread(target=self._sync, args=())
58
+
59
+ self.syncing_thread.start()
60
+ return self.alive()
61
+
62
+ def stop(self):
63
+ if self.alive():
64
+ self.event.set()
65
+ self.syncing_thread.join()
66
+
67
+ while self.alive():
68
+ if not self.alive(): break
69
+
70
+ return not self.alive()