plugin.audio.librespot/resources/lib/bottle_manager.py

65 lines
2.5 KiB
Python
Raw Normal View History

2024-02-21 06:32:58 +00:00
import os, sys
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "deps"))
2024-02-21 05:35:31 +00:00
from wsgiref.simple_server import make_server
from bottle import app, request, HTTPResponse, Bottle
import xbmcaddon
2024-02-21 05:35:31 +00:00
from librespot.core import Session
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality
from librespot.metadata import TrackId
from utils import ADDON_ID, log_msg
2024-02-21 05:35:31 +00:00
class LibrespotServer(Bottle):
__addon: xbmcaddon.Addon = xbmcaddon.Addon(id=ADDON_ID)
2024-02-21 05:35:31 +00:00
def __init__(self, session: Session):
super(LibrespotServer, self).__init__()
self.session: Session = session
self.route('/track/<track_id>', callback=self.stream)
self.is_premium: bool = self.__addon.getSetting("isPremium")
2024-02-21 05:35:31 +00:00
# TODO: Make Range header work PLEASE I BEG
def stream(self, track_id):
try:
playabletrack_id = TrackId.from_uri(f"spotify:track:{track_id}")
quality = AudioQuality.NORMAL
2024-02-21 09:50:27 +00:00
if self.is_premium == True:
quality = AudioQuality.VERY_HIGH
2024-02-21 05:35:31 +00:00
stream = self.session.content_feeder().load(
playabletrack_id, VorbisOnlyAudioQuality(quality), False,
2024-02-21 05:35:31 +00:00
None)
start = 0
end = stream.input_stream.size
payload = stream.input_stream.stream()
log_msg(stream.input_stream.size)
# reqrange = request.get_header("range")
# if reqrange is not None:
# range_search = re.search(
# "^bytes=(?P<start>[0-9]+?)-(?P<end>[0-9]+?)$",
# reqrange)
# if range_search is not None:
# start = int(range_search.group("start"))
# end = (int(range_search.group("end"))
# if int(range_search.group("end")) <=
# stream.input_stream.size else
# stream.input_stream.size)
# payload.skip(start)
# else:
# payload = stream
response = HTTPResponse(body=payload)
response.add_header('Content-Type', 'audio/ogg')
# response.add_header('Accept-Ranges', 'bytes')
# response.add_header("Content-Length", str(stream.input_stream.size).encode() if
# stream.input_stream.size == end else "{}-{}/{}"
# .format(start, end,
# stream.input_stream.size).encode())
return response
except Exception as e:
log_msg(e)