librespot-python/examples/player/main.py

155 lines
4.7 KiB
Python
Raw Normal View History

2021-05-21 05:32:30 +02:00
import os
import platform
import re
import subprocess
import time
2021-05-21 05:46:07 +02:00
import requests
from librespot.audio.decoders import AudioQuality
from librespot.core import Session
from librespot.metadata import TrackId
from librespot.player.codecs import VorbisOnlyAudioQuality
2021-05-21 05:32:30 +02:00
quality: AudioQuality = AudioQuality.VERY_HIGH
session: Session = None
def clear():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def client():
global quality, session
while True:
clear()
splash()
cmd = input("Player >>> ")
args = cmd.split(" ")
if args[0] == "exit" or args[0] == "quit":
return
if (args[0] == "p" or args[0] == "play") and len(args) == 2:
track_uri_search = re.search(
2021-05-21 05:46:15 +02:00
r"^spotify:track:(?P<TrackID>[0-9a-zA-Z]{22})$", args[1])
track_url_search = re.search(
r"^(https?://)?open\.spotify\.com/track/(?P<TrackID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
2021-05-21 05:46:05 +02:00
args[1],
)
2021-05-21 05:32:30 +02:00
if track_uri_search is not None or track_url_search is not None:
2021-05-21 05:46:15 +02:00
track_id_str = (track_uri_search
if track_uri_search is not None else
track_url_search).group("TrackID")
2021-05-21 05:32:30 +02:00
play(track_id_str)
wait()
if args[0] == "q" or args[0] == "quality":
if len(args) == 1:
print("Current Quality: " + quality.name)
wait()
elif len(args) == 2:
if args[1] == "normal" or args[1] == "96":
quality = AudioQuality.NORMAL
elif args[1] == "high" or args[1] == "160":
quality = AudioQuality.HIGH
elif args[1] == "veryhigh" or args[1] == "320":
quality = AudioQuality.VERY_HIGH
print("Set Quality to %s" % quality.name)
wait()
2021-05-21 05:38:05 +02:00
if (args[0] == "s" or args[0] == "search") and len(args) >= 2:
2021-05-21 05:32:30 +02:00
token = session.tokens().get("user-read-email")
2021-05-21 05:46:05 +02:00
resp = requests.get(
"https://api.spotify.com/v1/search",
2021-05-21 05:46:15 +02:00
{
"limit": "5",
"offset": "0",
"q": cmd[2:],
"type": "track"
},
2021-05-21 05:46:05 +02:00
headers={"Authorization": "Bearer %s" % token},
)
2021-05-21 05:32:30 +02:00
i = 1
tracks = resp.json()["tracks"]["items"]
for track in tracks:
2021-05-21 05:46:15 +02:00
print("%d, %s | %s" % (
i,
track["name"],
",".join([artist["name"] for artist in track["artists"]]),
))
2021-05-21 05:32:30 +02:00
i += 1
position = -1
while True:
num_str = input("Select [1-5]: ")
2021-05-31 13:46:38 +02:00
if num_str == "exit" or num_str == "quit":
return
2021-05-21 05:32:30 +02:00
try:
num = int(num_str)
except ValueError:
continue
if num in range(1, 5, 1):
position = num - 1
break
play(tracks[position]["id"])
wait()
def login():
global session
if os.path.isfile("credentials.json"):
try:
session = Session.Builder().stored_file().create()
return
except RuntimeError:
pass
while True:
user_name = input("UserName: ")
password = input("Password: ")
try:
session = Session.Builder().user_pass(user_name, password).create()
return
except RuntimeError:
pass
def play(track_id_str: str):
track_id = TrackId.from_base62(track_id_str)
stream = session.content_feeder().load(track_id,
VorbisOnlyAudioQuality(quality),
False, None)
2021-05-21 05:46:05 +02:00
ffplay = subprocess.Popen(
["ffplay", "-"],
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
2021-05-21 05:32:30 +02:00
while True:
byte = stream.input_stream.stream().read()
if byte == -1:
return
ffplay.stdin.write(bytes([byte]))
def splash():
2021-05-21 05:46:15 +02:00
print("=================================\n"
"| Librespot-Python Player |\n"
"| |\n"
"| by kokarare1212 |\n"
"=================================\n\n\n")
2021-05-21 05:32:30 +02:00
def main():
login()
client()
def wait(seconds: int = 3):
for i in range(seconds)[::-1]:
print("\rWait for %d second(s)..." % (i + 1), end="")
time.sleep(1)
if __name__ == "__main__":
main()