Commit 12af3164 authored by Anurag Kumar's avatar Anurag Kumar

First commit

parent 5860c596
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Youtube_Api_data.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "PBHEstklEjUq"
},
"source": [
"import requests\n",
"import pandas as pd\n",
"import time"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hsSIBQbJFFzA"
},
"source": [
"API_KEY = \"AIzaSyCq9fWteOyROajADP4Ww2xSMa2p4TFAyUI\" # this is fake api key\n",
"Channel_Id = \"UCeVHRraqf7I68yt9dfsHx-l19T\" # this is fake channel id"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 269
},
"id": "bZGkPXgENd8h",
"outputId": "7259eac6-d1d8-4b25-9956-8c9037af58d4"
},
"source": [
"def getVideo_details(video_id):\n",
" url_stat = \"https://www.googleapis.com/youtube/v3/videos?id=\"+video_id+\"&part=statistics&key=\"+API_KEY\n",
" responce_stats = requests.get(url_stat).json()\n",
"\n",
" view_count = responce_stats['items'][0]['statistics']['viewCount']\n",
" comment_count = responce_stats['items'][0]['statistics']['commentCount']\n",
"\n",
" return view_count, comment_count\n",
"\n",
"\n",
"\n",
"\n",
"def getData(df): \n",
" pageToken = \"\"\n",
" url = \"https://www.googleapis.com/youtube/v3/search?key=\"+API_KEY+\"&channelId=\"+Channel_Id+\"&part=snippet,id&order=date&maxResults=1000\"+pageToken\n",
" responce = requests.get(url).json()\n",
"\n",
" responce['items'] = responce['items'][0:-1]\n",
" for video in responce['items']:\n",
" video_id = video['id']['videoId']\n",
" video_title = str(video['snippet']['title']).replace(\"&\",\"\")\n",
" upload_date = str(video['snippet']['publishedAt']).split(\"T\")[0]\n",
" \n",
" \n",
" view_count, comment_count = getVideo_details(video_id)\n",
" df = df.append({'video_id':video_id,\n",
" 'video_title':video_title,\n",
" 'upload_date':upload_date,\n",
" 'view_count':view_count,\n",
" 'comment_count':comment_count}, ignore_index=True)\n",
" \n",
" return df\n",
"\n",
"\n",
"\n",
"df = pd.DataFrame(columns=['video_id', 'video_title','upload_date','view_count','comment_count'])\n",
"df = getData(df)\n",
"df"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"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>video_id</th>\n",
" <th>video_title</th>\n",
" <th>upload_date</th>\n",
" <th>view_count</th>\n",
" <th>comment_count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3XJ3NOoeLf4</td>\n",
" <td>Piranha Movie Explained in Hindi | Piranha 201...</td>\n",
" <td>2021-09-05</td>\n",
" <td>29</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>BTW3dne0f3c</td>\n",
" <td>Mortal Kombat (2021) Film Explained in Hindi/U...</td>\n",
" <td>2021-09-03</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>mJ31LiGkj1E</td>\n",
" <td>Man of Steel (2013) Film Explained in Hindi/Ur...</td>\n",
" <td>2021-09-03</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9wM_3tnLvnU</td>\n",
" <td>Life Like (2019) Film Explained in Hindi/Urdu ...</td>\n",
" <td>2021-09-03</td>\n",
" <td>5</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>sZkyTsZD2nM</td>\n",
" <td>Captain America The First Avenger Explained In...</td>\n",
" <td>2021-08-30</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>MRfjCVoQHds</td>\n",
" <td>Yummy (2019) Film Explained in Hindi/Urdu | Yu...</td>\n",
" <td>2021-08-29</td>\n",
" <td>4</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>hJuuymhiP14</td>\n",
" <td>The Cupid (2020) Film Explained in Hindi/Urdu ...</td>\n",
" <td>2021-08-26</td>\n",
" <td>4</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" video_id ... comment_count\n",
"0 3XJ3NOoeLf4 ... 0\n",
"1 BTW3dne0f3c ... 0\n",
"2 mJ31LiGkj1E ... 1\n",
"3 9wM_3tnLvnU ... 0\n",
"4 sZkyTsZD2nM ... 0\n",
"5 MRfjCVoQHds ... 0\n",
"6 hJuuymhiP14 ... 0\n",
"\n",
"[7 rows x 5 columns]"
]
},
"metadata": {},
"execution_count": 121
}
]
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment