Last active
June 17, 2020 15:24
-
-
Save chris-lovejoy/ec20fbac3a9387c2d2f7070f150184f6 to your computer and use it in GitHub Desktop.
ranking by view-to-subscriber ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
def find_title(item): | |
title = item['snippet']['title'] | |
return title | |
def find_viewcount(item, youtube_api): | |
video_id = item['id']['videoId'] | |
video_statistics = youtube_api.videos().list(id=video_id, part='statistics').execute() | |
viewcount = int(video_statistics['items'][0]['statistics']['viewCount']) | |
return viewcount | |
def find_num_subscribers(channel_id, youtube_api): | |
subs_search = youtube_api.channels().list(id=channel_id, part='statistics').execute() | |
num_subscribers = int(subs_search['items'][0]['statistics']['subscriberCount']) | |
return num_subscribers | |
def custom_score(viewcount, subs): | |
ratio = viewcount / subs | |
score = viewcount * ratio | |
return score | |
df = pd.DataFrame(columns=('Title', 'Custom Score') | |
i = 1 | |
for item in results['items']: | |
title = find_title(item) | |
viewcount = find_viewcount(item, youtube_api) | |
num_subs = find_num_subscribers(channel_id, youtube_api) | |
score = custom_score(viewcount, num_subs) | |
df.loc[i] = [title, score] | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment