コード例 #1
0
ファイル: core.py プロジェクト: felipeluna/ytcc
    def play_video(video: Video, audio_only: bool = False) -> bool:
        """Play the given video with the mpv video player.

        The video will not be marked as watched, if the player exits unexpectedly (i.e. exits with
        non-zero exit code) or another error occurs.

        :param video: The video to play.
        :param audio_only: If True, only the audio track of the video is played
        :return: False if the given video_id does not exist or the player closed with a non-zero
         exit code. True if the video was played successfully.
        """
        no_video_flag = []
        if audio_only:
            no_video_flag.append("--no-video")

        if video:
            mpv_flags = filter(bool,
                               map(str.strip, config.ytcc.mpv_flags.split()))
            try:
                command = ["mpv", *no_video_flag, *mpv_flags, video.url]
                subprocess.run(command, check=True)
            except FileNotFoundError as fnfe:
                raise YtccException(
                    "Could not locate the mpv video player!") from fnfe
            except subprocess.CalledProcessError as cpe:
                logger.debug(
                    "MPV failed! Command: %s; Stdout: %s; Stderr %s; Returncode: %s",
                    cpe.cmd, cpe.stdout, cpe.stderr, cpe.returncode)
                return False

            return True

        return False
コード例 #2
0
    def print(self, obj: Printable) -> None:
        if not isinstance(obj, VideoPrintable):
            raise YtccException("RSS can only be generated for videos")

        rss = ET.Element("rss", version="2.0")
        channel = ET.SubElement(rss, "channel")
        title = ET.SubElement(channel, "title")
        title.text = "Ytcc videos"
        link = ET.SubElement(channel, "link")
        link.text = "https://github.com/woefe/ytcc"
        description = ET.SubElement(channel, "description")
        description.text = "Latest videos from your ytcc subscriptions"
        last_build_date = ET.SubElement(channel, "lastBuildDate")
        last_build_date.text = rss2_date(datetime.now(tz=timezone.utc))

        for video in obj.videos:
            item = ET.SubElement(channel, "item")
            title = ET.SubElement(item, "title")
            title.text = video.title
            link = ET.SubElement(item, "link")
            link.text = video.url
            author = ET.SubElement(item, "author")
            author.text = ", ".join(map(lambda v: v.name, video.playlists))
            description = ET.SubElement(item, "description")
            description.text = f"<pre>{html.escape(video.description)}</pre>"
            pub_date = ET.SubElement(item, "pubDate")
            pub_date.text = rss2_date(datetime.fromtimestamp(video.publish_date))
            guid = ET.SubElement(item, "guid", isPermaLink="false")
            guid.text = f"ytcc::{video.id}::{video.extractor_hash}"

        ET.dump(rss)
コード例 #3
0
    def play_video(self, video: Video, audio_only: bool = False) -> bool:
        """Play the given video with the mpv video player and mark the the video as watched.

        The video will not be marked as watched, if the player exits unexpectedly (i.e. exits with
        non-zero exit code) or another error occurs.

        :param video: The video to play.
        :param audio_only: If True, only the audio track of the video is played
        :return: False if the given video_id does not exist or the player closed with a non-zero
         exit code. True if the video was played successfully.
        """
        no_video_flag = []
        if audio_only:
            no_video_flag.append("--no-video")

        if video:
            try:
                command = [
                    "mpv", *no_video_flag, *self.config.mpv_flags,
                    self.get_youtube_video_url(video.yt_videoid)
                ]
                subprocess.run(command, check=True)
            except FileNotFoundError:
                raise YtccException("Could not locate the mpv video player!")
            except subprocess.CalledProcessError:
                return False

            video.watched = True
            return True

        return False
コード例 #4
0
    def get_youtube_video_url(yt_videoid: Optional[str]) -> str:
        """Return the YouTube URL for the given youtube video ID.

        :param yt_videoid:  The YouTube video ID.
        :return: The YouTube URL for the given youtube video ID.
        """
        if yt_videoid is None:
            raise YtccException("Video id is none!")

        return f"https://www.youtube.com/watch?v={yt_videoid}"
コード例 #5
0
 def __init__(self, separator: str = ","):
     super().__init__()
     if len(separator) != 1:
         raise YtccException("Separator must be a single character")
     self.separator = separator