Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor package to be imported as a single package #37

Merged
merged 14 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
secrets.cfg
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
24 changes: 0 additions & 24 deletions Camera.py

This file was deleted.

17 changes: 0 additions & 17 deletions ConfigHandler.py

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<p align="center">
<img alt="Reolink Approval" src="https://img.shields.io/badge/reolink-approved-blue?style=flat-square">
<img alt="GitHub" src="https://img.shields.io/github/license/ReolinkCameraApi/reolink-python-api?style=flat-square">
<img alt="GitHub tag (latest SemVer)" src="https://img.shields.io/github/v/tag/ReolinkCameraApi/reolink-python-api?style=flat-square">
<img alt="PyPI" src="https://img.shields.io/pypi/v/reolink-api?style=flat-square">
<img alt="GitHub" src="https://img.shields.io/github/license/ReolinkCameraAPI/reolinkapipy?style=flat-square">
<img alt="GitHub tag (latest SemVer)" src="https://img.shields.io/github/v/tag/ReolinkCameraAPI/reolinkapipy?style=flat-square">
<img alt="PyPI" src="https://img.shields.io/pypi/v/reolinkapi?style=flat-square">
<img alt="Discord" src="https://img.shields.io/discord/773257004911034389?style=flat-square">
</p>

Expand Down
4 changes: 0 additions & 4 deletions api/__init__.py

This file was deleted.

111 changes: 0 additions & 111 deletions api/recording.py

This file was deleted.

11 changes: 11 additions & 0 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import reolinkapi

if __name__ == "__main__":
cam = reolinkapi.Camera("192.168.0.102", defer_login=True)

# must first login since I defer have deferred the login process
cam.login()

dst = cam.get_dst()
ok = cam.add_user("foo", "bar", "admin")
alarm = cam.get_alarm_motion()
44 changes: 44 additions & 0 deletions examples/download_motions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Downloads all motion events from camera from the past hour."""
import os
from configparser import RawConfigParser
from datetime import datetime as dt, timedelta
from reolinkapi import Camera


def read_config(props_path: str) -> dict:
"""Reads in a properties file into variables.

NB! this config file is kept out of commits with .gitignore. The structure of this file is such:
# secrets.cfg
[camera]
ip={ip_address}
username={username}
password={password}
"""
config = RawConfigParser()
assert os.path.exists(props_path), f"Path does not exist: {props_path}"
config.read(props_path)
return config


# Read in your ip, username, & password
# (NB! you'll likely have to create this file. See tests/test_camera.py for details on structure)
config = read_config('../secrets.cfg')

ip = config.get('camera', 'ip')
un = config.get('camera', 'username')
pw = config.get('camera', 'password')

# Connect to camera
cam = Camera(ip, un, pw)

start = (dt.now() - timedelta(hours=1))
end = dt.now()
# Collect motion events between these timestamps for substream
processed_motions = cam.get_motion_files(start=start, end=end, streamtype='sub')

dl_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
for i, motion in enumerate(processed_motions):
fname = motion['filename']
# Download the mp4
resp = cam.get_file(fname, output_path=os.path.join(dl_dir, f'motion_event_{i}.mp4'))
3 changes: 1 addition & 2 deletions examples/streaming_video.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cv2

from Camera import Camera
from reolinkapi import Camera


def non_blocking():
Expand Down
4 changes: 4 additions & 0 deletions reolinkapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from reolinkapi.handlers.api_handler import APIHandler
from .camera import Camera

__version__ = "0.1.2"
38 changes: 38 additions & 0 deletions reolinkapi/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from reolinkapi.handlers.api_handler import APIHandler


class Camera(APIHandler):

def __init__(self, ip: str,
username: str = "admin",
password: str = "",
https: bool = False,
defer_login: bool = False,
**kwargs):
"""
Initialise the Camera object by passing the ip address.
The default details {"username":"admin", "password":""} will be used if nothing passed
For deferring the login to the camera, just pass defer_login = True.
For connecting to the camera behind a proxy pass a proxy argument: proxy={"http": "socks5://127.0.0.1:8000"}
:param ip:
:param username:
:param password:
:param https: connect to the camera over https
:param defer_login: defer the login process
:param proxy: Add a proxy dict for requests to consume.
eg: {"http":"socks5://[username]:[password]@[host]:[port], "https": ...}
More information on proxies in requests: https://stackoverflow.com/a/15661226/9313679
"""
# For when you need to connect to a camera behind a proxy, pass
# a proxy argument: proxy={"http": "socks5://127.0.0.1:8000"}
APIHandler.__init__(self, ip, username, password, https=https, **kwargs)

# Normal call without proxy:
# APIHandler.__init__(self, ip, username, password)

self.ip = ip
self.username = username
self.password = password

if not defer_login:
super().login()
Empty file added reolinkapi/handlers/__init__.py
Empty file.
Loading