{ "cells": [ { "cell_type": "markdown", "id": "b276f988-0f92-4ac0-b440-523252f841e9", "metadata": {}, "source": [ "# Playing around with Machine to Machine API access" ] }, { "cell_type": "code", "execution_count": null, "id": "0aac543d-e9f0-4a10-b4c2-35466085f2c2", "metadata": {}, "outputs": [], "source": [ "import requests\n", "from tqdm import tqdm" ] }, { "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": "c960ddb6-316f-4737-bd88-d796cdd7897b", "metadata": {}, "outputs": [], "source": [ "sqlite3.sqlite_version" ] }, { "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:\n", " if een.refresh_token == None or een.refresh_token == '':\n", " # if you get out of sync, pull the refresh_token from the db to get the loop started\n", " result = cur.execute(\"select user.refresh_token from user where user.email = ?;\", (\"mcotton@mcottondesign.com\",))\n", " \n", " for row in result:\n", " een.refresh_token = row[0]\n", " else:\n", " # een object and refresh_token appear to be good\n", " pass\n", "else:\n", " logging.error('een object is None')" ] }, { "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": "a6a4aaac-64b0-424b-b5a1-50a3630e3b6f", "metadata": {}, "outputs": [], "source": [ "een.refresh_token" ] }, { "cell_type": "code", "execution_count": null, "id": "205b1c7b-0845-4412-a77e-98e6506d36de", "metadata": {}, "outputs": [], "source": [ "een.current_user" ] }, { "cell_type": "code", "execution_count": null, "id": "e640fd2d-05eb-42c1-bcf6-3183afa065ef", "metadata": {}, "outputs": [], "source": [ "result = cur.execute(\"select user.refresh_token, user.id from user where user.email = ?;\", (een.current_user['email'],))\n", "\n", "for row in result:\n", " print(f\"found user {een.current_user['email']}, updating refresh_token\")\n", " print(row)\n", " print(een.refresh_token)\n", " print(f\"update user set refresh_token = {een.refresh_token} where id == {row[1]};\")\n", " cur.execute(\"update user set refresh_token = ? where id == ?;\", (een.refresh_token, row[1]))\n", " con.commit()" ] }, { "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*1), 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": "a924b74d-7a77-45b1-90a5-b9b0c3ab8d7d", "metadata": {}, "source": [ "## Download all the videos ##" ] }, { "cell_type": "code", "execution_count": null, "id": "74378ae5-0ade-4d8a-9636-2bd2800e59a3", "metadata": {}, "outputs": [], "source": [ "sql = '''SELECT\n", "\tdownload.id,\n", "\tdownload.url,\n", "\tcamera.device_id,\n", "\tdownload.start_timestamp,\n", "\tdownload.end_timestamp,\n", "\tdownload.status\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 == ?\n", "\tAND camera.id == ?;'''" ] }, { "cell_type": "code", "execution_count": null, "id": "8b61db76-e925-4ccb-bead-39239615daae", "metadata": { "scrolled": true }, "outputs": [], "source": [ "results = cur.execute(sql, ('new', 4))\n", "\n", "for row in tqdm(results):\n", " fname = f\"videos/{row[2]}/{row[2]}_{row[3]}-{row[4]}.mp4\"\n", " print(f\"{row[0]} - {row[5]}\")\n", " r2d2.save_video_to_file(url=row[1], filename=fname)\n", " new_cur = con.cursor()\n", " new_cur.execute(\"update download set status = ? where download.id == ?;\", ('done', row[0]))\n", " \n", " # less efficient to commit every iteration but it means I can watch the updates at the DB level\n", " con.commit()" ] }, { "cell_type": "raw", "id": "4f728219-12e2-4b60-b0af-0320efec95fb", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7a72cb7f-a6c5-49c1-8dca-38fe4a4981bb", "metadata": {}, "outputs": [], "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 }