class TestVideo(unittest.TestCase): def __init__(): self.key = os.environ.get('YT_KEY') self.yt = YoutubeDataApi(self.key) self.channel_id = 'UC3XTzVzaHQEd30rQbuvCtTQ' self.channel_title = 'LastWeekTonight' #written by Megan Brown on 11/30/2018 def test_channel_id(self): resp = self.yt.get_channel_id_from_user(channel_title) self.assertEqual(resp, self.channel_id) def test_get_channel_metadata_channel_id(self): resp = self.yt.get_channel_metadata(self.channel_id) self.assertEqual(resp['channel_id'], self.channel_id) self.assertEqual(resp['channel_title'])
import pandas as pd import time from youtube_api import YoutubeDataApi from utils import API_KEY from utils import video_playlist_dict2 from utils import tags from utils import channelIds youtube = YoutubeDataApi(key=API_KEY) for channelId, tag in zip(channelIds, tags): print(tag) meta = youtube.get_channel_metadata(channelId) meta['account_creation_date'] = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(meta['account_creation_date'])) meta_df = pd.DataFrame([meta]) meta_df.to_csv('data/{}/metadata.csv'.format(tag), index=False) playlist_id_uploads = meta['playlist_id_uploads'] try: videos = youtube.get_videos_from_playlist_id(playlist_id_uploads) videos = [video_playlist_dict2(video) for video in videos] # playlists = youtube.get_playlists(channelId) # # print(len(playlists)) # playlists = [playlist_dict(playlist) for playlist in playlists] # all_videos = [] # for playlist in playlists: # videos = youtube.get_videos_from_playlist_id(playlist['playlist_id']) # # playlists_df = pd.DataFrame(playlists) # # playlists_df.to_csv('{}_channel_playlists.csv'.format(channelId), index=False) # videos = [video_playlist_dict(video, playlist['playlist_id']) for video in videos] # # print(len(videos))