forked from MerginMaps/media-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_sync_daemon.py
101 lines (81 loc) · 2.52 KB
/
media_sync_daemon.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
"""
Mergin Media Sync - a tool to sync media files from Mergin projects to other storage backends
Copyright (C) 2021 Lutra Consulting
License: MIT
"""
import argparse
import sys
import datetime
import os
import time
from drivers import DriverError, create_driver
from media_sync import (
create_mergin_client,
mc_download,
media_sync_push,
mc_pull,
MediaSyncError,
)
from config import config, validate_config, ConfigError, update_config_path
from version import __version__
def main():
parser = argparse.ArgumentParser(
prog="media_sync_daemon.py",
description="Synchronization tool for media files in Mergin Maps project to other backends.",
epilog="www.merginmaps.com",
)
parser.add_argument(
"config_file",
nargs="?",
default="config.yaml",
help="Path to file with configuration. Default value is config.yaml in current working directory.",
)
args = parser.parse_args()
print(f"== Starting Mergin Media Sync daemon version {__version__} ==")
try:
update_config_path(args.config_file)
except IOError as e:
print("Error:" + str(e))
sys.exit(1)
sleep_time = config.as_int("daemon.sleep_time")
try:
validate_config(config)
except ConfigError as e:
print("Error: " + str(e))
return
try:
driver = create_driver(config)
except DriverError as e:
print("Error: " + str(e))
return
print("Logging in to Mergin...")
try:
mc = create_mergin_client()
# initialize or pull changes to sync with latest project version
if not os.path.exists(config.project_working_dir):
files_to_sync = mc_download(mc)
media_sync_push(mc, driver, files_to_sync)
except MediaSyncError as e:
print("Error: " + str(e))
return
# keep running until killed by ctrl+c:
# - sleep N seconds
# - pull
# - push
while True:
print(datetime.datetime.now())
try:
files_to_sync = mc_pull(mc)
media_sync_push(mc, driver, files_to_sync)
# check mergin client token expiration
delta = mc._auth_session["expire"] - datetime.datetime.now(
datetime.timezone.utc
)
if delta.total_seconds() < 3600:
mc = create_mergin_client()
except MediaSyncError as e:
print("Error: " + str(e))
print("Going to sleep")
time.sleep(sleep_time)
if __name__ == "__main__":
main()