def test_get_object_right_values(self): api = YoutubeApi() video1 = api.get_video('nVjsGKrE6E8') video2 = api.get_video('nVjsGKrE6E8') self.assertEqual(video1, video2)
def test_api_get_channel_videos(self): html_code = read_in_file('tests/htmls/channel_about_page.txt') html_code2 = read_in_file('tests/htmls/channel_videos_page.txt') channel = YoutubeApi(FakeFetcher(html_code, html_code2)).get_channel('LanaDelReyVEVO') signature = VideoSignature('JRWox-i6aAk', 'Lana Del Rey - Blue Jeans', 'LanaDelReyVEVO', '149473022', '4:21') self.assertTrue(signature in channel.get_uploaded_videos()) for video in channel.get_uploaded_videos(): self.assertIsInstance(video, VideoSignature)
def test_api_get_playlist(self): html_code = read_in_file('tests/htmls/playlist_sample_source.txt') playlist = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).get_playlist( 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') keys_playlist = ['videos', 'id', 'name', 'author', 'length', 'url', 'thumbnail'] self.assertTrue(all(x in playlist.as_dict() for x in keys_playlist)) keys_video = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views'] for video in playlist.as_dict()['videos']: self.assertTrue(all(x in video for x in keys_video))
def test_search_cache_right_values(self): api = YoutubeApi() # actual http request and parsing videos_a = api.search('Lana') # get cached version videos_c = api.search('Lana') # check if cache stores right values self.assertEqual(videos_a, videos_c)
def test_real_get_channel_videos(self): channel = YoutubeApi().get_channel('LanaDelReyVEVO') blue_jeans = [x for x in channel.get_uploaded_videos() if x.get_id() == 'JRWox-i6aAk'] self.assertEqual(len(blue_jeans), 1) blue_jeans = blue_jeans[0] self.assertEqual(blue_jeans.get_id(), 'JRWox-i6aAk') self.assertEqual(blue_jeans.get_title(), 'Lana Del Rey - Blue Jeans') self.assertEqual(blue_jeans.get_author(), 'LanaDelReyVEVO') self.assertEqual(blue_jeans.get_length(), '4:21') for video in channel.get_uploaded_videos(): self.assertIsInstance(video, VideoSignature)
def test_as_dict(self): html_code = read_in_file('tests/htmls/video_sample_source.txt') video = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).get_video('nVjsGKrE6E8') keys_video = [] keys_video = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views', 'length_in_seconds', 'next_video', 'related_videos'] self.assertTrue(all(x in video.as_dict() for x in keys_video)) keys_videosignature = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views'] self.assertTrue(all(x in video.as_dict()['next_video'] for x in keys_videosignature)) for video in video.as_dict()['related_videos']: self.assertTrue(all(x in video for x in keys_videosignature))
def test_global_cache_is_shared(self): api_1 = YoutubeApi(global_cache=True) api_2 = YoutubeApi(global_cache=True) start = timer() api_1.get_video('nVjsGKrE6E8') time1 = timer() - start api_2.clear_cache() start = timer() api_1.get_video('nVjsGKrE6E8') time2 = timer() - start # assert both are real http calls with parsing self.assertTrue(max(time1, time2) < min(time1, time2) * 15) api_1.clear_cache()
def test_get_object_cache_time(self): api = YoutubeApi() start = timer() api.get_video('nVjsGKrE6E8') api.get_playlist('PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') time1 = timer() - start start = timer() api.get_video('nVjsGKrE6E8') api.get_playlist('PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') time2 = timer() - start self.assertTrue(time1 > time2 * 1000)
def test_search_cache_time(self): api = YoutubeApi() # actual http request and parsing start = timer() api.search('Lana') api.search('Lana del rey') time1 = timer() - start # get cached version start = timer() api.search('Lana') api.search('Lana del rey') time2 = timer() - start # cached should be at least 1000 times faster self.assertTrue(time1 > time2 * 1000)
def test_real_get_playlist(self): playlist = YoutubeApi().get_playlist( 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_id(), 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_name(), 'Lana Del Rey - All songs playlist') self.assertEqual(playlist.get_author(), 'juluatanaya') self.assertTrue(int(playlist.get_length()) >= 105) self.assertEqual( playlist.get_url(), 'https://www.youtube.com/playlist?list=' + 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') for video in playlist.get_videos(): self.assertIsInstance(video, VideoSignature)
def test_global_cache_time(self): api_1 = YoutubeApi(global_cache=True) api_2 = YoutubeApi(global_cache=True) start = timer() api_1.get_video('nVjsGKrE6E8') time1 = timer() - start start = timer() api_2.get_video('nVjsGKrE6E8') time2 = timer() - start self.assertTrue(time1 > time2 * 1000) api_1.clear_cache()
def test_api_get_playlist(self): html_code = read_in_file('tests/htmls/playlist_sample_source.txt') playlist = YoutubeApi(FakeFetcher(html_code)).get_playlist( 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_id(), 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_name(), 'Lana Del Rey - All songs playlist') self.assertEqual(playlist.get_author(), 'juluatanaya') self.assertEqual(int(playlist.get_length()), 118) self.assertEqual( playlist.get_url(), 'https://www.youtube.com/playlist?list=' + 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_thumbnail_url(), 'https://i.ytimg.com/vi/cE6wxDqdOV0/mqdefault.jpg') video = playlist.get_video(0) self.assertEqual(video.get_id(), 'nVjsGKrE6E8') self.assertEqual(video.get_title(), 'Lana Del Rey - Summertime Sadness') self.assertEqual(video.get_author(), 'LanaDelRey') self.assertTrue(video.get_length(), '4:43') for video in playlist.get_videos(): self.assertIsInstance(video, VideoSignature)
def test_api_get_channel_signature(self): html_code = read_in_file('tests/htmls/channel_about_page.txt') html_code2 = read_in_file('tests/htmls/channel_videos_page.txt') channel = YoutubeApi(FakeFetcher(html_code, html_code2)).get_channel('LanaDelReyVEVO') self.assertEqual(channel.get_id(), 'LanaDelReyVEVO') self.assertEqual(channel.get_url(), 'https://www.youtube.com/user/LanaDelReyVEVO') self.assertEqual(channel.get_name(), 'LanaDelReyVEVO') self.assertEqual(channel.get_videos_amount(), 'NOTSET') self.assertEqual(channel.get_subscriptions(), '3983841') self.assertEqual(channel.get_thumbnail_url(), 'https://yt3.ggpht.com/-JXK6ocQ08J8/AAAAAAAAAAI/' + 'AAAAAAAAAAA/aGPAhXfQpMw/s100-c-k-no/photo.jpg')
def test_real_get_playlist(self): playlist = YoutubeApi().get_playlist('PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_id(), 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_name(), 'Lana Del Rey - All songs playlist') self.assertEqual(playlist.get_author(), 'juluatanaya') self.assertTrue(int(playlist.get_length()) >= 105) self.assertEqual(playlist.get_url(), 'https://www.youtube.com/playlist?list=' + 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') for video in playlist.get_videos(): self.assertIsInstance(video, VideoSignature)
def test_search_only_channels_20_results(self): html_code = read_in_file('tests/htmls/search_channels_20_results.txt') found_channels = YoutubeApi(FakeFetcher(html_code)).search_channels('') self.assertEqual(len(found_channels), 20) for channel in found_channels: self.assertIsInstance(channel, ChannelSignature) signature = ChannelSignature( 'UC3N5y6UWKJaKqoU2b_0MfTQ', 'LanaDelReyVEVO', '31', '3 947 232', 'https://yt3.ggpht.com/-JXK6ocQ08J8/AAAAAAAAAAI/' + 'AAAAAAAAAAA/aGPAhXfQpMw/s176-c-k-no/photo.jpg') self.assertTrue(signature in found_channels) invalid_signature = ChannelSignature(' ', ' ', ' ', ' ', ' ') self.assertTrue(invalid_signature not in found_channels)
def test_search_17_results(self): html_code = read_in_file('tests/htmls/search_mixed_17_results.txt') found_videos = YoutubeApi(FakeFetcher(html_code)).search_videos('') self.assertEqual(len(found_videos), 17) for video in found_videos: self.assertIsInstance(video, VideoSignature) signature = VideoSignature('JRWox-i6aAk', 'Lana Del Rey - Blue Jeans', 'LanaDelReyVEVO', '146576399', '4:21') self.assertTrue(signature in found_videos) invalid_signature = VideoSignature(' ', ' ', ' ', ' ', ' ') self.assertTrue(invalid_signature not in found_videos) self.assertEqual( found_videos[0].get_thumbnail_url(), "https://i.ytimg.com/vi/{}/mqdefault.jpg".format( found_videos[0].get_id()))
def test_real_search(self): found_items = YoutubeApi().search('lana del rey') self.assertEqual(len(found_items), 20) videos = [ item for item in found_items if isinstance(item, VideoSignature) ] playlists = [ item for item in found_items if isinstance(item, PlaylistSignature) ] channels = [ item for item in found_items if isinstance(item, ChannelSignature) ] self.assertTrue(len(videos) >= 15) self.assertTrue(len(playlists) >= 1) self.assertTrue(len(channels) >= 1)
def test_api_search(self): html_code = read_in_file('tests/htmls/search_mixed_17_results.txt') found_items = YoutubeApi(FakeFetcher(html_code)).search('') self.assertEqual(len(found_items), 20) videos = [ item for item in found_items if isinstance(item, VideoSignature) ] playlists = [ item for item in found_items if isinstance(item, PlaylistSignature) ] channels = [ item for item in found_items if isinstance(item, ChannelSignature) ] self.assertEqual(len(videos), 17) self.assertEqual(len(playlists), 1) self.assertEqual(len(channels), 2)
def test_api_get_playlist(self): html_code = read_in_file('tests/htmls/playlist_sample_source.txt') playlist = YoutubeApi(FakeFetcher(html_code)).get_playlist( 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_id(), 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_name(), 'Lana Del Rey - All songs playlist') self.assertEqual(playlist.get_author(), 'juluatanaya') self.assertEqual(int(playlist.get_length()), 118) self.assertEqual(playlist.get_url(), 'https://www.youtube.com/playlist?list=' + 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi') self.assertEqual(playlist.get_thumbnail_url(), 'https://i.ytimg.com/vi/cE6wxDqdOV0/mqdefault.jpg') video = playlist.get_video(0) self.assertEqual(video.get_id(), 'nVjsGKrE6E8') self.assertEqual(video.get_title(), 'Lana Del Rey - Summertime Sadness') self.assertEqual(video.get_author(), 'LanaDelRey') self.assertTrue(video.get_length(), '4:43') for video in playlist.get_videos(): self.assertIsInstance(video, VideoSignature)
def test_clear_cache(self): api = YoutubeApi() # actual http request and parsing start = timer() api.search('Lana del rey') time1 = timer() - start # get cached version start = timer() api.search('Lana del rey') time2 = timer() - start # clear cache api.clear_cache() # actual http request and parsing again start = timer() api.search('Lana del rey') time3 = timer() - start self.assertTrue(min(time1, time3) > time2 * 1000)
def test_search_only_playlists_20_results(self): html_code = read_in_file('tests/htmls/search_playlists_20_results.txt') found_playlists = YoutubeApi( FakeFetcher(html_code)).search_playlists('') self.assertEqual(len(found_playlists), 20) for playlist in found_playlists: self.assertIsInstance(playlist, PlaylistSignature) signature = PlaylistSignature('PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi', 'Lana Del Rey - All songs playlist', '119', 'juluatanaya', 'https://i.ytimg.com/vi/nVjsGKrE6E8/mqdefault.jpg', 'nVjsGKrE6E8') self.assertTrue(signature in found_playlists) invalid_signature = PlaylistSignature(' ', ' ', ' ', ' ', ' ', ' ') self.assertTrue(invalid_signature not in found_playlists)
def test_search_only_videos_20_results(self): html_code = read_in_file('tests/htmls/search_videos_20_results.txt') found_videos = YoutubeApi(FakeFetcher(html_code)).search_videos('') self.assertEqual(len(found_videos), 20) for video in found_videos: self.assertIsInstance(video, VideoSignature)
def test_api_get_video(self): html_code = read_in_file('tests/htmls/video_sample_source.txt') video = YoutubeApi(FakeFetcher(html_code)).get_video('nVjsGKrE6E8') self.assertEqual(video.get_id(), 'nVjsGKrE6E8') self.assertEqual(video.get_url(), 'https://www.youtube.com/watch?v=nVjsGKrE6E8') self.assertEqual(video.get_title(), 'Lana Del Rey - Summertime Sadness') self.assertEqual(video.get_author(), 'UCqk3CdGN_j8IR9z4uBbVPSg') self.assertEqual(video.get_views(), '247990936') self.assertTrue(video.get_length(), '4:43') self.assertEqual(video.get_length_in_seconds(), 283) self.assertIsInstance(video.get_next_video(), VideoSignature) self.assertEqual(len(video.get_related_videos()), 17) self.assertEqual(video.get_thumbnail_url(), "https://i.ytimg.com/vi/{}/mqdefault.jpg".format( video.get_id())) for video in video.get_related_videos(): self.assertIsInstance(video, VideoSignature)
def test_real_get_video_invalid_fetcher(self): self.assertRaises(YoutubeApiConnectionError, lambda: YoutubeApi(ExceptionRaisingFetcher() ).get_video('nVjsGKrE6E8'))
def test_real_get_video_invalid_id(self): self.assertRaises(YoutubeInvalidIdError, lambda: YoutubeApi().get_video('hdsvhbdhdsvhhdsv'))
def test_youtubevideo_constructor(self): html_code = read_in_file('tests/htmls/video_sample_source.txt') video = YoutubeApi(FakeFetcher(html_code)).get_video(' ') self.assertTrue(video is not None)
def test_real_search_multiple_results(self): found_videos = YoutubeApi().search_videos('pink floyd') self.assertTrue(len(found_videos) > 15) for video in found_videos: self.assertIsInstance(video, VideoSignature)
def test_real_search_multiple_results(self): found_channels = YoutubeApi().search_channels('lana del rey') self.assertEqual(len(found_channels), 20) for channel in found_channels: self.assertIsInstance(channel, ChannelSignature)
def test_real_search_multiple_results(self): found_results = YoutubeApi().search('pink floyd') self.assertEqual(len(found_results), 20)
def test_real_search_invalid_url(self): self.assertRaises( YoutubeApiConnectionError, lambda: YoutubeApi(ExceptionRaisingFetcher()).search_videos(''))
def test_api_search_no_results(self): html_code = read_in_file('tests/htmls/search_no_results.txt') found_items = YoutubeApi(FakeFetcher(html_code)).search('') self.assertEqual(len(found_items), 0)
def test_youtubeplaylist_constructor(self): html_code = read_in_file('tests/htmls/playlist_sample_source.txt') playlist = YoutubeApi( FakeFetcher(html_code)).get_playlist('PL ') self.assertTrue(playlist is not None)
def test_real_search_invalid_url(self): with self.assertRaises(YoutubeApiConnectionError): YoutubeApi(ExceptionRaisingFetcher()).search('')
def test_real_get_video(self): video = YoutubeApi().get_video('nVjsGKrE6E8') self.assertEqual(video.get_id(), 'nVjsGKrE6E8') self.assertEqual(video.get_title(), 'Lana Del Rey - Summertime Sadness') self.assertEqual(video.get_author(), 'UCqk3CdGN_j8IR9z4uBbVPSg') self.assertTrue(int(video.get_views()) >= 49740141) self.assertEqual(video.get_length(), '4:43') self.assertEqual(video.get_length_in_seconds(), 283) self.assertIsInstance(video.get_next_video(), VideoSignature) self.assertTrue(len(video.get_related_videos()) > 12) for video in video.get_related_videos(): self.assertIsInstance(video, VideoSignature)
def test_real_search_multiple_results(self): found_playlists = YoutubeApi().search_playlists('lana del rey') self.assertEqual(len(found_playlists), 20) for playlist in found_playlists: self.assertIsInstance(playlist, PlaylistSignature)
def test_real_search_no_results(self): found_results = YoutubeApi().search('dbg76i6bncw6ogefnxbwegfbnl') self.assertEqual(len(found_results), 0)
def test_real_get_channel_about(self): channel = YoutubeApi().get_channel('LanaDelReyVEVO') self.assertEqual(channel.get_id(), 'LanaDelReyVEVO') self.assertEqual(channel.get_url(), 'https://www.youtube.com/user/LanaDelReyVEVO') self.assertEqual(channel.get_name(), 'LanaDelReyVEVO') self.assertTrue(int(channel.get_subscriptions()) >= 3983841)