In [ ]:
import requests
import pprint
pp = pprint.PrettyPrinter(indent=2)
# API Testing
r = requests.get('https://api.evercam.io/v1/cameras?api_id=7c238a82&api_key=0f001555dff7c3469b8d21c5ba1db51c')
response_dict = r.json()
camera_list_info = []
for k, cameras in response_dict.items():
for camera in cameras:
temp_dict = {
'id': camera.get('id', 'empty'),
'owner': camera.get('owner', 'empty'),
'username': camera.get('cam_username', 'empty'),
'password': camera.get('cam_password', 'empty'),
'vendor_id': camera.get('vendor_id', 'empty'),
'host': camera.get('external', {}).get('host', 'empty'), # multidimensional dicts
'http': camera.get('external', {}).get('http', {}).get('camera', 'empty'),
'rtsp': camera.get('external', {}).get('rtsp', {}).get('h264', 'empty'),
}
camera_list_info.append(temp_dict)
pp.pprint(camera_list_info)
In [2]:
r_2 = requests.get('https://api.evercam.io/v1/cameras/my_camera_1?api_id=7c238a82&api_key=0f001555dff7c3469b8d21c5ba1db51c')
response_dict_2 = r_2.json()
# pp.pprint(response_dict_2)
for k, camera in response_dict.iteritems():
pp.pprint(camera[0])
In [3]:
# API Testing with Python3
API_URL = 'https://api.evercam.io/v1/'
API_ID = '7c238a82'
API_KEY = '0f001555dff7c3469b8d21c5ba1db51c'
class SmartcamAPI():
""" Handle Evercam's APIs. Use globaly available API_URL, API_ID, API_KEY
Members order:
1. cameras_api_response or camera_api_response
2. get_api_response -> build_api_url
3. get_camera_info
"""
def __init__(self, api_url, api_id, api_key, api_request):
''' Initiate instance variables:
self.api_url: a string representing api url
self.api_id : a string representing api id
self.api_key: encrypted string representing public key
api_request : request specifier (i.e. cameras, camera, etc. )
'''
self.api_url = api_url
self.api_id = api_id
self.api_key = api_key
self.api_request = api_request
def cameras_api_response(self):
''' Returns a list of dictionary objects, where each object represents a camera.
'''
camera_list = []
response_dict = self.get_api_response()
for k, cameras in response_dict.items():
for camera in cameras:
temp_dict = self.get_camera_info(camera)
camera_list.append(temp_dict)
return camera_list
def camera_api_response(self):
''' Returns a single camera object with all parameters.
'''
camera_info = {}
response_dict = self.get_api_response()
for k, values in response_dict.items():
camera = values[0]
camera_info.update(self.get_camera_info(camera))
return camera_info
def get_api_response(self):
''' Get a JSON response from the specified API and convert it into a dictionary.
Return this response dictionary to calling members.
'''
response = requests.get(self.build_api_url())
response_dict = response.json()
return response_dict
def build_api_url(self):
''' Builds the url string with specified api information.
'''
api_name = self.api_url + self.api_request + '?' + 'api_id=' + self.api_id + '&' + 'api_key=' + self.api_key
return api_name
def get_camera_info(self, camera_dict):
''' Given a dictionary with information regarding a single camera returned from the API,
return specific information (username, password, http, rtsp, etc.) needed to generete the feed.
'''
temp_dict = {
'id': camera_dict.get('id', 'empty'),
'owner': camera_dict.get('owner', 'empty'),
'username': camera_dict.get('cam_username', 'empty'),
'password': camera_dict.get('cam_password', 'empty'),
'vendor_id': camera_dict.get('vendor_id', 'empty'),
'host': camera_dict.get('external', {}).get('host', 'empty'), # multidimensional dicts
'http': camera_dict.get('external', {}).get('http', {}).get('camera', 'empty'),
'rtsp': camera_dict.get('external', {}).get('rtsp', {}).get('h264', 'empty'),
}
return temp_dict
smartcam_obj = SmartcamAPI(API_URL, API_ID, API_KEY, 'cameras')
camera_list = smartcam_obj.cameras_api_response()
pp.pprint(camera_list)
camera_id = 'my_camera_3'
smartcam_obj = SmartcamAPI(API_URL, API_ID, API_KEY, 'cameras/' + str(camera_id))
camera_info = smartcam_obj.camera_api_response()
pp.pprint(camera_info)
In [ ]: