{ "cells": [ { "cell_type": "markdown", "id": "b276f988-0f92-4ac0-b440-523252f841e9", "metadata": {}, "source": [ "# Playing around with Machine to Machine API access" ] }, { "cell_type": "markdown", "id": "5efb08d1-b0f6-4f64-b87a-f2f5afd4c050", "metadata": {}, "source": [ "## Let's do some EagleEye setup" ] }, { "cell_type": "code", "execution_count": null, "id": "8f557a22-7ffe-4c0d-910d-9dee63b40832", "metadata": {}, "outputs": [], "source": [ "import logging\n", "logger = logging.getLogger()\n", "logger.setLevel('INFO')" ] }, { "cell_type": "code", "execution_count": null, "id": "d8ced614-480a-4341-bc40-6c66e16b54cd", "metadata": {}, "outputs": [], "source": [ "from EagleEyev3 import *\n", "from settings import config" ] }, { "cell_type": "code", "execution_count": null, "id": "091f1c2f-6bff-40e2-a0e8-d22d5f0ccd31", "metadata": {}, "outputs": [], "source": [ "een = EagleEyev3(config)" ] }, { "cell_type": "code", "execution_count": null, "id": "23864933-181c-402e-b2f5-e47a9f1871c8", "metadata": {}, "outputs": [], "source": [ "een.__version__" ] }, { "cell_type": "markdown", "id": "2aaf14a9-f206-4909-a85f-fc736e964592", "metadata": {}, "source": [ "## Let's do some DB setup" ] }, { "cell_type": "code", "execution_count": null, "id": "12364fa5-fa06-419a-9d1c-b44f02c9ac63", "metadata": {}, "outputs": [], "source": [ "import sqlite3" ] }, { "cell_type": "code", "execution_count": null, "id": "d8ca6f35-ee80-4463-aab1-3c83e72068f0", "metadata": {}, "outputs": [], "source": [ "con = sqlite3.connect('instance/project.db')" ] }, { "cell_type": "code", "execution_count": null, "id": "a8c9679c-37e1-4091-998d-08fc83f9c53b", "metadata": {}, "outputs": [], "source": [ "cur = con.cursor()" ] }, { "cell_type": "markdown", "id": "80f58426-168f-47e1-b4f6-164cd5c6840a", "metadata": {}, "source": [ "## Login as a user with their refresh_token and query their cameras" ] }, { "cell_type": "code", "execution_count": null, "id": "954c5d7b-db09-483e-a5f0-a59fb5733c34", "metadata": {}, "outputs": [], "source": [ "if een.refresh_token == None or een.refresh_token == '':\n", " # if you get out of sync you manually copy a refresh_token here to get the loop started\n", " een.refresh_token = ''" ] }, { "cell_type": "code", "execution_count": null, "id": "a3cd64b9-3e3c-4f02-971c-1b4436a776ea", "metadata": {}, "outputs": [], "source": [ "een.login_tokens(code=None, cascade=True, refresh_token=een.refresh_token)" ] }, { "cell_type": "code", "execution_count": null, "id": "d1405ab3-81d9-4add-8876-d5396c81a3ac", "metadata": {}, "outputs": [], "source": [ "een.refresh_token" ] }, { "cell_type": "code", "execution_count": null, "id": "79badad1-2ad1-4029-adba-c0ab7abf4a3e", "metadata": {}, "outputs": [], "source": [ "een.get_base_url()" ] }, { "cell_type": "code", "execution_count": null, "id": "bdfde2f6-28b1-4623-8528-1492de00b83d", "metadata": {}, "outputs": [], "source": [ "_ = een.get_list_of_cameras()" ] }, { "cell_type": "code", "execution_count": null, "id": "e85f28ee-165d-442b-896e-05d74e29e644", "metadata": {}, "outputs": [], "source": [ "een.cameras" ] }, { "cell_type": "code", "execution_count": null, "id": "17806b22-5bad-497d-977d-b1c31ed48304", "metadata": {}, "outputs": [], "source": [ "r2d2 = een.cameras[3]" ] }, { "cell_type": "code", "execution_count": null, "id": "a6dc31ee-6041-44f7-8967-67856033a6a7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*14), end_timestamp=een.time_now())" ] }, { "cell_type": "code", "execution_count": null, "id": "3395164b-307d-473e-a090-fd782d0c1dc1", "metadata": {}, "outputs": [], "source": [ "len(r2d2.videos)" ] }, { "cell_type": "markdown", "id": "6adf2661-8d10-477e-9f76-d0f67b4857ac", "metadata": {}, "source": [ "## Poke around in the DB" ] }, { "cell_type": "code", "execution_count": null, "id": "d5faf8e2-bfa9-4e85-8713-355973daf3aa", "metadata": {}, "outputs": [], "source": [ "for row in cur.execute('SELECT * FROM download WHERE camera_id = 53 limit 10'):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "77a703da-8e9e-4e6e-90e7-1113d537362d", "metadata": {}, "outputs": [], "source": [ "sql = \"\"\"\n", "SELECT\n", "\tuser.email,\n", "\tcamera.name,\n", "\tdownload.url,\n", "\tcount(download.url)\n", "FROM\n", "\tcamera\n", "\tJOIN download ON download.camera_id = camera.id\n", "\tJOIN USER ON camera.user_id = user.id\n", "WHERE\n", "\tdownload.status = 'new'\n", "GROUP BY\n", "\tcamera.name\n", "ORDER BY\n", "\tcount(download.url)\n", "\tDESC;\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "e4a34474-3282-45cb-97fc-bedd0c0258f0", "metadata": {}, "source": [ "## Try downloading a video ##" ] }, { "cell_type": "code", "execution_count": null, "id": "3f5c62fd-e21d-4b5e-afa1-d63741cc10a2", "metadata": {}, "outputs": [], "source": [ "r2d2.videos[9]" ] }, { "cell_type": "code", "execution_count": null, "id": "8c5160f0-52aa-4790-8b80-9e7edd528148", "metadata": {}, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "execution_count": null, "id": "f81a8dd8-9190-4709-8a94-276552cbdd5f", "metadata": {}, "outputs": [], "source": [ "#req = requests.get(url=benny.videos[9]['mp4Url'], headers={\"Authorization\": f\"Bearer {een.access_token}\"})" ] }, { "cell_type": "code", "execution_count": null, "id": "854e94f9-5341-4e09-adfe-c704014dd6a6", "metadata": {}, "outputs": [], "source": [ "#req.headers" ] }, { "cell_type": "code", "execution_count": null, "id": "aebe701f-2e83-497d-aa1e-ed2ba26ea4d4", "metadata": {}, "outputs": [], "source": [ "#benny.save_video_to_file(url=benny.videos[9]['mp4Url'], filename='benny9.mp4')" ] }, { "cell_type": "code", "execution_count": null, "id": "74378ae5-0ade-4d8a-9636-2bd2800e59a3", "metadata": {}, "outputs": [], "source": [ "sql = '''SELECT\n", "\tdownload.id,\n", "\tdownload.url,\n", " camera.device_id,\n", "\tdownload.start_timestamp,\n", " download.end_timestamp\n", "FROM\n", "\tcamera\n", "\tJOIN download ON download.camera_id = camera.id\n", "\tJOIN USER ON camera.user_id = user.id\n", "WHERE\n", "\tdownload.status == 'new' \n", "\tand camera.id == 4;'''" ] }, { "cell_type": "code", "execution_count": null, "id": "0aac543d-e9f0-4a10-b4c2-35466085f2c2", "metadata": {}, "outputs": [], "source": [ "from tqdm import tqdm\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8b61db76-e925-4ccb-bead-39239615daae", "metadata": {}, "outputs": [], "source": [ "for row in tqdm(cur.execute(sql)):\n", " fname = f\"videos/{row[2]}/{row[2]}_{row[3]}-{row[4]}.mp4\"\n", " #print(fname)\n", " r2d2.save_video_to_file(url=row[1], filename=fname)" ] }, { "cell_type": "raw", "id": "4f728219-12e2-4b60-b0af-0320efec95fb", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "flask2", "language": "python", "name": "flask2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }