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 pathutils.py
60 lines (44 loc) · 1.4 KB
/
utils.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
# coding: utf-8
"""Implementation of Arlo utils."""
import logging
import time
from datetime import datetime as dt
import requests
_LOGGER = logging.getLogger(__name__)
def to_datetime(timestamp):
"""Return datetime object from timestamp."""
return dt.fromtimestamp(time.mktime(
time.localtime(int(str(timestamp)[:10]))))
def pretty_timestamp(timestamp, date_format='%a-%m_%d_%y:%H:%M:%S'):
"""Huminize timestamp."""
return time.strftime(date_format,
time.localtime(int(str(timestamp)[:10])))
def http_get(url, filename=None):
"""Download HTTP data."""
try:
ret = requests.get(url)
except requests.exceptions.SSLError as error:
_LOGGER.error(error)
return False
if ret.status_code != 200:
return False
if filename is None:
return ret.content
with open(filename, 'wb') as data:
data.write(ret.content)
return True
def http_stream(url, chunk=4096):
"""Generate stream for a given record video.
:param chunk: chunk bytes to read per time
:returns generator object
"""
ret = requests.get(url, stream=True)
ret.raise_for_status()
for data in ret.iter_content(chunk):
yield data
def assert_is_dict(var):
"""Assert variable is from the type dictionary."""
if var is None or not isinstance(var, dict):
return {}
return var
# vim:sw=4:ts=4:et: