Skip to content

Commit

Permalink
[PLAT-375] Add m3u playlist API (#4255)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheran-senthil committed Nov 2, 2022
1 parent b15487c commit cc676ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions discovery-provider/src/api/v1/playlists.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from flask import Blueprint, Response
from flask.globals import request
from flask_restx import Namespace, Resource, fields
from src.api.v1.helpers import (
Expand Down Expand Up @@ -41,6 +42,7 @@
)
from src.trending_strategies.trending_type_and_version import TrendingType
from src.utils.db_session import get_db_read_replica
from src.utils.helpers import decode_string_id
from src.utils.redis_cache import cache
from src.utils.redis_metrics import record_metrics

Expand Down Expand Up @@ -428,3 +430,27 @@ def get(self, version):
)
playlists = get_full_trending_playlists(request, args, strategy)
return success_response(playlists)


playlist_stream_bp = Blueprint("playlist_stream", __name__)


@playlist_stream_bp.route("/v1/playlists/<string:playlist_id>/stream", methods=["GET"])
def playlist_stream(playlist_id):
decoded_id = decode_string_id(playlist_id)
if decoded_id is None:
return f"#Invalid Id: {playlist_id}", 404

tracks = get_tracks_for_playlist(playlist_id=decoded_id, exclude_premium=True)

return Response(
response="#EXTM3U\n"
+ "\n".join(
[
f"#EXTINF:{track['duration']},{track['title']}\n../../tracks/{track['id']}/stream"
for track in tracks
]
),
status=200,
mimetype="application/mpegurl",
)
2 changes: 2 additions & 0 deletions discovery-provider/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sqlalchemy_utils import create_database, database_exists
from src import api_helpers, exceptions, tracer
from src.api.v1 import api as api_v1
from src.api.v1.playlists import playlist_stream_bp
from src.challenges.challenge_event_bus import setup_challenge_bus
from src.challenges.create_new_challenges import create_new_challenges
from src.database_task import DatabaseTask
Expand Down Expand Up @@ -379,6 +380,7 @@ def default(self, o):

app.register_blueprint(api_v1.bp)
app.register_blueprint(api_v1.bp_full)
app.register_blueprint(playlist_stream_bp)

return app

Expand Down

0 comments on commit cc676ef

Please sign in to comment.