{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "420e69f4-87b4-483b-9a0e-959249c4d493",
   "metadata": {},
   "source": [
    "# Retrieving the RESISLANDS Training Package for week 1 and 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4e7d059",
   "metadata": {},
   "source": [
    "## Download and extract the RESISLANDS Week1 and Week2 Training package\n",
    "\n",
    "This notebook downloads `WEkEO_GI_course.zip` from the ITC file-transfer server, creates a folder named `Resislands_W1_2` in the current working directory, extracts the ZIP content into that folder, and deletes the downloaded ZIP file after successful extraction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dbecd57e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import urllib.request\n",
    "import zipfile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ccffcec",
   "metadata": {},
   "source": [
    "### 1. Define the download and destination paths\n",
    "\n",
    "The notebook uses the directory from which the Jupyter notebook is running as its working directory. Change `work_dir` if you want to use a different location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4be14e4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Working directory: /home/jovyan\n",
      "ZIP file: /home/jovyan/wekeo_GI_private_start_backup.zip\n",
      "Extraction folder: /home/jovyan/Resislands\n"
     ]
    }
   ],
   "source": [
    "work_dir = os.getcwd()\n",
    "\n",
    "base_url = \"https://filetransfer.itc.nl/pub/52n/Resislands/\"\n",
    "zip_filename = \"WEkEO_GI_course.zip\"\n",
    "download_url = base_url + zip_filename\n",
    "\n",
    "zip_path = os.path.join(work_dir, zip_filename)\n",
    "target_folder = os.path.join(work_dir, \"Resislands_W1_2\")\n",
    "\n",
    "print(\"Working directory:\", work_dir)\n",
    "print(\"ZIP file:\", zip_path)\n",
    "print(\"Extraction folder:\", target_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "052a8cab",
   "metadata": {},
   "source": [
    "### 2. Create the destination folder and download the ZIP file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c47207f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading the backup ZIP file...\n",
      "Download completed: /home/jovyan/wekeo_GI_private_start_backup.zip\n",
      "Downloaded size: 41.87 MB\n"
     ]
    }
   ],
   "source": [
    "os.makedirs(target_folder, exist_ok=True)\n",
    "\n",
    "print(\"Downloading the backup ZIP file...\")\n",
    "urllib.request.urlretrieve(download_url, zip_path)\n",
    "print(\"Download completed:\", zip_path)\n",
    "print(\"Downloaded size: {:.2f} MB\".format(os.path.getsize(zip_path) / (1024 * 1024)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f915b10e",
   "metadata": {},
   "source": [
    "### 3. Extract the ZIP file into `Resislands`\n",
    "\n",
    "The extraction step checks archive paths before writing files, to prevent files from being extracted outside the destination folder. If the `Resislands` folder already exists, extracted files are added to it and files with the same names may be overwritten."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8bbb67f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extraction completed.\n"
     ]
    }
   ],
   "source": [
    "with zipfile.ZipFile(zip_path, \"r\") as zip_ref:\n",
    "    target_real_path = os.path.realpath(target_folder)\n",
    "\n",
    "    for member in zip_ref.infolist():\n",
    "        member_path = os.path.realpath(os.path.join(target_folder, member.filename))\n",
    "        if os.path.commonpath([target_real_path, member_path]) != target_real_path:\n",
    "            raise ValueError(\"Unsafe path in ZIP archive: {}\".format(member.filename))\n",
    "\n",
    "    zip_ref.extractall(target_folder)\n",
    "\n",
    "print(\"Extraction completed.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3522ef91",
   "metadata": {},
   "source": [
    "### 4. Delete the downloaded ZIP file\n",
    "\n",
    "The ZIP file is deleted only after extraction has completed successfully. If downloading or extraction fails, the ZIP file is left in place for troubleshooting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7646d0ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZIP file was not found; nothing to delete.\n",
      "\n",
      "RESISLANDS content is available: /home/jovyan/Resislands\n"
     ]
    }
   ],
   "source": [
    "if os.path.exists(zip_path):\n",
    "    os.remove(zip_path)\n",
    "    print(\"Deleted ZIP file:\", zip_path)\n",
    "else:\n",
    "    print(\"ZIP file was not found; nothing to delete.\")\n",
    "\n",
    "print(\"\\nRESISLANDS content is available:\", target_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25c0b7c6-376d-46b6-9321-423c1e41b372",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
