# lets import libraries
from googleapiclient.discovery import build
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Function to get Celin Deon Youtube channel statistics
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Celin_Deon_channel_id = ['UC_yGU4qz9zAjEWLQxCg9NZQ' # Celin Deon
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Celin_Deon_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Celin_Deon_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Celin_Deon_channel_id )
[{'Channel_name': 'Celine Dion', 'Subscribers': '8080000', 'Views': '4863781960', 'Total_videos': '108'}]
channel_statistics = get_channel_stats(youtube,Celin_Deon_channel_id )
Celin_Deon_channel_data =pd.DataFrame(channel_statistics)
Celin_Deon_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Celine Dion | 8080000 | 4863781960 | 108 |
# Function to get Kirk Franklin Youtube channel statistics
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Whitney_Houston_channel_id = ['UC7fzrpTArAqDHuB3Hbmd_CQ' # Whitney Houston
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Whitney_Houston_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Whitney_Houston_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Whitney_Houston_channel_id )
[{'Channel_name': 'Whitney Houston', 'Subscribers': '8610000', 'Views': '6407064063', 'Total_videos': '131'}]
channel_statistics = get_channel_stats(youtube,Whitney_Houston_channel_id )
Whitney_Houston_channel_data =pd.DataFrame(channel_statistics)
Whitney_Houston_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Whitney Houston | 8610000 | 6407064063 | 131 |
# Function to get Mariah Carey Youtube channel statistics
api_key = 'AIzaSyB24Kop04L1GlTgRCm1XtQ4KB2a4gaBOwA'
Mariah_Carey_channel_id = ['UCurpiDXSkcUbgdMwHNZkrCg' # Mariah Carey
]
youtube = build('youtube','v3',developerKey = api_key)
from googleapiclient.errors import HttpError
def get_channel_stats(youtube, Mariah_Carey_channel_id):
all_data = []
try:
request = youtube.channels().list(
part='snippet,contentDetails,statistics',
id=','.join(Mariah_Carey_channel_id)
)
response = request.execute()
for item in response.get('items', []):
snippet = item.get('snippet', {})
statistics = item.get('statistics', {})
data = {
'Channel_name': snippet.get('title', ''),
'Subscribers': statistics.get('subscriberCount', ''),
'Views': statistics.get('viewCount', ''),
'Total_videos': statistics.get('videoCount', '')
}
all_data.append(data)
except HttpError as e:
print(f"HTTP error occurred: {e}")
print(f"Request URL: {e.resp.request.url}")
print(f"Request body: {e.resp.request.body}")
return all_data
get_channel_stats(youtube,Mariah_Carey_channel_id)
[{'Channel_name': 'Mariah Carey', 'Subscribers': '10600000', 'Views': '8471138420', 'Total_videos': '136'}]
channel_statistics = get_channel_stats(youtube,Mariah_Carey_channel_id )
Mariah_Carey_channel_data =pd.DataFrame(channel_statistics)
Mariah_Carey_channel_data
Channel_name | Subscribers | Views | Total_videos | |
---|---|---|---|---|
0 | Mariah Carey | 10600000 | 8471138420 | 136 |
# Combine DataFrames using concat
combined_rnb_artist_channels_df = pd.concat([
Celin_Deon_channel_data,
Whitney_Houston_channel_data ,
Mariah_Carey_channel_data,
], ignore_index=True)
# Display the result
print(combined_rnb_artist_channels_df)
Channel_name Subscribers Views Total_videos 0 Celine Dion 8080000 4863781960 108 1 Whitney Houston 8610000 6407064063 131 2 Mariah Carey 10600000 8471138420 136
# lets change the datatypes to perform visualisations
combined_rnb_artist_channels_df['Subscribers'] = pd.to_numeric(combined_rnb_artist_channels_df['Subscribers'])
combined_rnb_artist_channels_df['Views'] = pd.to_numeric(combined_rnb_artist_channels_df['Views'])
combined_rnb_artist_channels_df['Total_videos'] = pd.to_numeric(combined_rnb_artist_channels_df['Total_videos'])
# letsconirm if the datatypes has changed
combined_rnb_artist_channels_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Channel_name 3 non-null object 1 Subscribers 3 non-null int64 2 Views 3 non-null int64 3 Total_videos 3 non-null int64 dtypes: int64(3), object(1) memory usage: 228.0+ bytes
import matplotlib.pyplot as plt
# Sort the DataFrame by 'Subscribers' in descending order
combined_rnb_artist_channels_df_subscribers_sorted = combined_rnb_artist_channels_df.sort_values(by='Subscribers', ascending=False)
# Plotting Subscribers in descending order
fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(10, 15))
axes[0].bar(combined_rnb_artist_channels_df_subscribers_sorted['Channel_name'],combined_rnb_artist_channels_df_subscribers_sorted['Subscribers'], color='blue')
axes[0].set_title('Subscribers', fontsize=20) # Adjust title font size as needed
# Sort the DataFrame by 'Views' in descending order
combined_rnb_artist_channels_df_views_sorted = combined_rnb_artist_channels_df.sort_values(by='Views', ascending=False)
# Plotting Views in descending order
axes[1].bar(combined_rnb_artist_channels_df_views_sorted['Channel_name'], combined_rnb_artist_channels_df_views_sorted['Views'], color='green')
axes[1].set_title('Views', fontsize=20) # Adjust title font size as needed
# Sort the DataFrame by 'Total_videos' in descending order
combined_rnb_artist_channels_df_total_videos_sorted = combined_rnb_artist_channels_df.sort_values(by='Total_videos', ascending=False)
# Plotting Total Videos in descending order
axes[2].bar(combined_rnb_artist_channels_df_total_videos_sorted['Channel_name'], combined_rnb_artist_channels_df_total_videos_sorted['Total_videos'], color='orange')
axes[2].set_title('Total Videos', fontsize=20) # Adjust title font size as needed
# Adjust layout for better visibility
plt.tight_layout()
# Show the plot
plt.show()