This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmedia.py
219 lines (178 loc) · 6.64 KB
/
media.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# coding: utf-8
"""Implementation of Arlo Media object."""
import logging
from datetime import datetime
from datetime import timedelta
from pyarlo.const import LIBRARY_ENDPOINT, PRELOAD_DAYS
from pyarlo.utils import (
http_get, http_stream, to_datetime, pretty_timestamp,
assert_is_dict)
_LOGGER = logging.getLogger(__name__)
class ArloMediaLibrary(object):
"""Arlo Library Media module implementation."""
def __init__(self, arlo_session, preload=True, days=PRELOAD_DAYS):
"""Initialiaze Arlo Media Library object.
:param arlo_session: PyArlo shared session
:param preload: Boolean to pre-load video library.
:param days: If preload, number of days to lookup.
:returns ArloMediaLibrary object
"""
self._session = arlo_session
if preload and days:
self.videos = self.load(days)
else:
self.videos = []
def __repr__(self):
"""Representation string of object."""
return "<{0}: {1}>".format(self.__class__.__name__,
self._session.userid)
def load(self, days=PRELOAD_DAYS, only_cameras=None,
date_from=None, date_to=None, limit=None):
"""Load Arlo videos from the given criteria
:param days: number of days to retrieve
:param only_cameras: retrieve only <ArloCamera> on that list
:param date_from: refine from initial date
:param date_to: refine final date
:param limit: define number of objects to return
"""
videos = []
url = LIBRARY_ENDPOINT
if not (date_from and date_to):
now = datetime.today()
date_from = (now - timedelta(days=days)).strftime('%Y%m%d')
date_to = now.strftime('%Y%m%d')
params = {'dateFrom': date_from, 'dateTo': date_to}
data = self._session.query(url,
method='POST',
extra_params=params).get('data')
# get all cameras to append to create ArloVideo object
all_cameras = self._session.cameras
for video in data:
# pylint: disable=cell-var-from-loop
try:
srccam = \
list(filter(
lambda cam: cam.device_id == video.get('deviceId'),
all_cameras)
)[0]
except IndexError:
continue
# make sure only_cameras is a list
if only_cameras and \
not isinstance(only_cameras, list):
only_cameras = [(only_cameras)]
# filter by camera only
if only_cameras:
if list(filter(lambda cam: cam.device_id == srccam.device_id,
list(only_cameras))):
videos.append(ArloVideo(video, srccam, self._session))
else:
videos.append(ArloVideo(video, srccam, self._session))
if limit:
return videos[:limit]
return videos
class ArloVideo(object):
"""Object for Arlo Video file."""
def __init__(self, attrs, camera, arlo_session):
"""Initialiaze Arlo Video object.
:param attrs: Video attributes
:param camera: Arlo camera which recorded the video
:param arlo_session: Arlo shared session
"""
self._attrs = attrs
self._attrs = assert_is_dict(self._attrs)
self._camera = camera
self._session = arlo_session
def __repr__(self):
"""Representation string of object."""
return "<{0}: {1}>".format(self.__class__.__name__, self._name)
@property
def _name(self):
"""Define object name."""
return "{0} {1} {2}".format(
self._camera.name,
pretty_timestamp(self.created_at),
self._attrs.get('mediaDuration'))
# pylint: disable=invalid-name
@property
def id(self):
"""Return object id."""
if self._attrs is not None:
return self._attrs.get('name')
return None
@property
def created_at(self):
"""Return timestamp."""
if self._attrs is not None:
return self._attrs.get('localCreatedDate')
return None
def created_at_pretty(self, date_format=None):
"""Return pretty timestamp."""
if date_format:
return pretty_timestamp(self.created_at, date_format=date_format)
return pretty_timestamp(self.created_at)
@property
def created_today(self):
"""Return True if created today."""
if self.datetime.date() == datetime.today().date():
return True
return False
@property
def datetime(self):
"""Return datetime when video was created."""
return to_datetime(self.created_at)
@property
def content_type(self):
"""Return content_type."""
if self._attrs is not None:
return self._attrs.get('contentType')
return None
@property
def camera(self):
"""Return camera object that recorded video."""
return self._camera
@property
def media_duration_seconds(self):
"""Return media duration in seconds."""
if self._attrs is not None:
return self._attrs.get('mediaDurationSecond')
return None
@property
def triggered_by(self):
"""Return the reason why video was recorded."""
if self._attrs is not None:
return self._attrs.get('reason')
return None
@property
def thumbnail_url(self):
"""Return thumbnail url."""
if self._attrs is not None:
return self._attrs.get('presignedThumbnailUrl')
return None
@property
def video_url(self):
"""Return video content url."""
if self._attrs is not None:
return self._attrs.get('presignedContentUrl')
return None
@property
def motion_type(self):
"""Returns the type of motion that triggered the camera. Requires subscription."""
if self._attrs is not None:
return self._attrs.get("objCategory")
return None
def download_thumbnail(self, filename=None):
"""Download JPEG thumbnail.
:param filename: File to save thumbnail. Default: stdout
"""
return http_get(self.thumbnail_url, filename)
def download_video(self, filename=None):
"""Download video content.
:param filename: File to save video. Default: stdout
"""
return http_get(self.video_url, filename)
@property
def stream_video(self):
"""Stream video."""
return http_stream(self.video_url)
# vim:sw=4:ts=4:et: