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 __remove_commercials(self, output_path): with utils.temp_file(suffix='.jpg') as temp_picture: # check whether or not to save picture cover # function will throw exception if cant identify cover picture properly save_cover_picture = True try: metadata.picture_extract(output_path, temp_picture) self.logger.debug("Cover picture for file %s being saved to file %s", output_path, temp_picture) except AudioFileException: save_cover_picture = False self.logger.warn("No cover picture found for file %s", output_path) with utils.temp_file(suffix='.mp3', delete=False) as temp_audio: self.logger.debug("Generating audio volume list for commercial identify") volume_list = editor.generate_audio_volume_array(output_path) self.logger.debug("Gathering commercial data for file %s", output_path) commercial_intervals, non_commercial_intervals = \ editor.commercial_identify(output_path, volume_list=volume_list) self.logger.debug("Found commercial intervals %s in file %s, removing commercials", commercial_intervals, output_path) self.logger.debug("Writing new audio file %s with non commercial intervals %s", temp_audio, non_commercial_intervals) editor.commercial_remove(output_path, temp_audio, non_commercial_intervals, verbose=False) self.logger.debug("Moving commercial free file %s back to original path %s", temp_audio, output_path) if save_cover_picture: self.logger.debug("Reset cover picture on file %s from file %s", temp_audio, temp_picture) metadata.picture_update(temp_audio, temp_picture) self.logger.debug("Moving contents of file %s back to correct path %s", temp_audio, output_path) os.rename(temp_audio, output_path) # make sure you get the right file size return os.path.getsize(output_path)
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 temp_image_file(suffix='.jpg'): # stolen from http://stackoverflow.com/questions/10901049/create-set-of-random-jpgs with utils.temp_file(suffix=suffix) as temp: randoms = np.random.rand(30, 30, 3) * 255 im_out = Image.fromarray(randoms.astype('uint8')).convert('RGBA') im_out.save(temp) yield temp
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 download(self, _): if self.raise_error: raise youtube_dl.utils.DownloadError('error message') with temp_audio_file(suffix='.mp4') as mp4_body: with utils.temp_file(delete=False, suffix='.mp4') as temp_file: with open(temp_file, 'wb') as write: write.write(mp4_body) dictionary = { 'status' : 'finished', 'filename' : temp_file, } for hook in self.hooks: hook(dictionary)
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 temp_audio_file(open_data=True, duration=2, suffix='.mp3', delete=True): assert suffix in ['.mp3', '.mp4'], 'Invalid suffix type:%s' % suffix # logic taken from https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=sin with utils.temp_file(suffix=suffix, delete=delete) as temp_file: audio_frames = lambda t: 2 *[np.sin(404 * 2 * np.pi * t)] audioclip = AudioClip(audio_frames, duration=duration) if suffix == '.mp3': audioclip.write_audiofile(temp_file, verbose=False) else: image = ImageClip(np.random.rand(30, 30, 3) * 255) videoclip = image.set_audio(audioclip) videoclip.duration = duration videoclip.fps = 24 videoclip.write_videofile(temp_file, verbose=False) try: if not open_data: yield temp_file else: with open(temp_file, 'r') as f: data = f.read() yield data finally: pass
def test_delete_file(self): with test_utils.temp_client(logging_level=logging.WARNING) as client_args: client = client_args.pop("podcast_client") with utils.temp_file() as temp_file: client._remove_file(temp_file) # pylint:disable=protected-access self.assertFalse(os.path.isfile(temp_file))
def test_logger_file(self): with utils.temp_file() as log_file: with test_utils.temp_client(log_file=log_file): with open(log_file, "r") as file_read: data = file_read.read() self.assertTrue(len(data) > 0)
def test_database_file(self): with utils.temp_file() as temp_file: with test_utils.temp_client(database_file=temp_file) as client_args: client = client_args.pop("podcast_client") self.assert_not_none(client.podcast_list())
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)