summary refs log tree commit diff
path: root/youtube.py
blob: f15613ebf5e8eb6c270c48d986c1c04856f4827e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode, urlparse, parse_qs
from json import loads as json_loads


class YouTube:
    def __init__(self):
        try:
            YouTube.prefer_playlist = YouTube.prefer_playlist
        except AttributeError:
            YouTube.prefer_playlist = False

    def mesg(self, msg, t=None):
        self.util.mesg(msg, t)

    def match_urls(self, str):
        r = [
            i
            for i in str.split()
            if "https://youtu.be/" in i
            or "https://www.youtube.com/watch?v=" in i
            or "https://m.youtube.com/watch?v=" in i
            or "https://youtube.com/watch?v=" in i
            or "https://www.youtube.com/embed/" in i
            or "https://www.youtube-nocookie.com/embed/" in i
            or "https://music.youtube.com/watch?v=" in i
            or "https://youtube.com/shorts/" in i
            or "https://www.youtube.com/shorts/" in i
        ]
        r = list(dict.fromkeys(r))
        n = 0
        for i in r:
            if not i.startswith("http"):
                r.pop(n)
            n += 1

        return r

    def is_embed(self, *str):
        if type(self) == type("a"):
            str = self
        else:
            str = str[0]
        return str.startswith("https://www.youtube.com/embed/") or str.startswith(
            "https://www.youtube-nocookie.com/embed/"
        )

    def is_ytmusic(self, *str):
        if type(self) == type("a"):
            str = self
        else:
            str = str[0]
        return str.startswith("https://music.youtube.com/watch?v=")

    def is_ytshorts(self, *str):
        if type(self) == type("a"):
            str = self
        else:
            str = str[0]
        return str.startswith("https://youtube.com/shorts/") or str.startswith(
            "https://www.youtube.com/shorts/"
        )

    def is_clip(self, *str):
        if type(self) == type("a"):
            str = self
        else:
            str = str[0]
        return str.startswith("https://youtube.com/clip/") or str.startswith(
            "https://www.youtube.com/clip/"
        )

    def yt(self, url):
        irc_string = "[\x0304Youtube\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03"
        ansi_string = "[\x1b[31mYoutube\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m"
        # self.util.mesg("dbg hello")
        url = url.rstrip("\x01")
        if self.is_embed(url):
            videoId = url.split("/")[4]
            url = f"https://www.youtube.com/watch?v={videoId}"
        elif self.is_ytmusic(url):
            for i in url.split("?")[1].split("&"):
                if i[0:2] == "v=":
                    videoId = i[2:]
            url = f"https://www.youtube.com/watch?v={videoId}"
        elif self.is_ytshorts(url):
            videoId = url.split("?")[0].split("/")[-1]
            url = f"https://www.youtube.com/watch?v={videoId}"
        url = urlparse(url)
        qs = parse_qs(url.query)
        try:
            video_id = qs["v"][0]
        except KeyError:
            video_id = None
        try:
            playlist_id = qs["list"][0]
        except KeyError:
            playlist_id = None
        if (self.prefer_playlist and playlist_id) or (playlist_id and not video_id):
            url = url.scheme + "://" + url.netloc + "/playlist?list=" + playlist_id
        else:
            url = url.scheme + "://" + url.netloc + url.path + "?v=" + video_id
        url = f"https://www.youtube.com/oembed?{urlencode([('url',url),('format','json')])}"
        try:
            #print(url, " and ", playlist_id)
            data = urlopen(url).read().decode()
            data = json_loads(data)
            title = data["title"]
            channelName = data["author_name"]
        except HTTPError as e:
            irc_string = f"[\x0304Youtube\x03] \x0307ERROR:\x0308 {e} \x0315\x03"
            ansi_string = f"[\x1b[31mYoutube\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m {e} \x1b[37;2m\x1b[0m"
            print(ansi_string)
            return irc_string, True
        irc_string = (
            f"[\x0303Youtube\x03] \x02{title}\x02 uploaded by \x1d{channelName}\x1d"
        )
        ansi_string = f"[\x1b[32mYoutube\x1b[0m] \x1b[1m{title}\x1b[0m uploaded by \x1b[03m{channelName}\x1b[0m"
        print(ansi_string)
        return irc_string, False


if __name__ == "__main__":
    import sys

    #if url is a video that's part of a playlist,
    #return playlist (True) or video (False, default)?
    #YouTube.prefer_playlist=False
    YouTube().yt(sys.argv[1])
    #YouTube.yt(YouTube, sys.argv[1])