{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "457e7807",
   "metadata": {},
   "source": [
    "# Get data from owncloud"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc9ed3f8",
   "metadata": {},
   "source": [
    "Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install pyocclient\n",
    "import os\n",
    "import time\n",
    "import yaml\n",
    "import owncloud\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68e34abc",
   "metadata": {},
   "source": [
    "Download and save"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = time.time()\n",
    "\n",
    "# TODO: User input: directory where downloaded files should be saved\n",
    "save_dir = r\"./downloads\"\n",
    "\n",
    "\n",
    "os.makedirs(save_dir, exist_ok=True)\n",
    "\n",
    "# Load credentials\n",
    "with open(\"login.yaml\", \"r\") as f:\n",
    "    cfg = yaml.safe_load(f)\n",
    "\n",
    "url = cfg[0][\"url\"]\n",
    "password = cfg[1][\"password\"]\n",
    "\n",
    "# Connect to OwnCloud public link\n",
    "oc = owncloud.Client.from_public_link(url, folder_password=password)\n",
    "\n",
    "# List all available files in the shared folder\n",
    "remote_files = oc.list(\".\")\n",
    "\n",
    "# Keep only HDF5 files and sort them by name\n",
    "hdf5_files = sorted([f.get_name() for f in remote_files if f.get_name().endswith(\".hdf5\")])\n",
    "\n",
    "print(f\"Found {len(hdf5_files)} .hdf5 files in OwnCloud\")\n",
    "\n",
    "if not hdf5_files:\n",
    "    print(\"No .hdf5 files found.\")\n",
    "else:\n",
    "    for i, remote_name in enumerate(hdf5_files):\n",
    "        local_name = f\"tmp_{i:04d}.h5\"\n",
    "        local_path = os.path.join(save_dir, local_name)\n",
    "\n",
    "        try:\n",
    "            oc.get_file(remote_name, local_path)\n",
    "            print(f\"Downloaded: {remote_name} -> {local_path}\")\n",
    "        except Exception as e:\n",
    "            print(f\"Failed to download {remote_name}: {e}\")\n",
    "\n",
    "end = time.time()\n",
    "print(f\"Finished in {end - start:.2f} seconds\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
