def test_podcast_file_sync(self): # download only one podcast episode with utils.temp_podcast(self.client, archive_type='soundcloud', max_allowed=1) as podcast: url = urls.soundcloud_track_list(podcast['broadcast_id'], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_one_track.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[0]['download_url'], mp3_body) self.client.podcast_file_sync() episode_list = self.client.episode_list() self.assert_not_none(episode_list[0]['file_path']) first_episode_date = episode_list[0]['date'] # add an additional, newer podcast, make sure things are deleted url = urls.soundcloud_track_list(podcast['broadcast_id'], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_two_tracks.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[1]['download_url'], mp3_body) self.client.podcast_file_sync() # make sure 2 episodes in db, but only 1 with a file path episode_list = self.client.episode_list() self.assert_not_none(episode_list[0]['file_path']) all_episodes = self.client.episode_list(only_files=False) self.assertNotEqual(len(episode_list), len(all_episodes)) second_episode_date = episode_list[0]['date'] self.assertTrue(datetime.strptime(second_episode_date, self.client.datetime_output_format) > datetime.strptime(first_episode_date, self.client.datetime_output_format))
def test_audio_picture_update_invalid_type(self): # foo is a bad file to load from with utils.temp_file(suffix='.foo') as test_file: with test_utils.temp_audio_file(open_data=False) as temp_audio: with self.assertRaises(AudioFileException) as error: metadata.picture_update(temp_audio, test_file) self.check_error_message('Unsupported image type:%s' % test_file, error) # test the ones that work with test_utils.temp_image_file(suffix='.jpg') as test_file: with test_utils.temp_audio_file(open_data=False) as temp_audio: metadata.picture_update(temp_audio, test_file) with test_utils.temp_image_file(suffix='.png') as test_file: with test_utils.temp_audio_file(open_data=False) as temp_audio: metadata.picture_update(temp_audio, test_file)
def test_episode_download_remove_commercial_with_picture(self): # curl download used for rss and soundcloud pod_args = {"archive_type": "rss", "max_allowed": 1, "remove_commercials": True} with test_utils.temp_podcast(self.client, broadcast_url=True, **pod_args) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file(open_data=False) as mp3_file: with test_utils.temp_image_file() as image_file: metadata.picture_update(mp3_file, image_file) with open(mp3_file, "r") as f: mp3_body = f.read() test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_not_none(episode["file_path"]) # make sure episode list shows episode with only_files=True episode_list = self.client.episode_list() self.assert_length(episode_list, 1) self.assert_not_none(episode_list[0]["file_size"]) self.assertTrue(episode_list[0]["file_size"] > 0) # make sure image file is right with utils.temp_file(suffix=".jpg") as temper: metadata.picture_extract(episode_list[0]["file_path"], temper) with open(temper, "r") as f: with open(image_file, "r") as ff: self.assertEqual(f.read(), ff.read())
def test_audio_picture_extract_data(self): # test picture update and extract and data is same with test_utils.temp_audio_file(open_data=False) as temp_audio: # test with jpg with test_utils.temp_image_file() as temp_pic: with open(temp_pic, 'rb') as reader: temp_pic_data = reader.read() metadata.picture_update(temp_audio, temp_pic) with utils.temp_file(suffix='.jpg') as new_temp_pic: metadata.picture_extract(temp_audio, new_temp_pic) with open(new_temp_pic, 'rb') as reader: new_temp_data = reader.read() self.assertEqual(new_temp_data, temp_pic_data) # test with png with test_utils.temp_image_file(suffix='.png') as temp_pic: with open(temp_pic, 'rb') as reader: temp_pic_data = reader.read() metadata.picture_update(temp_audio, temp_pic) with utils.temp_file(suffix='.png') as new_temp_pic: metadata.picture_extract(temp_audio, new_temp_pic) with open(new_temp_pic, 'rb') as reader: new_temp_data = reader.read() self.assertEqual(new_temp_data, temp_pic_data)
def test_podcast_location_update(self): # check fails with invalid data with self.assertRaises(HathorException) as error: self.client.podcast_update_file_location(1, 'foo') self.check_error_message('Podcast not found for ID:1', error) # check works with valid data with utils.temp_podcast(self.client, archive_type='rss', broadcast_url=True, max_allowed=2) as podcast: httpretty.register_uri(httpretty.GET, podcast['broadcast_id'], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[0]['download_url'], mp3_body) self.client.episode_download(episode_list[0]['id']) old_episode = self.client.episode_show(episode_list[0]['id'])[0] with utils.temp_dir(delete=False) as temp: self.client.podcast_update_file_location(podcast['id'], temp) # make sure episode path changed new_episode = self.client.episode_show(episode_list[0]['id'])[0] self.assertTrue(new_episode['file_path'].startswith(temp)) self.assertNotEqual(old_episode['file_path'], new_episode['file_path']) # make sure podcast path changed new_podcast = self.client.podcast_show(podcast['id'])[0] self.assertNotEqual(podcast['file_location'], new_podcast['file_location'])
def test_episode_prevent_deletion(self): # download only one podcast episode with test_utils.temp_podcast(self.client, archive_type="soundcloud", max_allowed=1) as podcast: url = urls.soundcloud_track_list(podcast["broadcast_id"], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_one_track.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.podcast_file_sync() # mark episode to prevent deletion self.client.episode_update(episode_list[0]["id"], prevent_delete=True) # add an additional, newer podcast, make sure prevented deletion episode stays url = urls.soundcloud_track_list(podcast["broadcast_id"], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_three_tracks.DATA)) self.client.episode_sync(max_episode_sync=0) episode_list = self.client.episode_list(only_files=False) test_utils.mock_mp3_download(episode_list[1]["download_url"], mp3_body) test_utils.mock_mp3_download(episode_list[2]["download_url"], mp3_body) self.client.podcast_file_sync() episode_list = self.client.episode_list() ep_ids = [i["id"] for i in episode_list] self.assertTrue(2 in ep_ids) self.assertTrue(1 in ep_ids) self.assertTrue(3 not in ep_ids)
def test_audio_tags_reset_mp4(self): with test_utils.temp_audio_file(open_data=False, suffix='.mp4') as temp: args = { 'title' : utils.random_string(), } metadata.tags_update(temp, **args) metadata.tags_delete(temp, 'foo', 'bar') new_tags = metadata.tags_show(temp) self.assertEqual(new_tags, args)
def test_audio_tags_none_isnt_set(self): with test_utils.temp_audio_file(open_data=False) as temp: args = { 'title' : utils.random_string(), 'album' : None, } metadata.tags_update(temp, **args) new_tags = metadata.tags_show(temp) self.assertEqual(new_tags, {'title' : args['title']})
def test_audio_tags_delete_args_not_there(self): with test_utils.temp_audio_file(open_data=False) as temp: args = { 'title' : utils.random_string(), 'album' : utils.random_string(), 'artist' : utils.random_string(), 'album_artist' : utils.random_string(), } metadata.tags_update(temp, **args) metadata.tags_delete(temp, 'foo') new_tags = metadata.tags_show(temp) self.assertEqual(args, new_tags)
def test_podcast_file_sync_no_automatic_episode_download(self): # make sure no max allowed downloads all possible podcasts with utils.temp_podcast(self.client, archive_type='soundcloud', max_allowed=None, automatic_download=False) as podcast: url = urls.soundcloud_track_list(podcast['broadcast_id'], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_one_track_only_page.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[0]['download_url'], mp3_body) self.client.podcast_file_sync() episode_list = self.client.episode_list() self.assert_length(episode_list, 0)
def test_episode_download_curl(self): # curl download used for rss and soundcloud with test_utils.temp_podcast(self.client, archive_type="rss", broadcast_url=True) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_not_none(episode["file_path"]) # make sure episode list shows episode with only_files=True episode_list = self.client.episode_list() self.assert_length(episode_list, 1)
def test_episode_delete(self): with test_utils.temp_podcast(self.client, archive_type="rss", broadcast_url=True) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_not_none(episode["file_path"]) self.client.episode_delete(episode_list[0]["id"]) # make sure actually deleted self.assert_length(self.client.episode_list(), 0)
def test_podcast_dont_delete_episode_files(self): with utils.temp_podcast(self.client, archive_type='soundcloud', max_allowed=1) as podcast: url = urls.soundcloud_track_list(podcast['broadcast_id'], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_one_track.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[0]['download_url'], mp3_body) self.client.podcast_file_sync() episode_list = self.client.episode_list() # delete and make sure file is still there self.client.podcast_delete(podcast['id'], delete_files=False) self.assertTrue(len(os.listdir(podcast['file_location'])) > 0) os.remove(episode_list[0]['file_path']) os.rmdir(podcast['file_location'])
def test_podcast_file_sync_exclude(self): # create two podcasts, exclude one, make sure only that pod was updated with utils.temp_podcast(self.client, archive_type='soundcloud', max_allowed=1) as podcast1: url = urls.soundcloud_track_list(podcast1['broadcast_id'], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_one_track.DATA)) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with utils.temp_audio_file() as mp3_body: utils.mock_mp3_download(episode_list[0]['download_url'], mp3_body) with utils.temp_podcast(self.client) as podcast2: self.client.podcast_file_sync(exclude_podcasts=[podcast2['id']], ) episode_list = self.client.episode_list() self.assertTrue(len(episode_list) > 0) for episode in episode_list: self.assertEqual(podcast1['id'], episode['podcast_id'])
def test_episode_download_remove_commercial(self): # curl download used for rss and soundcloud pod_args = {"archive_type": "rss", "max_allowed": 1, "remove_commercials": True} with test_utils.temp_podcast(self.client, broadcast_url=True, **pod_args) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_not_none(episode["file_path"]) # make sure episode list shows episode with only_files=True episode_list = self.client.episode_list() self.assert_length(episode_list, 1) self.assert_not_none(episode_list[0]["file_size"]) self.assertTrue(episode_list[0]["file_size"] > 0)
def test_episode_delete_file(self): # check works with valid input with test_utils.temp_podcast(self.client, archive_type="rss", broadcast_url=True) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) # make sure file exists episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_not_none(episode["file_path"]) # delete episode file, but not episode self.client.episode_delete_file(episode_list[0]["id"]) episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_none(episode["file_path"]) self.assert_none(episode["file_size"])
def test_podcast_database_cleanup(self): # download only one podcast episode with test_utils.temp_podcast(self.client, archive_type="soundcloud", max_allowed=1) as podcast: url = urls.soundcloud_track_list(podcast["broadcast_id"], self.client.soundcloud_client_id) httpretty.register_uri(httpretty.GET, url, body=json.dumps(soundcloud_two_tracks.DATA)) self.client.episode_sync(max_episode_sync=0) episode_list = self.client.episode_list(only_files=False) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.podcast_file_sync() episode_list = self.client.episode_list() self.assert_not_none(episode_list[0]["file_path"]) all_episodes = self.client.episode_list(only_files=False) self.assertTrue(len(all_episodes) > 1) self.client.database_cleanup() all_episodes = self.client.episode_list(only_files=False) self.assert_length(all_episodes, 1)
def test_episode_show(self): # check works with valid data with test_utils.temp_podcast(self.client, archive_type="rss", broadcast_url=True) as podcast: httpretty.register_uri(httpretty.GET, podcast["broadcast_id"], body=history_on_fire.DATA) self.client.episode_sync() episode_list = self.client.episode_list(only_files=False) self.assert_not_length(episode_list, 0) with test_utils.temp_audio_file() as mp3_body: test_utils.mock_mp3_download(episode_list[0]["download_url"], mp3_body) self.client.episode_download(episode_list[0]["id"]) # works as single episode = self.client.episode_show(episode_list[0]["id"])[0] self.assert_dictionary(episode) # works as list episode = self.client.episode_show([episode_list[0]["id"]])[0] self.assert_dictionary(episode)
def test_audio_tags(self): with test_utils.temp_audio_file(open_data=False) as temp: args = { 'title' : utils.random_string(), 'album' : utils.random_string(), 'performer' : utils.random_string(), 'track_number' : '1/2', 'disc_number' : '1/1', 'genre' : utils.random_string(), 'date' : '2015', 'copyright' : utils.random_string(), 'album_artist' : utils.random_string(), } metadata.tags_update(temp, **args) new_tags = metadata.tags_show(temp) self.assertEqual(args, new_tags) for key in args: metadata.tags_delete(temp, key) new_tags = metadata.tags_show(temp) self.assertEqual(new_tags, {})
def test_audio_picture_extract_name_overriden(self): with test_utils.temp_audio_file(open_data=False) as temp_audio: # give it a bad ending, should be overriden with .jpg with test_utils.temp_image_file() as temp_pic: metadata.picture_update(temp_audio, temp_pic) with utils.temp_file(suffix='.foo') as new_temp_pic: output = metadata.picture_extract(temp_audio, new_temp_pic) actual_path = output['output_path'] self.assertNotEqual(actual_path, new_temp_pic) self.assertTrue(actual_path.endswith('.jpg')) # make sure file gets deleted os.remove('%s.jpg' % new_temp_pic) # give it a bad ending, should be overriden with .png with test_utils.temp_image_file(suffix='.png') as temp_pic: metadata.picture_update(temp_audio, temp_pic) with utils.temp_file(suffix='.foo') as new_temp_pic: output = metadata.picture_extract(temp_audio, new_temp_pic) actual_path = output['output_path'] self.assertNotEqual(actual_path, new_temp_pic) self.assertTrue(actual_path.endswith('.png')) # make sure file gets deleted os.remove('%s.png' % new_temp_pic)
def test_volume_data_png(self): #pylint:disable=no-self-use with test_utils.temp_audio_file(open_data=False) as temp_audio: with utils.temp_file(suffix='.png') as temp_png: editor.volume_data_png(temp_audio, temp_png)
def test_generate_volume_data(self): length = 12 with test_utils.temp_audio_file(open_data=False, duration=length) as temp_audio: volume_data = editor.generate_audio_volume_array(temp_audio) self.assert_length(volume_data, length - 1)