|
| 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