{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b721926",
   "metadata": {},
   "source": [
    "# Open City Data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35c00849",
   "metadata": {},
   "source": [
    "[Socrata](https://dev.socrata.com/foundry/data.sfgov.org/vw6y-z8j6) provides an API for city open data. \n",
    "\n",
    "For a dataset such as [SF crime](https://data.sfgov.org/Public-Safety/Police-Department-Incident-Reports-Historical-2003/tmnf-yvry), to to the `API` tab on top right. \n",
    "\n",
    "That provides you with the `dataset identifier`.\n",
    "\n",
    "Use the dataset identifier to grab specific tables for a given city_id (`data.sfgov.org`) - \n",
    "\n",
    "E.g., `vw6y-z8j6` for [SF 311 data](https://dev.socrata.com/foundry/data.sfgov.org/vw6y-z8j6).\n",
    "\n",
    "E.g., `tmnf-yvry` for [SF Police data](https://dev.socrata.com/foundry/data.sfgov.org/tmnf-yvry)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c93cc247",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install sodapy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b3464a02",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import OpenCityDataLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "478c5255",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = \"vw6y-z8j6\"  # 311 data\n",
    "dataset = \"tmnf-yvry\"  # crime data\n",
    "loader = OpenCityDataLoader(city_id=\"data.sfgov.org\", dataset_id=dataset, limit=2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fa914fc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Requests made without an app_token will be subject to strict throttling limits.\n"
     ]
    }
   ],
   "source": [
    "docs = loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73a6def2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pdid': '4133422003074',\n",
       " 'incidntnum': '041334220',\n",
       " 'incident_code': '03074',\n",
       " 'category': 'ROBBERY',\n",
       " 'descript': 'ROBBERY, BODILY FORCE',\n",
       " 'dayofweek': 'Monday',\n",
       " 'date': '2004-11-22T00:00:00.000',\n",
       " 'time': '17:50',\n",
       " 'pddistrict': 'INGLESIDE',\n",
       " 'resolution': 'NONE',\n",
       " 'address': 'GENEVA AV / SANTOS ST',\n",
       " 'x': '-122.420084075249',\n",
       " 'y': '37.7083109744362',\n",
       " 'location': {'type': 'Point',\n",
       "  'coordinates': [-122.420084075249, 37.7083109744362]},\n",
       " ':@computed_region_26cr_cadq': '9',\n",
       " ':@computed_region_rxqg_mtj9': '8',\n",
       " ':@computed_region_bh8s_q3mv': '309'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eval(docs[0].page_content)"
   ]
  }
 ],
 "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
