Skip to content

Commit

Permalink
INTRODUCING yt-music
Browse files Browse the repository at this point in the history
  • Loading branch information
DemonKingSwarn committed Jan 31, 2024
0 parents commit da10d9c
Show file tree
Hide file tree
Showing 10 changed files with 853 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish version changes to PyPI
on:
push:
paths:
- "yt_music/__version__.py"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checking out repository content
uses: actions/checkout@v2

- name: Setting up Python 3.11
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Build the file
run: |
pip install setuptools wheel
python setup.py sdist
- name: Publish a Python distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
env
yt_music.egg-info
build
__pycache__
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# yt-music

A command line YouTube Music client.

## Installation

```sh
pip install --upgrade yt-music
```

**NOTE** YOU NEED `PYTHON` INSTALLED AND IN YOUR `PATH`

## Usage

- Open a terminal

- Type:
- `yt-music <your music you wanna listen'
or
- `yt-music`
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
httpx
krfzf-py
26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup, find_packages
from yt_music.__version__ import __core__

with open("requirements.txt") as requirements_txt:
requirements = requirements_txt.read().splitlines()

setup(
name="yt-music",
version=__core__,
author="d3m0n@demonkingswarn",
author_email="[email protected]",
description="A command line YouTube Music client",
packages=find_packages(),
url="https://github.com/DemonKingSwarn/yt-music",
keywords=[
"youtube",
"youtube music",
"yt-music"
],
install_requires=requirements,
entry_points="""
[console_scripts]
yt-music=yt_music.__main__:__ytmusic__
""",
include_package_data=True,
)
Empty file added yt_music/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions yt_music/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import __yt_music__

def __ytmusic__():
__yt_music__

if __name__ == "__main__":
__ytmusic__()
1 change: 1 addition & 0 deletions yt_music/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__core__ = "0.0.1a"
92 changes: 92 additions & 0 deletions yt_music/__yt_music__.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit da10d9c

Please sign in to comment.