aboutsummaryrefslogtreecommitdiff
path: root/yt_music/__yt_music__.py
blob: c87a6fdca51562f53c3b5872b4796ff3d13c0f2c (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
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()