{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5e79187",
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.utils.podcasts import load_podcast_db\n",
    "\n",
    "podcast_df = load_podcast_db(\n",
    "    \"/mnt/data-ssd-1/data/podcasts/meta/podcastindex_feeds.db\", english_only=True, anchor_only=True,\n",
    ")\n",
    "phl_podcast_df = podcast_df[podcast_df[\"language\"] == \"en-ph\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6e70c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "country_df = pd.read_csv(\"podcasts/country_podcasts.csv\")\n",
    "phl_country_df = country_df[country_df[\"country\"] == \"Philippines\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3dfc1ce7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1159\n",
      "691\n",
      "161\n"
     ]
    }
   ],
   "source": [
    "ids_1 = set(phl_podcast_df[\"id\"])\n",
    "ids_2 = set(phl_country_df[\"id\"])\n",
    "print(len(ids_1))\n",
    "print(len(ids_2))\n",
    "print(len(ids_1 & ids_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "608044fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "998 podcasts\n"
     ]
    }
   ],
   "source": [
    "phl_df = phl_podcast_df[~phl_podcast_df[\"id\"].isin(ids_2)].reset_index(drop=True)\n",
    "print(phl_df.shape[0], \"podcasts\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "759df7f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: listen to a few and assemble as usual with ones \n",
    "#   with more than 3 episodes where the mostr recent one is reachable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "948449b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id                                                                   9732\n",
       "url                               https://anchor.fm/s/205aaa8/podcast/rss\n",
       "title                                                 Saultower's Podcast\n",
       "last_update                                           2021-06-03 18:47:04\n",
       "link                                                    www.saultower.com\n",
       "last_http_status                                                      200\n",
       "dead                                                                    0\n",
       "content_type                           application/rss+xml; charset=utf-8\n",
       "itunes_id                                                      1331515580\n",
       "original_url                      https://anchor.fm/s/205aaa8/podcast/rss\n",
       "itunes_author                                                   saultower\n",
       "itunes_owner_name                                               saultower\n",
       "explicit                                                                0\n",
       "image_url               https://d3t3ozftmdmh3i.cloudfront.net/producti...\n",
       "itunes_type                                                      episodic\n",
       "generator                                                 Anchor Podcasts\n",
       "newest_item_pubdate                                   2018-01-05 00:15:02\n",
       "language                                                            en-ph\n",
       "oldest_item_pubdate                                   2018-01-05 00:15:02\n",
       "episode_count                                                           1\n",
       "popularity_score                                                        0\n",
       "priority                                                               -1\n",
       "created_on                                            2020-08-06 22:25:29\n",
       "update_frequency                                                        9\n",
       "chash                                    a0ef1e7669c3277361e49d69eed90924\n",
       "host                                                            anchor.fm\n",
       "newest_enclosure_url    https://anchor.fm/s/205aaa8/podcast/play/90537...\n",
       "podcast_guid                         193ba456-c275-5411-bf63-73ec473ab303\n",
       "Name: 0, dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phl_df.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "733149e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pdb\n",
    "from suno_utils.utils.podcasts import load_rss_feed\n",
    "\n",
    "d = \"/mnt/data-ssd-1/data/podcasts/bulk_rss_feeds/raw_files/\"\n",
    "all_emails = []\n",
    "for _, row in phl_df.iterrows():\n",
    "    rss_filepath = d + f\"{row['id']}.feed\"\n",
    "    try:\n",
    "        rss_feed = load_rss_feed(rss_filepath)\n",
    "    except:\n",
    "        all_emails.append(None)\n",
    "        continue\n",
    "    email_1 = rss_feed[\"feed\"][\"publisher_detail\"][\"email\"]\n",
    "    email_2 = [e[\"email\"] for e in rss_feed[\"feed\"][\"authors\"] if \"email\" in e]\n",
    "    emails = list(set([email_1] + email_2))\n",
    "    if len(emails) == 0:\n",
    "        pdb.set_trace()\n",
    "        all_emails.append(None)\n",
    "        continue\n",
    "    elif len(emails) == 1:\n",
    "        all_emails.append(emails[0])\n",
    "        continue\n",
    "    # check if we have a non-anchor email\n",
    "    nl = [e for e in emails if \"anchor.fm\" not in e]\n",
    "    if len(nl) > 0:\n",
    "        email = nl[0]\n",
    "    else:\n",
    "        email = emails[0]\n",
    "    all_emails.append(email)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fb4e77b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "phl_df[\"author_email\"] = all_emails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5afce960",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(337, 29)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = phl_df.dropna(subset=[\"author_email\"])\n",
    "df = df[~df[\"author_email\"].str.contains(\"anchor.fm\")]\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b1cbd613",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = phl_df.dropna(subset=[\"author_email\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "514c3ec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get author name\n",
    "# get emails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5435e756",
   "metadata": {},
   "outputs": [],
   "source": [
    "rss_filepath = d + f\"24830.feed\"\n",
    "rss_feed = load_rss_feed(rss_filepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2676c91a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'RainbowTalks.PH'},\n",
       " {'name': 'RainbowTalks.PH', 'email': 'podcasts15+f50990c@anchor.fm'}]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rss_feed[\"feed\"][\"authors\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ff057fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'RainbowTalks.PH', 'email': 'podcasts15+f50990c@anchor.fm'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rss_feed[\"feed\"][\"publisher_detail\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "7ed67b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id                                                                  39000\n",
       "url                              https://anchor.fm/s/2a623138/podcast/rss\n",
       "title                            Growth Conversations with Chris Dao-anis\n",
       "last_update                                           2022-03-28 23:52:01\n",
       "link                                              http://chrisdaoanis.com\n",
       "last_http_status                                                      200\n",
       "dead                                                                    0\n",
       "content_type                           application/rss+xml; charset=utf-8\n",
       "itunes_id                                                      1522410948\n",
       "original_url                     https://anchor.fm/s/2a623138/podcast/rss\n",
       "itunes_author                                              Chris Dao-anis\n",
       "itunes_owner_name                                          Chris Dao-anis\n",
       "explicit                                                                0\n",
       "image_url               https://d3t3ozftmdmh3i.cloudfront.net/producti...\n",
       "itunes_type                                                      episodic\n",
       "generator                                                 Anchor Podcasts\n",
       "newest_item_pubdate                                   2021-09-21 10:41:20\n",
       "language                                                            en-ph\n",
       "oldest_item_pubdate                                   2020-07-07 04:11:37\n",
       "episode_count                                                          15\n",
       "popularity_score                                                        0\n",
       "priority                                                               -1\n",
       "created_on                                            2020-08-06 22:28:21\n",
       "update_frequency                                                        7\n",
       "chash                                    f6b67ae4d7785cf974045091e619f73c\n",
       "host                                                            anchor.fm\n",
       "newest_enclosure_url    https://anchor.fm/s/2a623138/podcast/play/3992...\n",
       "podcast_guid                         6287609d-3ccb-527c-b7b0-3e2b76ed246e\n",
       "author_email                                podcasts60+2a623138@anchor.fm\n",
       "Name: 11, dtype: object"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22634ba6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ca375cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b280517",
   "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
}
