-
Notifications
You must be signed in to change notification settings - Fork 1
/
blackvue_wifi.py
128 lines (101 loc) · 3.5 KB
/
blackvue_wifi.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
"""
Connect with the HTTP API of a BlackVue dashcam. Use the tool to sync files
or modify settings.
Usage:
blackvue_wifi --download-all-recordings <foldername>
blackvue_wifi --download-missing-recordings [--continuously] <foldername>
"""
import warnings
import confidence
import requests
import wget
from docopt import docopt
from tqdm import tqdm
class BlackVueClient:
def __init__(self, config=None):
"""
An HTTP API client for BlackVue dashcams.
:param config: an object containing configuration (generated by
docopt or confidence for example)
"""
self.endpoint = config.endpoint
if not self.dashcam_present():
warnings.warn('Could not find a connected dashcam')
def dashcam_present(self):
"""
Boolean indicating presence of a dashcam.
:return: True if dashcam is found, False otherwise
"""
try:
return 'BlackVue' in self.get('', timeout=5).text
except requests.exceptions.ConnectTimeout:
return False
except requests.ConnectionError:
return False
def list_recordings(self):
"""
List all recordings on the connected dashcam.
:return: a list of recordings
"""
lines = self.get('blackvue_vod.cgi').text.split('\n')
version, recordings = lines[0], lines[1:]
recordings = [recording.split(',') for recording in recordings]
recordings = [recording[0].split(":")[1] for recording in
recordings[:-1]]
return recordings
def download_all_recordings(self, *args, **kwargs):
"""
Download all recordings on the connected dashcam with wget for Python.
"""
for recording in tqdm(self.list_recordings()):
self.download(recording, *args, **kwargs)
def get_config(self):
"""
:return:
"""
return self.get('Config/config.ini').text
def set_config(self):
"""
:return:
"""
def get_dashcam_metadata(self):
"""
:return:
"""
return self.get('Config/version.bin').text
def upload(self):
"""
:return:
"""
def absolute_url(self, path):
"""
Create a full URL from a partial path.
:param path: a relative URL
:return: full URL
"""
return "/".join(['http:/', self.endpoint, path])
def post(self, path, *args, **kwargs):
return requests.post(self.absolute_url(path),
*args, **kwargs)
def get(self, path, *args, **kwargs):
return requests.get(self.absolute_url(path),
*args, **kwargs)
def download(self, path, *args, **kwargs):
return wget.download(self.absolute_url(path),
*args, **kwargs)
def main():
client = BlackVueClient(config=confidence.Configuration(
confidence.Configuration({'endpoint': '10.99.77.1'}),
confidence.load_name('blackvue'),
confidence.Configuration(docopt(__doc__))
))
options = docopt(__doc__)
if options['--download-all-recordings']:
client.download_all_recordings(out=options['<foldername>'])
if options['--download-missing-recordings']:
if options['--continuously']:
print("Continous syncing will be included in the next version")
else:
print("Incremental downloads will be included in the next version")
if __name__ == "__main__":
main()