From 2a16f2b476a9564cce4c4d1ce6820a8ecf2f6589 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 10 Oct 2019 11:58:17 +0100 Subject: [PATCH] added sort endpoint --- spotify/__init__.py | 2 +- spotify/api/__init__.py | 1 + spotify/api/fm.py | 4 ---- spotify/api/player.py | 3 --- spotify/api/spotfm.py | 5 ----- spotify/api/spotify.py | 36 ++++++++++++++++++++++++++++++++ spotify/{spotify.py => music.py} | 5 +++-- 7 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 spotify/api/spotify.py rename spotify/{spotify.py => music.py} (86%) diff --git a/spotify/__init__.py b/spotify/__init__.py index b0d5691..6e93fc8 100644 --- a/spotify/__init__.py +++ b/spotify/__init__.py @@ -1,4 +1,4 @@ -from .spotify import app +from .music import app import logging import os diff --git a/spotify/api/__init__.py b/spotify/api/__init__.py index 6b924ea..46c891a 100644 --- a/spotify/api/__init__.py +++ b/spotify/api/__init__.py @@ -2,3 +2,4 @@ from .api import blueprint as api_blueprint from .player import blueprint as player_blueprint from .fm import blueprint as fm_blueprint from .spotfm import blueprint as spotfm_blueprint +from .spotify import blueprint as spotify_blueprint diff --git a/spotify/api/fm.py b/spotify/api/fm.py index bd830b3..7899824 100644 --- a/spotify/api/fm.py +++ b/spotify/api/fm.py @@ -2,15 +2,11 @@ from flask import Blueprint, jsonify from datetime import date import logging -from google.cloud import firestore - from spotify.api.decorators import login_or_basic_auth, lastfm_username_required import spotify.db.database as database blueprint = Blueprint('fm-api', __name__) -db = firestore.Client() - logger = logging.getLogger(__name__) diff --git a/spotify/api/player.py b/spotify/api/player.py index 880a7f3..632e835 100644 --- a/spotify/api/player.py +++ b/spotify/api/player.py @@ -2,8 +2,6 @@ from flask import Blueprint, request, jsonify import logging -from google.cloud import firestore - from spotify.api.decorators import login_or_basic_auth, spotify_link_required import spotify.db.database as database @@ -13,7 +11,6 @@ from spotframework.model.service import Context from spotframework.player.player import Player blueprint = Blueprint('player_api', __name__) -db = firestore.Client() logger = logging.getLogger(__name__) diff --git a/spotify/api/spotfm.py b/spotify/api/spotfm.py index 719312b..7a39fd0 100644 --- a/spotify/api/spotfm.py +++ b/spotify/api/spotfm.py @@ -1,18 +1,13 @@ from flask import Blueprint, jsonify, request import logging -from google.cloud import firestore - from spotify.api.decorators import login_or_basic_auth, lastfm_username_required, spotify_link_required - import spotify.db.database as database from spotfm.maths.counter import Counter from spotframework.model.uri import Uri blueprint = Blueprint('spotfm-api', __name__) -db = firestore.Client() - logger = logging.getLogger(__name__) diff --git a/spotify/api/spotify.py b/spotify/api/spotify.py new file mode 100644 index 0000000..e1b5e8a --- /dev/null +++ b/spotify/api/spotify.py @@ -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 diff --git a/spotify/spotify.py b/spotify/music.py similarity index 86% rename from spotify/spotify.py rename to spotify/music.py index 32e8366..bef95a1 100644 --- a/spotify/spotify.py +++ b/spotify/music.py @@ -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 import os 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() @@ -15,6 +15,7 @@ app.register_blueprint(api_blueprint, url_prefix='/api') app.register_blueprint(player_blueprint, url_prefix='/api/player') app.register_blueprint(fm_blueprint, url_prefix='/api/fm') app.register_blueprint(spotfm_blueprint, url_prefix='/api/spotfm') +app.register_blueprint(spotify_blueprint, url_prefix='/api/spotify') @app.route('/')