/
virb.py
61 lines (47 loc) · 1.88 KB
/
virb.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
from threading import Thread
import requests
import json
class Virb(object):
def __init__(self, ip):
self._ip = ip
def _send_command(self, data):
json_data = json.dumps(data)
r = requests.post("http://%s/virb" % self._ip, data=json_data)
if r.status_code != 200:
raise Exception("Virb returned HTTP %d %s for request %s" % (r.status_code, r.reason, data['command']))
res = json.loads(r.text)
if res['result'] != 1:
raise Exception("Virb returned error code %d for request %s" % (res['result'], data['command']))
return res
def test_connection(self):
status = self.get_status()
# print(status)
charge_state = ''
if status['batteryChargingState'] > 0:
charge_state = ' (charging)'
print("Virb battery : %d%%%s" % (status['batteryLevel'], charge_state))
res = self.get_commandList()
print("Supported command:")
for command in res['commandList']:
print(" - %s(%d)" % (command['command'], command['version']))
print("Virb status is %s" % (status['state']))
if status['state'] != 'idle':
print("stop recording...")
self.stop_recording()
print("Virb status is %s" % (status['state']))
def start_recording(self):
self._send_command({"command": "startRecording"})
def stop_recording(self):
self._send_command({"command": "stopRecording"})
def get_status(self):
res = self._send_command({"command": "status"})
return res
def get_features(self):
res = self._send_command({"command": "features"})
return res
def get_commandList(self):
res = self._send_command({"command": "commandList"})
return res
def get_mediaList(self):
res = self._send_command({"command": "mediaList"})
return res