diff options
| author | DemonKingSwarn <rockingswarn@gmail.com> | 2024-02-01 01:22:03 +0530 | 
|---|---|---|
| committer | DemonKingSwarn <rockingswarn@gmail.com> | 2024-02-01 01:22:03 +0530 | 
| commit | da10d9c42ac52eed54b4a127bccf26ae87fcdd0a (patch) | |
| tree | cb3173b9f47aee316114b23c42e8b5542900c288 /yt_music | |
| download | yt-music-da10d9c42ac52eed54b4a127bccf26ae87fcdd0a.zip yt-music-da10d9c42ac52eed54b4a127bccf26ae87fcdd0a.tar.gz | |
INTRODUCING yt-music
Diffstat (limited to 'yt_music')
| -rw-r--r-- | yt_music/__init__.py | 0 | ||||
| -rw-r--r-- | yt_music/__main__.py | 7 | ||||
| -rw-r--r-- | yt_music/__version__.py | 1 | ||||
| -rw-r--r-- | yt_music/__yt_music__.py | 92 | 
4 files changed, 100 insertions, 0 deletions
| diff --git a/yt_music/__init__.py b/yt_music/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/yt_music/__init__.py diff --git a/yt_music/__main__.py b/yt_music/__main__.py new file mode 100644 index 0000000..cfe86c2 --- /dev/null +++ b/yt_music/__main__.py @@ -0,0 +1,7 @@ +from . import __yt_music__ + +def __ytmusic__(): +    __yt_music__ + +if __name__ == "__main__": +    __ytmusic__() diff --git a/yt_music/__version__.py b/yt_music/__version__.py new file mode 100644 index 0000000..919a733 --- /dev/null +++ b/yt_music/__version__.py @@ -0,0 +1 @@ +__core__ = "0.0.1a" diff --git a/yt_music/__yt_music__.py b/yt_music/__yt_music__.py new file mode 100644 index 0000000..c87a6fd --- /dev/null +++ b/yt_music/__yt_music__.py @@ -0,0 +1,92 @@ +import sys +import re +import subprocess + +import httpx +import fzf + +headers = { +    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0" +} + +client = httpx.Client(headers=headers, timeout=None) + +base_url = "https://vid.puffyan.us" +pattern = r'<a.*?href="/watch\?v=(.*?)".*?><p.*?>(.*?)<\/p></a>' + +MPV_EXECUTABLE = "mpv" + +try: +    if len(sys.argv) == 1: +        query = input("Search: ") +        if query == "": +            print("ValueError: no query parameter provided") +            exit(1) +    else: +        query = " ".join(sys.argv[1:]) +except KeyboardInterrupt: +    exit(0) + +query = query.replace(' ', '+') +opts = [] + + +def extract_video_id(video_title): +    pattern = r' - (\w+)$' +     +    match = re.search(pattern, video_title) +     +    if match: +        video_id = match.group(1) +        return video_id +    else: +        return None + +def play_loop(video_id, video_title): +    +    args = [ +        MPV_EXECUTABLE, +        f"https://music.youtube.com/watch?v={video_id}", +        f"--force-media-title={video_title}", +        "--no-video", +        "--loop", +    ] + +    mpv_process = subprocess.Popen(args, stdout=subprocess.DEVNULL) +    mpv_process.wait() + + + +def play(video_id, video_title): +    +    args = [ +        MPV_EXECUTABLE, +        f"https://music.youtube.com/watch?v={video_id}", +        f"--force-media-title={video_title}", +        "--no-video", +    ] + +    mpv_process = subprocess.Popen(args, stdout=subprocess.DEVNULL) +    mpv_process.wait() + + +def main(): +    fetch = client.get(f"{base_url}/search?q={query}") +    matches = re.findall(pattern, fetch.text) +    for match in matches: +        video_id,  title = match +        opt = f"{title} - {video_id}" +        opts.append(opt) +    ch = fzf.fzf_prompt(opts) +    print(ch) +    idx = extract_video_id(ch) +    play_ch = fzf.fzf_prompt(["play", "loop"]) +    try: +        if play_ch == "play": +            play(idx, ch) +        else: +            play_loop(idx, ch) +    except KeyboardInterrupt: +        exit(0) + +main() | 
