def upload(self, file_name): """ Uploads the file associated with the file_name passed to Google Drive in the Music folder. :param str file_name: Filename of the file to be uploaded :return str: Original filename passed as an argument (in order for the worker to send it to the delete queue) """ response = self.connection.files().list( q="name='Music' and mimeType='application/vnd.google-apps.folder' and trashed=false" ).execute() folder_id = response.get('files', [])[0]['id'] file_metadata = {'name': file_name, 'parents': [folder_id]} media = MediaFileUpload(file_name, mimetype='audio/mpeg') log.info(f"Upload for {file_name} has started") start_time = time() self.connection.files().create(body=file_metadata, media_body=media, fields='id').execute() end_time = time() log.info( f"Upload for {file_name} has finished in {end_time - start_time} seconds" ) return file_name
def upload(self, file_name): """ Moves the file associated with the file_name passed to the Music folder in the local storage. :param str file_name: Filename of the file to be uploaded """ log.info(f"Upload for {file_name} has started") start_time = time() os.rename(file_name, os.path.join(self.music_folder, file_name)) end_time = time() log.info( f"Upload for {file_name} has finished in {end_time - start_time} seconds" )
def delete_local_file(file_name): """ Deletes the file associated with the file_name passed from local storage. :param str file_name: Filename of the file to be deleted :return str: Filename of the file that was just deleted """ try: os.remove(file_name) log.info(f"Deletion for {file_name} has finished") return file_name except OSError: pass
def __call__(self, signum, frame): """ Gets called by Python's signal module when the threads should be stopped. :param signal signum: Signal number that must be handled :param Frame frame: Current stack frame """ log.info('Gracefully killing the threads...') self.stopper.set() for worker in self.workers: worker.join(1) sys.exit(0)
def convert_to_mp3(file_name, delete_queue): """ Converts the file associated with the file_name passed into a MP3 file. :param str file_name: Filename of the original file in local storage :param Queue delete_queue: Delete queue to add the original file to after conversion is done :return str: Filename of the new file in local storage """ file = os.path.splitext(file_name) if file[1] == '.mp3': log.info(f"{file_name} is already a MP3 file, no conversion needed.") return file_name new_file_name = file[0] + '.mp3' ff = FFmpeg(inputs={file_name: None}, outputs={new_file_name: None}) log.info(f"Conversion for {file_name} has started") start_time = time() try: ff.run(stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except FFRuntimeError: os.remove(new_file_name) ff.run(stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) end_time = time() log.info( f"Conversion for {file_name} has finished in {end_time - start_time} seconds" ) delete_queue.put(file_name) return new_file_name
def download(self, url): """ Downloads a MP4 or WebM file that is associated with the video at the URL passed. :param str url: URL of the video to be downloaded :return str: Filename of the file in local storage """ try: yt = YouTube(url) except RegexMatchError: log.error(f"Cannot download file at {url}") else: stream = yt.streams.first() log.info(f"Download for {stream.default_filename} has started") start_time = time() stream.download() end_time = time() log.info( f"Download for {stream.default_filename} has finished in {end_time - start_time} seconds" ) return stream.default_filename