Playground goes to the DB for the refresh_token

database
Mark Cotton 2023-09-12 21:13:01 -05:00
parent 281b5be9ff
commit fbfbb70f83
1 changed files with 84 additions and 124 deletions

View File

@ -8,6 +8,17 @@
"# 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",
@ -77,6 +88,16 @@
"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,
@ -112,9 +133,18 @@
"metadata": {},
"outputs": [],
"source": [
"if een:\n",
" 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 = ''"
" # 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')"
]
},
{
@ -124,13 +154,13 @@
"metadata": {},
"outputs": [],
"source": [
"een.login_tokens(code=None, cascade=True, refresh_token=een.refresh_token)"
"_ = een.login_tokens(code=None, cascade=True, refresh_token=een.refresh_token)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1405ab3-81d9-4add-8876-d5396c81a3ac",
"id": "a6a4aaac-64b0-424b-b5a1-50a3630e3b6f",
"metadata": {},
"outputs": [],
"source": [
@ -140,11 +170,29 @@
{
"cell_type": "code",
"execution_count": null,
"id": "79badad1-2ad1-4029-adba-c0ab7abf4a3e",
"id": "205b1c7b-0845-4412-a77e-98e6506d36de",
"metadata": {},
"outputs": [],
"source": [
"een.get_base_url()"
"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()"
]
},
{
@ -186,7 +234,7 @@
},
"outputs": [],
"source": [
"r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*14), end_timestamp=een.time_now())"
"_ = r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*1), end_timestamp=een.time_now())"
]
},
{
@ -201,106 +249,10 @@
},
{
"cell_type": "markdown",
"id": "6adf2661-8d10-477e-9f76-d0f67b4857ac",
"id": "a924b74d-7a77-45b1-90a5-b9b0c3ab8d7d",
"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')"
"## Download all the videos ##"
]
},
{
@ -313,39 +265,39 @@
"sql = '''SELECT\n",
"\tdownload.id,\n",
"\tdownload.url,\n",
" camera.device_id,\n",
"\tcamera.device_id,\n",
"\tdownload.start_timestamp,\n",
" download.end_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 == '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"
"\tdownload.status == ?\n",
"\tAND camera.id == ?;'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b61db76-e925-4ccb-bead-39239615daae",
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"for row in tqdm(cur.execute(sql)):\n",
"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(fname)\n",
" r2d2.save_video_to_file(url=row[1], filename=fname)"
" 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()"
]
},
{
@ -353,6 +305,14 @@
"id": "4f728219-12e2-4b60-b0af-0320efec95fb",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a72cb7f-a6c5-49c1-8dca-38fe4a4981bb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {