Ejemplo n.º 1
0
    async def _get_episode_data(self, same_episode: Episode, podcast_id: int,
                                video_id: str, youtube_link: str) -> dict:
        """
        Allows to get information for new episode.
        This info can be given from same episode (episode which has same source_id)
        and part information - from YouTube.

        :return: dict with information for new episode
        """

        if same_episode:
            self.logger.info(
                f"Episode for video {video_id} already exists: {same_episode}. "
                f"Using for information about downloaded file.")
            same_episode_data = same_episode.to_dict(field_names=[
                "source_id",
                "watch_url",
                "title",
                "description",
                "image_url",
                "author",
                "length",
                "file_size",
                "file_name",
                "remote_url",
            ])
        else:
            self.logger.info(
                f"New episode for video {video_id} will be created.")
            same_episode_data = {}

        youtube_info = None
        try:
            youtube_info = await get_youtube_info(youtube_link)
        except YoutubeExtractInfoError:
            add_message(self.request,
                        "Sorry.. Fetching YouTube video was failed",
                        kind="error")

        if youtube_info:
            new_episode_data = {
                "source_id":
                video_id,
                "watch_url":
                youtube_info.watch_url,
                "title":
                self._replace_special_symbols(youtube_info.title),
                "description":
                self._replace_special_symbols(youtube_info.description),
                "image_url":
                youtube_info.thumbnail_url,
                "author":
                youtube_info.author,
                "length":
                youtube_info.length,
                "file_size":
                same_episode_data.get("file_size"),
                "file_name":
                same_episode_data.get("file_name") or get_file_name(video_id),
                "remote_url":
                same_episode_data.get("remote_url"),
            }
            message = "Episode was successfully created from the YouTube video."
            self.logger.info(message)
            self.logger.debug("New episode data = %s", new_episode_data)
            add_message(self.request, message)
        elif same_episode:
            message = "Episode will be copied from other episode with same video."
            self.logger.info(message)
            add_message(self.request, message)
            new_episode_data = same_episode_data
        else:
            raise YoutubeFetchError

        new_episode_data.update({
            "podcast_id": podcast_id,
            "created_by_id": self.user.id
        })
        return new_episode_data
Ejemplo n.º 2
0
    async def _get_episode_data(self, same_episode: Episode) -> dict:
        """
        Allows to get information for new episode.
        This info can be given from same episode (episode which has same source_id)
        and part information - from YouTube.

        :return: dict with information for new episode
        """

        if same_episode:
            logger.info(
                f"Episode for video {self.source_id} already exists: {same_episode}."
            )
            same_episode_data = same_episode.to_dict()
        else:
            logger.info(
                f"New episode for video {self.source_id} will be created.")
            same_episode_data = {}

        extract_error, youtube_info = await get_youtube_info(self.source_url)

        if youtube_info:
            logger.info("Episode will be created from the YouTube video.")
            new_episode_data = {
                "source_id":
                self.source_id,
                "watch_url":
                youtube_info.watch_url,
                "title":
                self._replace_special_symbols(youtube_info.title),
                "description":
                self._replace_special_symbols(youtube_info.description),
                "image_url":
                youtube_info.thumbnail_url,
                "author":
                youtube_info.author,
                "length":
                youtube_info.length,
                "file_size":
                same_episode_data.get("file_size"),
                "file_name":
                same_episode_data.get("file_name")
                or get_file_name(self.source_id),
                "remote_url":
                same_episode_data.get("remote_url"),
            }

        elif same_episode:
            logger.info(
                "Episode will be copied from other episode with same video.")
            same_episode_data.pop("id", None)
            new_episode_data = same_episode_data

        else:
            raise YoutubeFetchError(
                f"Extracting data for new Episode failed: {extract_error}")

        new_episode_data.update({
            "podcast_id": self.podcast_id,
            "created_by_id": self.user_id
        })
        return new_episode_data