Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding vanilla Py as an option to download assets #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
"from pathlib import Path\n",
"from pprint import pprint\n",
"from zipfile import ZipFile\n",
"import requests\n",
"import time\n",
"import logging\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
Expand Down Expand Up @@ -675,6 +678,13 @@
"order_id = order[\"id\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Option 1: Using Planet CLI"
]
},
{
"cell_type": "code",
"execution_count": 22,
Expand All @@ -696,6 +706,58 @@
"!planet orders wait $order_id"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Option 2: Using Vanila Python\n",
"Please be aware that this script is programmed to regularly check the endpoint and will cease operation only upon the appearance of a download link. It lacks a timeout function, meaning that the loop is configured to run indefinitely."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n",
"\n",
"def check_download_link(url, api_key, minutes=0.5):\n",
" \"\"\" \n",
" Check if the download link is ready.\n",
" \n",
" Args:\n",
" url (str): api url.\n",
" api_key (str): api key.\n",
" minutes (float): time to wait before checking again. default is 30 seconds(0.5).\n",
"\n",
" Returns:\n",
" none\n",
" \n",
" \"\"\"\n",
" while True:\n",
" try:\n",
" response = requests.get(url, auth=requests.auth.HTTPBasicAuth(api_key, ''))\n",
" response.raise_for_status()\n",
" if \"results\" in response.json().get(\"_links\", {}):\n",
" return logging.info(f\"{date.datetime.now()}: Download link is ready.\")\n",
" logging.info(f\"{date.datetime.now()}: Waiting for download link to be ready...\")\n",
" except requests.RequestException as e:\n",
" return logging.error(f\"An error occurred: {e}\")\n",
" time.sleep(minutes * 60)\n",
"\n",
"\n",
"check_download_link(order['_links']['_self'], API_KEY, minutes=5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Option 1: Using Planet CLI"
]
},
{
"cell_type": "code",
"execution_count": 23,
Expand Down Expand Up @@ -723,6 +785,49 @@
"!planet orders download $order_id --directory $demo_data_dir"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Option 2: Using Vanila Python\n",
"Please be aware that this script is programmed to regularly check the endpoint and will cease operation only upon the appearance of a download link. It lacks a timeout function, meaning that the loop is configured to run indefinitely."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get the path to the Downloads folder\n",
"downloads_folder = os.path.join(os.path.expanduser('~'), 'Downloads/') # just a generic path, you can adjust it\n",
"file_path = os.path.join(downloads_folder)\n",
"\n",
"def download_order(url, api_key):\n",
" \"\"\" download ordered files \"\"\"\n",
" try:\n",
" response = requests.get(url, auth=requests.auth.HTTPBasicAuth(api_key, ''))\n",
" response.raise_for_status()\n",
" \n",
" results = response.json()[\"_links\"][\"results\"]\n",
" for item in results: \n",
" res = requests.get(item[\"location\"], stream=True)\n",
" name = item[\"name\"].split(\"/\")[-1]\n",
"\n",
" print(\"Downloading '{name}' just started...\")\n",
" with open(file_path + name, 'wb') as f:\n",
" for chunk in res.iter_content(chunk_size=1048576): # 1MB (1024 * 1024)\n",
" # Write the data to the file in chunks\n",
" f.write(chunk) \n",
" print(f\"File '{name}' downloaded and saved at '{file_path}'\")\n",
" \n",
" except requests.RequestException as e:\n",
" return logging.error(f\"An error occurred: {e}\")\n",
"\n",
"\n",
"download_order(order['_links']['_self'], API_KEY)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down