Get Event Data in Excel using API

Introduction

To automate the process of Event data using the 3dEYE API, please follow the steps below. 

Before starting there are a few steps to be followed. The code will be written in Python language and Python should be installed on the system. A software called "Anaconda" can be downloaded on the system to install Python and Jupyter Notebook IDE to interact with Python script. 


  • Installing Anaconda

Please follow the link below to download the Anaconda application.

https://www.anaconda.com/ 

Select the appropriate Operating System and select download. After the installation please select Jupyter Notebook from the Anaconda dashboard.


  • Working on Jupyter Notebook

For information on how to use Jupyter Notebook please visit this page↗️

Once the Jupyter Notebook opens up in the browser, go to the location where you want the code to be saved and then open the new Python3(ipykernel) notebook from the 'NEW' option. This will open a new tab in the browser.


        

 

The new tab will show your project, this is an empty Jupyter Notebook. 


             

 

The code should start from the import of the required libraries in the notebook. Copy and paste the following lines into your notebook:

import requests
import json
import pandas as pd
from urllib.parse import quote

In the second line, we will define the variables required later. Replace your username, password, and API key with your actual credentials.

url = "https://admin-api.3deye.me/v3/auth/token"
username = quote(Your username)
password = 'Your Password'
apikey = 'Your API Key'

payload = f'username={username}&password={password}'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'x-api-key': f'{apikey}'
}

  • Obtaining API Key for access 

To obtain the 3dEYE API key, go to admin.3deye.me, then click on the API Keys tab if you are signed in as an integrator, if you are signed in as a customer then you need to request your API key from the integrator.


  • Generating Token

Before you can start getting event data you need to obtain a "token" using the get method. To obtain a token you will have to use your integrator/customer user credentials and integrator API key. 
The URL for generating Token is: https://admin-api.3deye.me/v3/auth/token

In the third line, we will send a POST request to the above URL. If the return response is "200", it means that the information provided is correct and we have received the token in response. If the response is something else, this means that the credentials used are not accurate.

response = requests.request("POST", url, headers=headers, data=payload)
print(response)

Output:

<Response [200]>

We will now create a function that will convert the response to a JSON object which will be useful in extracting data.

def response_to_json(response):
data = json.loads(response.text)
return data

Now we will save the token in a variable that can be used for another request to 3dEYE API.

data =response_to_json(response)
token = data['accessToken']

  • Using the generated Token to get Event Data

Now we will change the URL to get the list of Events from a camera the customer has. The new URL will be: f"https://admin-api.3deye.me/v3/events/camera?cameraId={camera_id}&from={time_from}&to={time_to}&type={event_type}"

In the new empty line, we will add the code below. Please ensure the time format is maintained, or else the API will return an error. Change the camera ID with the ID of the actual camera. For event type, enter "Analytics" if the data you want is for object detection, or enter "Motion", to get motion events. For this tutorial, we will be using Motion as an event type.

df = pd.DataFrame()
camera_id = "Your Camer Id"
time_from = '2023-12-12T15:06:38Z'
time_to = '2023-12-13T15:06:38Z'
event_type = 'Motion'
url = f"https://admin-api.3deye.me/v3/events/camera?cameraId={camera_id}&from={time_from}&to={time_to}&type={event_type}"
payload = {}
headers = {
'Authorization': f'Bearer {token}',
'x-api-key': f'{apikey}'
}

response = requests.request("GET", url, headers=headers, data=payload)
camera_data = response_to_json(response)

try:
camera_data = pd.json_normalize(camera_data['data'])
except KeyError:
print(camera_data)

df = pd.concat([df, camera_data], ignore_index=True)
print(df['id'])

  • Saving the Event Data as an Excel File

To save the returned event data as an Excel sheet, we will use the pandas' library. 

We will write the below code in a new line.

df.to_excel('Filename.xlsx')

The file will be saved in the same folder where you created the Jupyter Notebook.

The Jupyter Notebook will look like this:


            


             

The saved Excel file will have data in a similar form.


    Tags