From 3a6db0d0d3b9e2ce250c4c9c020d7ece4e953b13 Mon Sep 17 00:00:00 2001 From: Pawky Languish Date: Thu, 28 Nov 2024 03:51:42 +0000 Subject: make cli initialize class properly >_> --- bandcamp.py | 3 ++- soundcloud.py | 7 +++--- spotify.py | 4 ++-- test.sh | 1 + youtube.py | 68 +++++++++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/bandcamp.py b/bandcamp.py index 7b32b3f..29f2b2b 100755 --- a/bandcamp.py +++ b/bandcamp.py @@ -83,4 +83,5 @@ class Bandcamp: if __name__ == "__main__": import sys - Bandcamp.bandcamp(Bandcamp, sys.argv[1]) + #Bandcamp.bandcamp(Bandcamp, sys.argv[1]) + Bandcamp().bandcamp(sys.argv[1]) diff --git a/soundcloud.py b/soundcloud.py index a1f5f91..4b6e717 100755 --- a/soundcloud.py +++ b/soundcloud.py @@ -29,7 +29,7 @@ class SoundCloud: url = f"https://soundcloud.com/oembed?{urlencode([('url',url),('format','json')])}" data = urlopen(url).read().decode() data = json_loads(data) - #print(data) + # print(data) # print(data["title"].removesuffix(" by "+data["author_name"]),data["author_name"]) try: artist = data["author_name"] @@ -37,7 +37,7 @@ class SoundCloud: except KeyError: title = "" artist = "" - #print(title.removesuffix(" by " + artist), "|", artist) + # print(title.removesuffix(" by " + artist), "|", artist) if title == "": irc_string = "[\x0304SoundCloud\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" ansi_string = "[\x1b[31mSoundCloud\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" @@ -56,4 +56,5 @@ class SoundCloud: if __name__ == "__main__": import sys - SoundCloud.soundcloud(SoundCloud, sys.argv[1]) + #SoundCloud.soundcloud(SoundCloud, sys.argv[1]) + SoundCloud().soundcloud(sys.argv[1]) diff --git a/spotify.py b/spotify.py index 1586784..fc70f34 100755 --- a/spotify.py +++ b/spotify.py @@ -84,5 +84,5 @@ class Spotify: if __name__ == "__main__": import sys - Spotify.premature_optimization = False - Spotify.spotify(Spotify, sys.argv[1]) + #Spotify.spotify(Spotify, sys.argv[1]) + Spotify().spotify(sys.argv[1]) diff --git a/test.sh b/test.sh index fdcbba3..cbe4f47 100755 --- a/test.sh +++ b/test.sh @@ -19,3 +19,4 @@ ./youtube.py https://www.youtube.com/watch?v=EUD9UTwXAZY ./youtube.py https://www.youtube.com/playlist?list=PL0bbUqXsNHE0ZELST3vW_11GDHKDAwLYh ./youtube.py 'https://www.youtube.com/watch?v=eneLP_P1_fg&list=PL0bbUqXsNHE0ZELST3vW_11GDHKDAwLYh&index=2' +./youtube.py 'https://www.youtube.com/watch?v=I1Dy0Zfw6Qs&list=PLA1IEkclm7_JyTGyYmbT3qId167rGif8I&index=1' diff --git a/youtube.py b/youtube.py index a75f52b..f15613e 100755 --- a/youtube.py +++ b/youtube.py @@ -4,7 +4,14 @@ 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) @@ -21,8 +28,6 @@ class YouTube: or "https://music.youtube.com/watch?v=" in i or "https://youtube.com/shorts/" in i or "https://www.youtube.com/shorts/" in i - or "https://www.youtube.com/clip/" in i - or "https://youtube.com/clip/" in i ] r = list(dict.fromkeys(r)) n = 0 @@ -33,20 +38,36 @@ class YouTube: return r - def is_embed(str): + 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(str): + 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(str): + 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(str): + 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/" ) @@ -68,26 +89,34 @@ class YouTube: videoId = url.split("?")[0].split("/")[-1] url = f"https://www.youtube.com/watch?v={videoId}" url = urlparse(url) - qs=parse_qs(url.query);video_id=qs['v'][0] - try: playlist_id=qs['list'][0] - except KeyError: playlist_id=None - if self.prefer_playlist and playlist_id: - url = url.scheme + "://" + url.netloc + "/playlist?list=" + playlist_id + 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 = 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) + #print(url, " and ", playlist_id) data = urlopen(url).read().decode() data = json_loads(data) - title=data['title'] - channelName=data['author_name'] + 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" + 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 @@ -96,5 +125,8 @@ class YouTube: if __name__ == "__main__": import sys - YouTube.prefer_playlist=False - YouTube.yt(YouTube, sys.argv[1]) + #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]) -- cgit 1.4.1-2-gfad0