Skip to content

Commit d6cd227

Browse files
authored
- Added ability to pass the paramenter 'older_than' to history() method (python-ring-doorbell#69)
- Added script which allows count the number of videos and download them
1 parent b36576f commit d6cd227

2 files changed

Lines changed: 157 additions & 1 deletion

File tree

ring_doorbell/doorbot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ def existing_doorbell_type_duration(self, value):
155155
return None
156156

157157
def history(self, limit=30, timezone=None, kind=None,
158-
enforce_limit=False, retry=8):
158+
enforce_limit=False, older_than=None, retry=8):
159159
"""
160160
Return history with datetime objects.
161161
162162
:param limit: specify number of objects to be returned
163163
:param timezone: determine which timezone to convert data objects
164164
:param kind: filter by kind (ding, motion, on_demand)
165165
:param enforce_limit: when True, this will enforce the limit and kind
166+
:param older_than: return older objects than the passed event_id
166167
:param retry: determine the max number of attempts to archive the limit
167168
"""
168169
queries = 0
@@ -174,6 +175,8 @@ def history(self, limit=30, timezone=None, kind=None,
174175

175176
while True:
176177
params = {'limit': str(limit)}
178+
if older_than:
179+
params['older_than'] = older_than
177180

178181
url = API_URI + URL_DOORBELL_HISTORY.format(self.account_id)
179182
response = self._ring.query(url, extra_params=params)

scripts/ringcli.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/usr/bin/env python
2+
# vim:sw=4:ts=4:et
3+
# Many thanks to @troopermax <https://github.com/troopermax>
4+
5+
import getpass
6+
import argparse
7+
from ring_doorbell import Ring
8+
9+
10+
def _header():
11+
_bar()
12+
print("Ring CLI")
13+
14+
15+
def _bar():
16+
print('---------------------------------')
17+
18+
19+
def get_username():
20+
try:
21+
username = raw_input("Username: ")
22+
except NameError:
23+
username = input("Username: ")
24+
return username
25+
26+
27+
def _format_filename(event):
28+
if not isinstance(event, dict):
29+
return
30+
31+
if event['answered']:
32+
answered_status = 'answered'
33+
else:
34+
answered_status = 'not_answered'
35+
36+
filename = "{}_{}_{}_{}".format(event['created_at'],
37+
event['kind'],
38+
answered_status,
39+
event['id'])
40+
41+
filename = filename.replace(' ', '_').replace(':', '.')+'.mp4'
42+
return filename
43+
44+
45+
def main():
46+
47+
parser = argparse.ArgumentParser(
48+
description='Ring Doorbell',
49+
epilog='https://github.com/tchellomello/python-ring-doorbell',
50+
formatter_class=argparse.RawDescriptionHelpFormatter)
51+
52+
parser.add_argument('-u',
53+
'--username',
54+
dest='username',
55+
type=str,
56+
help='username for Ring account')
57+
58+
parser.add_argument('-p',
59+
'--password',
60+
type=str,
61+
dest='password',
62+
help='username for Ring account')
63+
64+
parser.add_argument('--count',
65+
action='store_true',
66+
default=False,
67+
help='count the number of videos on your Ring account')
68+
69+
parser.add_argument('--download-all',
70+
action='store_true',
71+
default=False,
72+
help='download all videos on your Ring account')
73+
74+
args = parser.parse_args()
75+
_header()
76+
77+
if not args.username:
78+
args.username = get_username()
79+
80+
if not args.password:
81+
args.password = getpass.getpass("Password: ")
82+
83+
# connect to Ring account
84+
myring = Ring(args.username, args.password)
85+
doorbell = myring.doorbells[0]
86+
87+
_bar()
88+
89+
if args.count:
90+
print("\tCounting videos linked on your Ring account.\n" +
91+
"\tThis may take some time....\n")
92+
93+
events = []
94+
counter = 0
95+
history = doorbell.history(limit=100)
96+
while (len(history) > 0):
97+
events += history
98+
counter += len(history)
99+
history = doorbell.history(older_than=history[-1]['id'])
100+
101+
motion = len([m['kind'] for m in events if m['kind'] == 'motion'])
102+
ding = len([m['kind'] for m in events if m['kind'] == 'ding'])
103+
on_demand = \
104+
len([m['kind'] for m in events if m['kind'] == 'on_demand'])
105+
106+
print("\tTotal videos: {}".format(counter))
107+
print("\tDing triggered: {}".format(ding))
108+
print("\tMotion triggered: {}".format(motion))
109+
print("\tOn-Demand triggered: {}".format(on_demand))
110+
111+
# already have all events in memory
112+
if args.download_all:
113+
counter = 0
114+
print("\tDownloading all videos linked on your Ring account.\n" +
115+
"\tThis may take some time....\n")
116+
117+
for event in events:
118+
counter += 1
119+
filename = _format_filename(event)
120+
print("\t{}/{} Downloading {}".format(counter,
121+
len(events),
122+
filename))
123+
124+
doorbell.recording_download(event['id'],
125+
filename=filename,
126+
override=False)
127+
128+
if args.download_all and not args.count:
129+
print("\tDownloading all videos linked on your Ring account.\n" +
130+
"\tThis may take some time....\n")
131+
history = doorbell.history(limit=100)
132+
133+
while (len(history) > 0):
134+
print("\tProcessing and downloading the next" +
135+
" videos".format(len(history)))
136+
137+
counter = 0
138+
for event in history:
139+
counter += 1
140+
filename = _format_filename(event)
141+
print("\t{}/{} Downloading {}".format(counter,
142+
len(history),
143+
filename))
144+
145+
doorbell.recording_download(event['id'],
146+
filename=filename,
147+
override=False)
148+
149+
history = doorbell.history(limit=100, older_than=history[-1]['id'])
150+
151+
152+
if __name__ == '__main__':
153+
main()

0 commit comments

Comments
 (0)