{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "2d801693",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/georg/venvs/ml/lib/python3.8/site-packages/IPython/core/magics/pylab.py:159: UserWarning: pylab import has clobbered these variables: ['f']\n",
      "`%matplotlib` prevents importing * from pylab and numpy\n",
      "  warn(\"pylab import has clobbered these variables: %s\"  % clobbered +\n"
     ]
    }
   ],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "aee2f26a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "145312e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fm\n",
      "11261\n",
      "11261\n",
      "am\n",
      "4690\n",
      "4690\n",
      "tv\n",
      "3688\n",
      "3688\n"
     ]
    }
   ],
   "source": [
    "with open(\"tmp_data.json\") as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "parsed_data = {}\n",
    "for k, v in data.items():\n",
    "    print(k)\n",
    "    print(len(v))\n",
    "    print(len([True for kk, vv in v.items() if vv[\"status\"] == \"OK\"]))\n",
    "    parsed_data[k] = []\n",
    "    for kk, vv in v.items():\n",
    "        d = vv[\"results\"][\"facility\"]\n",
    "        if \"mainStudioContact\" not in d:\n",
    "            continue\n",
    "        parsed_data[k].append((\n",
    "            d[\"callSign\"], \n",
    "            d[\"communityCity\"], \n",
    "            d[\"communityState\"], \n",
    "            d[\"partyName\"], \n",
    "            d[\"mainStudioContact\"].get(\"contactWebsite\", \"\"),\n",
    "            d[\"mainStudioContact\"].get(\"contactName\", \"\"),\n",
    "            d[\"mainStudioContact\"].get(\"contactEmail\", \"\"),\n",
    "            d[\"mainStudioContact\"].get(\"contactPhone\", \"\"),\n",
    "        ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "6a23dd4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fm_df = pd.DataFrame(\n",
    "    parsed_data[\"fm\"], \n",
    "    columns=[\n",
    "        \"call_sign\", \"community_city\", \"community_state\", \"party_name\", \n",
    "        \"contact_website\", \"contact_name\", \"contact_email\", \"contact_phone\",\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "d8c614c0",
   "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>call_sign</th>\n",
       "      <th>community_city</th>\n",
       "      <th>community_state</th>\n",
       "      <th>party_name</th>\n",
       "      <th>contact_website</th>\n",
       "      <th>contact_name</th>\n",
       "      <th>contact_email</th>\n",
       "      <th>contact_phone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KCHA-FM</td>\n",
       "      <td>CHARLES CITY</td>\n",
       "      <td>IA</td>\n",
       "      <td>COLOFF MEDIA, LLC</td>\n",
       "      <td>https://kchanews.com</td>\n",
       "      <td>Chris Berg</td>\n",
       "      <td>chris@coloffmedia.com</td>\n",
       "      <td>(641) 228-1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>KFFF</td>\n",
       "      <td>BENNINGTON</td>\n",
       "      <td>NE</td>\n",
       "      <td>IHM LICENSES, LLC</td>\n",
       "      <td>wolfradio933.iheart.com</td>\n",
       "      <td>Becca Sautter</td>\n",
       "      <td>DisabilityAccessContact@iheartmedia.com</td>\n",
       "      <td>1(402)561-2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  call_sign community_city community_state         party_name  \\\n",
       "0   KCHA-FM   CHARLES CITY              IA  COLOFF MEDIA, LLC   \n",
       "1      KFFF     BENNINGTON              NE  IHM LICENSES, LLC   \n",
       "\n",
       "           contact_website   contact_name  \\\n",
       "0     https://kchanews.com     Chris Berg   \n",
       "1  wolfradio933.iheart.com  Becca Sautter   \n",
       "\n",
       "                             contact_email   contact_phone  \n",
       "0                    chris@coloffmedia.com  (641) 228-1000  \n",
       "1  DisabilityAccessContact@iheartmedia.com  1(402)561-2000  "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fm_df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "4749d4bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "fm_df.to_csv(\"fm_stations.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "418aba1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "am_df = pd.DataFrame(\n",
    "    parsed_data[\"am\"], \n",
    "    columns=[\n",
    "        \"call_sign\", \"community_city\", \"community_state\", \"party_name\", \n",
    "        \"contact_website\", \"contact_name\", \"contact_email\", \"contact_phone\",\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "de396e94",
   "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>call_sign</th>\n",
       "      <th>community_city</th>\n",
       "      <th>community_state</th>\n",
       "      <th>party_name</th>\n",
       "      <th>contact_website</th>\n",
       "      <th>contact_name</th>\n",
       "      <th>contact_email</th>\n",
       "      <th>contact_phone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>WEPM</td>\n",
       "      <td>MARTINSBURG</td>\n",
       "      <td>WV</td>\n",
       "      <td>WEST VIRGINIA RADIO CORPORATION OF THE ALLEGHE...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>WRAY</td>\n",
       "      <td>PRINCETON</td>\n",
       "      <td>IN</td>\n",
       "      <td>PRINCETON BROADCASTING CO., INC.</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>KBSR</td>\n",
       "      <td>LAUREL</td>\n",
       "      <td>MT</td>\n",
       "      <td>SUN MOUNTAIN, INC.</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>KPRO</td>\n",
       "      <td>MARSHALL</td>\n",
       "      <td>TX</td>\n",
       "      <td>RCA BROADCASTING LLC</td>\n",
       "      <td></td>\n",
       "      <td>Scottie Rice</td>\n",
       "      <td>kyxydj@yahoo.com</td>\n",
       "      <td>8582127968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KTKK</td>\n",
       "      <td>SANDY</td>\n",
       "      <td>UT</td>\n",
       "      <td>UNITED BROADCASTING COMPANY, INC</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  call_sign community_city community_state  \\\n",
       "0      WEPM    MARTINSBURG              WV   \n",
       "1      WRAY      PRINCETON              IN   \n",
       "2      KBSR         LAUREL              MT   \n",
       "3      KPRO       MARSHALL              TX   \n",
       "4      KTKK          SANDY              UT   \n",
       "\n",
       "                                          party_name contact_website  \\\n",
       "0  WEST VIRGINIA RADIO CORPORATION OF THE ALLEGHE...                   \n",
       "1                   PRINCETON BROADCASTING CO., INC.                   \n",
       "2                                 SUN MOUNTAIN, INC.                   \n",
       "3                               RCA BROADCASTING LLC                   \n",
       "4                   UNITED BROADCASTING COMPANY, INC                   \n",
       "\n",
       "   contact_name     contact_email contact_phone  \n",
       "0                                                \n",
       "1                                                \n",
       "2                                                \n",
       "3  Scottie Rice  kyxydj@yahoo.com    8582127968  \n",
       "4                                                "
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "am_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "78d4e05d",
   "metadata": {},
   "outputs": [],
   "source": [
    "am_df.to_csv(\"us_am_stations.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a62ceb8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "tv_df = pd.DataFrame(\n",
    "    parsed_data[\"tv\"], \n",
    "    columns=[\n",
    "        \"call_sign\", \"community_city\", \"community_state\", \"party_name\", \n",
    "        \"contact_website\", \"contact_name\", \"contact_email\", \"contact_phone\",\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "7a3d2324",
   "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>call_sign</th>\n",
       "      <th>community_city</th>\n",
       "      <th>community_state</th>\n",
       "      <th>party_name</th>\n",
       "      <th>contact_website</th>\n",
       "      <th>contact_name</th>\n",
       "      <th>contact_email</th>\n",
       "      <th>contact_phone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>WPHY-CD</td>\n",
       "      <td>TRENTON</td>\n",
       "      <td>NJ</td>\n",
       "      <td>RNN NATIONAL, LLC</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>gregzanoni@wphytv.com</td>\n",
       "      <td>6095872500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>K31KK-D</td>\n",
       "      <td>KINGSVILLE-ALICE</td>\n",
       "      <td>TX</td>\n",
       "      <td>SCRIPPS BROADCASTING HOLDINGS LLC</td>\n",
       "      <td>www.telemundocc.com</td>\n",
       "      <td>Phil Brooks</td>\n",
       "      <td>phil.brooks@kristv.com</td>\n",
       "      <td>361-886-6100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  call_sign    community_city community_state  \\\n",
       "0   WPHY-CD           TRENTON              NJ   \n",
       "1   K31KK-D  KINGSVILLE-ALICE              TX   \n",
       "\n",
       "                          party_name      contact_website contact_name  \\\n",
       "0                  RNN NATIONAL, LLC                                     \n",
       "1  SCRIPPS BROADCASTING HOLDINGS LLC  www.telemundocc.com  Phil Brooks   \n",
       "\n",
       "            contact_email contact_phone  \n",
       "0   gregzanoni@wphytv.com    6095872500  \n",
       "1  phil.brooks@kristv.com  361-886-6100  "
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tv_df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "dde698f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "tv_df.to_csv(\"us_tv_stations.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d2aba8d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87308fcc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9769d79",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
