added sort endpoint

This commit is contained in:
aj 2019-10-10 11:58:17 +01:00
parent d6bff0f9bd
commit 2a16f2b476
7 changed files with 41 additions and 15 deletions

View File

@ -1,4 +1,4 @@
from .spotify import app from .music import app
import logging import logging
import os import os

View File

@ -2,3 +2,4 @@ from .api import blueprint as api_blueprint
from .player import blueprint as player_blueprint from .player import blueprint as player_blueprint
from .fm import blueprint as fm_blueprint from .fm import blueprint as fm_blueprint
from .spotfm import blueprint as spotfm_blueprint from .spotfm import blueprint as spotfm_blueprint
from .spotify import blueprint as spotify_blueprint

View File

@ -2,15 +2,11 @@ from flask import Blueprint, jsonify
from datetime import date from datetime import date
import logging import logging
from google.cloud import firestore
from spotify.api.decorators import login_or_basic_auth, lastfm_username_required from spotify.api.decorators import login_or_basic_auth, lastfm_username_required
import spotify.db.database as database import spotify.db.database as database
blueprint = Blueprint('fm-api', __name__) blueprint = Blueprint('fm-api', __name__)
db = firestore.Client()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -2,8 +2,6 @@ from flask import Blueprint, request, jsonify
import logging import logging
from google.cloud import firestore
from spotify.api.decorators import login_or_basic_auth, spotify_link_required from spotify.api.decorators import login_or_basic_auth, spotify_link_required
import spotify.db.database as database import spotify.db.database as database
@ -13,7 +11,6 @@ from spotframework.model.service import Context
from spotframework.player.player import Player from spotframework.player.player import Player
blueprint = Blueprint('player_api', __name__) blueprint = Blueprint('player_api', __name__)
db = firestore.Client()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -1,18 +1,13 @@
from flask import Blueprint, jsonify, request from flask import Blueprint, jsonify, request
import logging import logging
from google.cloud import firestore
from spotify.api.decorators import login_or_basic_auth, lastfm_username_required, spotify_link_required from spotify.api.decorators import login_or_basic_auth, lastfm_username_required, spotify_link_required
import spotify.db.database as database import spotify.db.database as database
from spotfm.maths.counter import Counter from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri from spotframework.model.uri import Uri
blueprint = Blueprint('spotfm-api', __name__) blueprint = Blueprint('spotfm-api', __name__)
db = firestore.Client()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

36
spotify/api/spotify.py Normal file
View File

@ -0,0 +1,36 @@
from flask import Blueprint, request, jsonify
import logging
from spotify.api.decorators import login_or_basic_auth, spotify_link_required
import spotify.db.database as database
from spotframework.engine.playlistengine import PlaylistEngine
from spotframework.model.uri import Uri
blueprint = Blueprint('spotify_api', __name__)
logger = logging.getLogger(__name__)
@blueprint.route('/sort', methods=['POST'])
@login_or_basic_auth
@spotify_link_required
def play(username=None):
request_json = request.get_json()
net = database.get_authed_spotify_network(username)
engine = PlaylistEngine(net)
reverse = request_json.get('reverse', False)
if 'uri' in request_json:
try:
uri = Uri(request_json['uri'])
engine.reorder_playlist_by_added_date(uri=uri, reverse=reverse)
except ValueError:
return jsonify({'error': "malformed uri provided"}), 400
elif 'playlist_name' in request_json:
engine.reorder_playlist_by_added_date(name=request_json.get('playlist_name'), reverse=reverse)
else:
return jsonify({'error': "no uris provided"}), 400
return jsonify({'message': 'sorted', 'status': 'success'}), 200

View File

@ -1,10 +1,10 @@
from flask import Flask, render_template, redirect, request, session, flash, url_for from flask import Flask, render_template, redirect, session, flash, url_for
from google.cloud import firestore from google.cloud import firestore
import os import os
from spotify.auth import auth_blueprint from spotify.auth import auth_blueprint
from spotify.api import api_blueprint, player_blueprint, fm_blueprint, spotfm_blueprint from spotify.api import api_blueprint, player_blueprint, fm_blueprint, spotfm_blueprint, spotify_blueprint
db = firestore.Client() db = firestore.Client()
@ -15,6 +15,7 @@ app.register_blueprint(api_blueprint, url_prefix='/api')
app.register_blueprint(player_blueprint, url_prefix='/api/player') app.register_blueprint(player_blueprint, url_prefix='/api/player')
app.register_blueprint(fm_blueprint, url_prefix='/api/fm') app.register_blueprint(fm_blueprint, url_prefix='/api/fm')
app.register_blueprint(spotfm_blueprint, url_prefix='/api/spotfm') app.register_blueprint(spotfm_blueprint, url_prefix='/api/spotfm')
app.register_blueprint(spotify_blueprint, url_prefix='/api/spotify')
@app.route('/') @app.route('/')