A lightweight Python library to simplify querying YouTube stuff (videos, comments, channels, playlists).
Features:
- Search videos, channels, playlists with additional filters.
- Search video comments (including replies) by keywords.
- Pull channel latest comments.
- Pull channel upload videos infos.
- Query YouTube Analytics Data
- Pre-built reports
pip install git+https://github.com/DataSolveProblems/YTInspector.git
Requirements: Python 3.6+.
-
Create a project, set up OAuth2 protocol, and download client file.
-
Enable YouTube Data and YouTube Analytics APIs.
-
Start using YTInspector:
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
# initialize YouTube service connection
yt.initService()
# perform video search
video_results = yt.searchVideos('tesla')
from ytinspector import convert_to_RFC_datetime
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
video_results = yt.searchVideos(
'tesla',
region_code='us',
published_after=convert_to_RFC_datetime(2022, 1, 1),
published_before=convert_to_RFC_datetime(2022, 1, 31)
)
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
categories = yt.retrieveVideoCategoriesList()
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
channel_list = yt.searchChannels('electrical cars')
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
playlist_list = yt.searchPlaylists('electrical cars')
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
channel_videos = yt.retrieveChannelVideos('UCVhDYDVo3AqyMIKtMLSrcEg')
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
video_comments = yt.retrieveVideoComments('bcNUbQ2CBHM')
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
video_comments = yt.retrieveVideoComments('bcNUbQ2CBHM', include_replies=True)
from ytinspector.youtube import YouTube
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
latest_comments = yt.retrieveChannelRelatedComments('UCVhDYDVo3AqyMIKtMLSrcEg')
from ytinspector import locate_channel_id
video_id = 't49Q6qhMfk8'
channel_id = locate_channel_id(video_id)
print(channel_id)
CLIENT_FILE = 'client-secret.json'
yt = YouTube(CLIENT_FILE)
yt.initService()
yt_analytics = YTAnalytics(CLIENT_FILE)
yt_analytics.initService()
Using the query method, you should be able to run any report that the API supports
below query is equalvant to top 200 playlists by views
response = yt_analytics.query(
start_date='2017-01-01',
end_date='2022-05-31',
metric_list=['playlistStarts', 'estimatedMinutesWatched', 'views', 'viewsPerPlaylistStart'],
dimension_list=['playlist'],
sort_by='-views',
max_result=200,
filters='isCurated==1'
)
df = pd.DataFrame(response[1], columns=response[0])
print(df)
response = yt_analytics.channelSummary('2022-01-01', '2022-05-31')
df = pd.DataFrame(response[1], columns=response[0])
print(df)
response = yt_analytics.summaryByCountry('2022-01-01', '2022-05-31', country_code='au', is_yt_partner=True)
df = pd.DataFrame(response[1], columns=response[0])
print(df)
response = yt_analytics.top200Videos('2022-01-01', '2022-05-31', sortby_field='subscribersGained', is_yt_partner=True)
df = pd.DataFrame(response[1], columns=response[0])
print(df)
response = yt_analytics.playlistSummary('2022-01-01', '2022-05-31', playlist_id=None)
df = pd.DataFrame(response[1], columns=response[0])
print(df)
response = yt_analytics.top200Playlists('2022-01-01', '2022-05-31', sortby_field='estimatedMinutesWatched')
df = pd.DataFrame(response[1], columns=response[0])
print(df)