endremborza/aswan

View on GitHub
notebooks/imdb-case.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "twelve-interval",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "import aswan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "published-explanation",
   "metadata": {},
   "outputs": [],
   "source": [
    "project = aswan.Project(\"imdb-example\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "optimum-preservation",
   "metadata": {},
   "outputs": [],
   "source": [
    "@project.register_handler\n",
    "class CelebHandler(aswan.RequestSoupHandler):\n",
    "    url_root = \"https://www.imdb.com\"\n",
    "\n",
    "    def parse(self, soup: BeautifulSoup):\n",
    "        return {\n",
    "            \"name\": soup.find(\"h1\").find(\"span\").text.strip(),\n",
    "            \"dob\": soup.find(\"div\", id=\"name-born-info\").find(\"time\")[\"datetime\"],\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "front-shower",
   "metadata": {},
   "outputs": [],
   "source": [
    "@project.register_handler\n",
    "class MovieHandler(aswan.RequestSoupHandler):\n",
    "    url_root = \"https://www.imdb.com\"\n",
    "\n",
    "    def parse(self, soup: BeautifulSoup):\n",
    "\n",
    "        for cast in soup.find(\"table\", class_=\"cast_list\").find_all(\n",
    "            \"td\", class_=\"primary_photo\"\n",
    "        )[:3]:\n",
    "            self.register_links_to_handler([cast.find(\"a\")[\"href\"]], CelebHandler)\n",
    "\n",
    "        ref_section = soup.find(\"section\", class_=\"titlereference-section-overview\")\n",
    "        summary = None\n",
    "        if ref_section is not None:\n",
    "            summary = getattr(ref_section.find(\"div\"), \"text\", \"\").strip()\n",
    "        return {\n",
    "            \"title\": soup.find(\"title\")\n",
    "            .text.replace(\" - Reference View - IMDb\", \"\")\n",
    "            .strip(),\n",
    "            \"summary\": summary,\n",
    "            \"year\": int(\n",
    "                soup.find(\"span\", class_=\"titlereference-title-year\").find(\"a\").text\n",
    "            ),\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "divided-promise",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-06 16:47.56 [info     ] running function setup         batch=prep\n",
      "2022-10-06 16:47.56 [info     ] function setup returned None   batch=prep\n",
      "2022-10-06 16:47.56 [info     ] running function _initiate_status batch=prep\n",
      "2022-10-06 16:47.56 [info     ] function _initiate_status returned None batch=prep\n",
      "2022-10-06 16:47.56 [info     ] running function _create_scheduler batch=prep\n",
      "2022-10-06 16:47.56 [info     ] function _create_scheduler returned None batch=prep\n",
      "2022-10-06 16:48.12 [info     ] running function join          batch=cleanup\n",
      "2022-10-06 16:48.12 [info     ] function join returned None    batch=cleanup\n"
     ]
    }
   ],
   "source": [
    "project.run(\n",
    "    urls_to_register={\n",
    "        MovieHandler: [\n",
    "            \"https://www.imdb.com/title/tt1045772/reference\",\n",
    "            \"https://www.imdb.com/title/tt2543164/reference\",\n",
    "        ],\n",
    "        CelebHandler: [\"https://www.imdb.com/name/nm0000190\"],\n",
    "    },\n",
    "    force_sync=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dea76c82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>summary</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>I Love You Phillip Morris (2009)</td>\n",
       "      <td>A cop turns con man once he comes out of the c...</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arrival (2016)</td>\n",
       "      <td>A linguist works with the military to communic...</td>\n",
       "      <td>2016</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              title  \\\n",
       "0  I Love You Phillip Morris (2009)   \n",
       "1                    Arrival (2016)   \n",
       "\n",
       "                                             summary  year  \n",
       "0  A cop turns con man once he comes out of the c...  2009  \n",
       "1  A linguist works with the military to communic...  2016  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame([pcev.content for pcev in project.depot.get_handler_events(MovieHandler)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "57d58380",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>dob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ewan McGregor</td>\n",
       "      <td>1971-3-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Jeremy Renner</td>\n",
       "      <td>1971-1-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Leslie Mann</td>\n",
       "      <td>1972-3-26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Forest Whitaker</td>\n",
       "      <td>1961-7-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Amy Adams</td>\n",
       "      <td>1974-8-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Jim Carrey</td>\n",
       "      <td>1962-1-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Matthew McConaughey</td>\n",
       "      <td>1969-11-4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  name        dob\n",
       "0        Ewan McGregor  1971-3-31\n",
       "1        Jeremy Renner   1971-1-7\n",
       "2          Leslie Mann  1972-3-26\n",
       "3      Forest Whitaker  1961-7-15\n",
       "4            Amy Adams  1974-8-20\n",
       "5           Jim Carrey  1962-1-17\n",
       "6  Matthew McConaughey  1969-11-4"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame([pcev.content for pcev in project.depot.get_handler_events(CelebHandler)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4ae4da03",
   "metadata": {},
   "outputs": [],
   "source": [
    "project.cleanup_current_run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "beautiful-lawsuit",
   "metadata": {},
   "outputs": [],
   "source": [
    "project.depot.purge()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('data-project')",
   "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.10.6"
  },
  "vscode": {
   "interpreter": {
    "hash": "c34365d60e34b30dc48102674d627ffeb37891f3ae5b0d543a727d8b1b3f5762"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}