def downloaded(self) -> bool: """bool: whether or not the episode is downloaded""" found_downloaded = False feed_dirname = helpers.sanitize_path(str(self._feed)) episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = os.path.join(DataFile.DOWNLOADED_DIR, feed_dirname) if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): found_downloaded = True return found_downloaded
def download(self, download_queue, display=None): """Downloads this episode to the file system. This method currently only supports downloading from an external URL. In the future, it may be worthwhile to determine whether the episode's source is a local file and simply copy it instead. Args: download_queue: the download_queue overseeing this download display: (optional) the display to write status updates to """ if self._enclosure is None: if display is not None: display.change_status("Download failed: episode does not have" " a valid media source") return feed_directory = self._feed_directory() episode_partial_filename = helpers.sanitize_path(str(self)) extension = os.path.splitext(self._enclosure)[1].split('?')[0] output_path = os.path.join(feed_directory, episode_partial_filename + str(extension)) DataFile.ensure_path(output_path) if display is not None: display.change_status("Starting episode download...") t = threading.Thread(target=DataFile.download_to_file, args=[ self._enclosure, output_path, str(self), download_queue, display ], name="download_%s" % str(self)) t.start()
def get_playable(self, config=None) -> str: """Gets a playable path for this episode. This method checks whether the episode is available on the disk, giving the path to that file if so. Otherwise, simply return the episode's enclosure, which is probably a URL. Args: config: (optional) the user's Config. If unset, this will check in DataFile.DEFAULT_DOWNLOADED_DIR Returns: str: a path to a playable file for this episode """ playable = self.enclosure episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory(config) if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): playable = os.path.join(feed_directory, File) return playable
def _feed_directory(self) -> str: """Gets the path to the downloaded episode's feed directory. This method does not ensure whether the directory exists -- it simply acts as a single definition of where it _should_ be. Returns: str: a path to the feed directory """ feed_dirname = helpers.sanitize_path(str(self._feed)) return os.path.join(DataFile.DOWNLOADED_DIR, feed_dirname)
def _feed_directory(self) -> str: """Gets the path to the downloaded episode's feed directory. This method does not ensure whether the directory exists -- it simply acts as a single definition of where it _should_ be. Returns: str: a path to the feed directory """ feed_dirname = helpers.sanitize_path(str(self._feed)) if Config is None or Config["custom_download_dir"] == "": path = DataFile.DEFAULT_DOWNLOADED_DIR else: path = Config["custom_download_dir"] return os.path.join(path, feed_dirname)
def downloaded(self) -> bool: """Determines whether the episode is downloaded. Returns: bool: whether or not the episode is downloaded """ found_downloaded = False episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory() if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): found_downloaded = True return found_downloaded
def check_downloaded(self) -> bool: """Check whether the episode is downloaded. This method updates the downloaded property. Returns: bool: whether or not the episode is downloaded """ self._downloaded = False episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory() if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): self._downloaded = True return self._downloaded
def _feed_directory(self, config=None) -> str: """Gets the path to the downloaded episode's feed directory. This method does not ensure whether the directory exists -- it simply acts as a single definition of where it _should_ be. Args: config: (optional) the user's Config. If unset, this will check in DataFile.DEFAULT_DOWNLOADED_DIR Returns: str: a path to the feed directory """ feed_dirname = helpers.sanitize_path(str(self._feed)) if config is None or config["custom_download_dir"] == "": path = DataFile.DEFAULT_DOWNLOADED_DIR else: path = config["custom_download_dir"] return os.path.join(path, feed_dirname)
def downloaded(self, config=None) -> bool: """Determines whether the episode is downloaded. Args: config: (optional) the user's Config. If unset, this will check in DataFile.DEFAULT_DOWNLOADED_DIR Returns: bool: whether or not the episode is downloaded """ found_downloaded = False episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory(config) if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): found_downloaded = True return found_downloaded
def get_playable(self) -> str: """Gets a playable path for this episode. This method checks whether the episode is available on the disk, giving the path to that file if so. Otherwise, simply return the episode's enclosure, which is probably a URL. Returns: str: a path to a playable file for this episode """ playable = self.enclosure episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory() if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): playable = os.path.join(feed_directory, File) return playable
def delete(self, display=None): """Deletes the episode file from the file system. Args: display: (optional) the display to write status updates to """ if self.downloaded: episode_partial_filename = helpers.sanitize_path(str(self)) feed_directory = self._feed_directory() if os.path.exists(feed_directory): for File in os.listdir(feed_directory): if File.startswith(episode_partial_filename + '.'): os.remove(os.path.join(feed_directory, File)) if display is not None: display.change_status( "Successfully deleted the downloaded episode") # if there are no more files in the feed directory, delete it if len(os.listdir(feed_directory)) == 0: os.rmdir(feed_directory)
def test_sanitize_path_many(): path = "!@#$%^&*()=+`~<>?/" ";:" result = helpers.sanitize_path(path) assert result == "_" * len(path)
def test_sanitize_path_hyphen(): path = "te-st" result = helpers.sanitize_path(path) assert result == "te-st"
def test_sanitize_path_1(): path = "te%st" result = helpers.sanitize_path(path) assert result == "te_st"
def test_sanitize_path_blank(): path = "" result = helpers.sanitize_path(path) assert result == ""