forked from python-ring-doorbell/python-ring-doorbell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringcli.py
More file actions
executable file
·170 lines (129 loc) · 4.66 KB
/
ringcli.py
File metadata and controls
executable file
·170 lines (129 loc) · 4.66 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# vim:sw=4:ts=4:et
# Many thanks to @troopermax <https://github.com/troopermax>
import json
import getpass
import argparse
from pathlib import Path
from ring_doorbell import Ring, Auth
from oauthlib.oauth2 import MissingTokenError
def _header():
_bar()
print("Ring CLI")
def _bar():
print("---------------------------------")
cache_file = Path("test_token.cache")
def token_updated(token):
cache_file.write_text(json.dumps(token))
def _format_filename(event):
if not isinstance(event, dict):
return
if event["answered"]:
answered_status = "answered"
else:
answered_status = "not_answered"
filename = "{}_{}_{}_{}".format(
event["created_at"], event["kind"], answered_status, event["id"]
)
filename = filename.replace(" ", "_").replace(":", ".") + ".mp4"
return filename
def main():
parser = argparse.ArgumentParser(
description="Ring Doorbell",
epilog="https://github.com/tchellomello/python-ring-doorbell",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-u", "--username", dest="username", type=str, help="username for Ring account"
)
parser.add_argument(
"-p", "--password", type=str, dest="password", help="username for Ring account"
)
parser.add_argument(
"--count",
action="store_true",
default=False,
help="count the number of videos on your Ring account",
)
parser.add_argument(
"--download-all",
action="store_true",
default=False,
help="download all videos on your Ring account",
)
args = parser.parse_args()
_header()
# connect to Ring account
if cache_file.is_file():
auth = Auth("RingCLI/0.6", json.loads(cache_file.read_text()), token_updated)
else:
if not args.username:
args.username = input("Username: ")
if not args.password:
args.password = getpass.getpass("Password: ")
auth = Auth("RingCLI/0.6", None, token_updated)
try:
auth.fetch_token(args.username, args.password)
except MissingTokenError:
auth.fetch_token(args.username, args.password, input("2FA Code: "))
ring = Ring(auth)
ring.update_data()
devices = ring.devices()
doorbell = devices["doorbots"][0]
_bar()
if args.count:
print(
"\tCounting videos linked on your Ring account.\n"
+ "\tThis may take some time....\n"
)
events = []
counter = 0
history = doorbell.history(limit=100)
while len(history) > 0:
events += history
counter += len(history)
history = doorbell.history(older_than=history[-1]["id"])
motion = len([m["kind"] for m in events if m["kind"] == "motion"])
ding = len([m["kind"] for m in events if m["kind"] == "ding"])
on_demand = len([m["kind"] for m in events if m["kind"] == "on_demand"])
print("\tTotal videos: {}".format(counter))
print("\tDing triggered: {}".format(ding))
print("\tMotion triggered: {}".format(motion))
print("\tOn-Demand triggered: {}".format(on_demand))
# already have all events in memory
if args.download_all:
counter = 0
print(
"\tDownloading all videos linked on your Ring account.\n"
+ "\tThis may take some time....\n"
)
for event in events:
counter += 1
filename = _format_filename(event)
print("\t{}/{} Downloading {}".format(counter, len(events), filename))
doorbell.recording_download(
event["id"], filename=filename, override=False
)
if args.download_all and not args.count:
print(
"\tDownloading all videos linked on your Ring account.\n"
+ "\tThis may take some time....\n"
)
history = doorbell.history(limit=100)
while len(history) > 0:
print(
"\tProcessing and downloading the next "
+ format(len(history))
+ " videos"
)
counter = 0
for event in history:
counter += 1
filename = _format_filename(event)
print("\t{}/{} Downloading {}".format(counter, len(history), filename))
doorbell.recording_download(
event["id"], filename=filename, override=False
)
history = doorbell.history(limit=100, older_than=history[-1]["id"])
if __name__ == "__main__":
main()