{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parsing votes\n",
    "> Downloading & parsing votes from [bundestag.de](https://www.bundestag.de/parlament/plenum/abstimmung/liste)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to use\n",
    "\n",
    "Run the notebook top to bottom. Given parameters control the download and transformation components.\n",
    "\n",
    "### CLI equivalent\n",
    "\n",
    "Download\n",
    "\n",
    "    uv run bundestag download bundestag-sheets --do-create-xlsx-uris-json\n",
    "\n",
    "transformation\n",
    "\n",
    "    uv run bundestag transform bundestag-sheet --sheet-source=json_file\n",
    "\n",
    "### Skip processing\n",
    "\n",
    "You can run this notebook to re-do some data processing. But know you can also skip this by running\n",
    "\n",
    "    uv run bundestag download huggingface\n",
    "\n",
    "instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bundestag.data.download.bundestag_sheets import (\n",
    "    run as download_bundestag_sheets,\n",
    "    Source,\n",
    ")\n",
    "from bundestag.data.transform.bundestag_sheets import run as transform_bundestag_sheets\n",
    "from bundestag.data.utils import RE_SHEET\n",
    "import pandas as pd\n",
    "from bundestag.paths import get_paths\n",
    "from bundestag.fine_logging import setup_logging\n",
    "import logging\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = logging.getLogger(__name__)\n",
    "setup_logging(logging.INFO)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paths = get_paths(\"../data\")\n",
    "paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dry = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Collecting URIs for `.xlsx`/`.xls` documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nmax = 999\n",
    "pattern = RE_SHEET\n",
    "assume_yes = True\n",
    "do_create_xlsx_uris_json = False\n",
    "max_pages = 10\n",
    "json_filename = \"xlsx_uris.json\"\n",
    "source = Source.json_file\n",
    "\n",
    "html_dir = paths.raw_bundestag_html\n",
    "json_path = html_dir.parent / json_filename\n",
    "sheet_dir = paths.raw_bundestag_sheets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "download_bundestag_sheets(\n",
    "    html_dir=html_dir,\n",
    "    sheet_dir=sheet_dir,\n",
    "    nmax=nmax,\n",
    "    dry=dry,\n",
    "    pattern=pattern,\n",
    "    assume_yes=assume_yes,\n",
    "    source=source,\n",
    "    do_create_xlsx_uris_json=do_create_xlsx_uris_json,\n",
    "    max_pages=max_pages,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with json_path.open(\"r\") as f:\n",
    "    xlsx_uris = json.load(f)\n",
    "\n",
    "xlsx_uris"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transforming sheet files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocessed_path = paths.preprocessed_bundestag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transform_bundestag_sheets(\n",
    "    html_dir=html_dir,\n",
    "    sheet_dir=sheet_dir,\n",
    "    preprocessed_path=preprocessed_path,\n",
    "    dry=dry,\n",
    "    source=source,\n",
    "    json_filename=json_filename,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_parquet_path = preprocessed_path / \"bundestag.de_votes.parquet\"\n",
    "results_parquet_path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading sheets DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_parquet(results_parquet_path)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.12.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
