Skip to content

Commit

Permalink
initial commit + add: function to get channel stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
faizanxmulla committed Jan 24, 2024
1 parent def12c3 commit c4b129c
Showing 1 changed file with 216 additions and 0 deletions.
216 changes: 216 additions & 0 deletions Youtube API + Data Analysis/yt.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **Importing Libraries**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"from googleapiclient.discovery import build"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **Setting up YouTube API.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"api_service_name = \"youtube\"\n",
"api_version = \"v3\"\n",
"\n",
"yt_api_key = os.environ[\"YT_API_KEY\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **I. Scraping Channel Statistics.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Getting Channel ID's."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_ids = [\n",
" \"UCX6OQ3DkcsbYNE6H8uQQuVA\",\n",
" \"UC59ZRYCHev_IqjUhremZ8Tg\",\n",
" \"UCvgfXK4nTYKudb0rFR6noLA\",\n",
" \"UCc0YbtMkRdhcqwhu3Oad-lw\",\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Building YouTube API Service."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"youtube = build(api_service_name, api_version, developerKey=yt_api_key) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Function to obtain channel statistics."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_channel_statistics(youtube, channel_ids):\n",
" data = []\n",
"\n",
" request = youtube.channels().list(\n",
" part=\"snippet,contentDetails,statistics\", id=\",\".join(channel_ids)\n",
" )\n",
"\n",
" response = request.execute()\n",
"\n",
" for i in range(len(response[\"items\"])):\n",
" info = dict(\n",
" channel_name=response[\"items\"][i][\"snippet\"][\"title\"],\n",
" subscribers=response[\"items\"][i][\"statistics\"][\"subscriberCount\"],\n",
" videos=response[\"items\"][i][\"statistics\"][\"videoCount\"],\n",
" views=response[\"items\"][i][\"statistics\"][\"viewCount\"],\n",
" # --------\n",
" playlist_id=response[\"items\"][i][\"contentDetails\"][\"relatedPlaylists\"]['uploads'],\n",
" )\n",
"\n",
" data.append(info)\n",
"\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_statistics = get_channel_statistics(youtube, channel_ids)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a dataframe. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_df = pd.DataFrame(channel_statistics)\n",
"channel_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Change datatype from object to integer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_df.dtypes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"exclude_cols = ['channel_name']\n",
"\n",
"cols_to_include = [col for col in channel_df.columns if col not in exclude_cols]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_df[cols_to_include] = channel_df[cols_to_include].applymap(pd.to_numeric, errors='coerce')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"channel_df.dtypes"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit c4b129c

Please sign in to comment.