|
8 | 8 | :license: FreeBSD, see license file for more details.
|
9 | 9 | '''
|
10 | 10 |
|
| 11 | + |
| 12 | +# Imports |
11 | 13 | # Set default encoding to UTF-8
|
12 | 14 | import sys
|
13 | 15 | reload(sys)
|
14 | 16 | sys.setdefaultencoding('utf8')
|
15 |
| - |
16 |
| -# Imports |
17 | 17 | from flask import Flask, request, redirect, url_for, render_template, jsonify, stream_with_context, Response
|
18 | 18 | from sqlalchemy import or_, and_, desc, func
|
19 | 19 | import json
|
20 | 20 | from ess.db import get_session, Media, Artist, Album, Player, PlaylistEntry
|
21 | 21 | import os.path
|
| 22 | +from time import sleep |
| 23 | + |
| 24 | + |
22 | 25 | # Create aplication
|
23 | 26 | app = Flask(__name__)
|
24 | 27 | app.config.from_object(__name__)
|
@@ -860,6 +863,7 @@ def current_playing_set(name):
|
860 | 863 | return '', 400
|
861 | 864 | return '', 201
|
862 | 865 |
|
| 866 | + |
863 | 867 | @app.route('/playlist/<name>/current/done', methods = ['GET'])
|
864 | 868 | def current_done(name):
|
865 | 869 | '''Let the server know, that the current song was played successful.
|
@@ -895,3 +899,103 @@ def current_done(name):
|
895 | 899 | session.commit()
|
896 | 900 | return '', 204
|
897 | 901 |
|
| 902 | + |
| 903 | + |
| 904 | +@app.route('/command/<name>', methods = ['GET']) |
| 905 | +def command_get(name): |
| 906 | + '''Get command for the player *name*. The Output is JSON |
| 907 | + encoded. |
| 908 | +
|
| 909 | + HTTP return codes: |
| 910 | +
|
| 911 | + ==== ===================== ===================== |
| 912 | + Code Status Meaning |
| 913 | + ==== ===================== ===================== |
| 914 | + 200 OK Returned command |
| 915 | + 200 No Content No Command set |
| 916 | + 404 Not Found Player does not exist |
| 917 | + 500 Internal Server Error Please report this |
| 918 | + ==== ===================== ===================== |
| 919 | +
|
| 920 | + cURL command to get current of “player01“:: |
| 921 | +
|
| 922 | + curl -i http://127.0.0.1:5001/command/player01 |
| 923 | +
|
| 924 | + Example of a result:: |
| 925 | +
|
| 926 | + {"command": "next"} |
| 927 | + ''' |
| 928 | + for i in range(300): |
| 929 | + session = get_session() |
| 930 | + player = session.query(Player).filter(Player.playername==name).first() |
| 931 | + if not player: |
| 932 | + return '', 404 |
| 933 | + if player.command: |
| 934 | + command = player.command |
| 935 | + player.command = None |
| 936 | + session.commit() |
| 937 | + return jsonify({'command':command}), 200 |
| 938 | + session.close() |
| 939 | + sleep(0.1) |
| 940 | + return '', 204 |
| 941 | + |
| 942 | + |
| 943 | +@app.route('/command/<name>', methods = ['PUT']) |
| 944 | +def command_set(name): |
| 945 | + '''Set command for the player *name*. The data have to be JSON encoded. |
| 946 | +
|
| 947 | + HTTP return codes: |
| 948 | +
|
| 949 | + ==== ===================== ===================== |
| 950 | + Code Status Meaning |
| 951 | + ==== ===================== ===================== |
| 952 | + 201 Created Set command |
| 953 | + 400 Bad Request Review your request |
| 954 | + 404 Not Found Player does not exist |
| 955 | + 500 Internal Server Error Please report this |
| 956 | + ==== ===================== ===================== |
| 957 | +
|
| 958 | + Example data to set command “pause“:: |
| 959 | +
|
| 960 | + {"command":"pause"} |
| 961 | +
|
| 962 | + cURL command to set command “pause“ for “player01”:: |
| 963 | +
|
| 964 | + curl -i --request PUT -H 'Content-Type: application/json' \\ |
| 965 | + --data '{"command":"pause"}' "http://127.0.0.1:5001/command/player01" |
| 966 | +
|
| 967 | + Sending the data: |
| 968 | +
|
| 969 | + The data have to be JSON encoded and should fill the whole request body. |
| 970 | + The content type of the request should be “application/json”. If |
| 971 | + necessary, the content type can also be “multipart/form-data” or |
| 972 | + “application/x-www-form-urlencoded” with the JSON data in the field |
| 973 | + called “data”. However, we very much like to discourage you from using |
| 974 | + the later method. While it should work in theory we are only using and |
| 975 | + testing the first method. |
| 976 | + ''' |
| 977 | + if request.content_type in _formdata: |
| 978 | + data = request.form['data'] |
| 979 | + type = request.form['type'] |
| 980 | + else: |
| 981 | + data = request.data |
| 982 | + type = request.content_type |
| 983 | + if not type in ['application/json']: |
| 984 | + return '' % type, 400 |
| 985 | + try: |
| 986 | + data = json.loads(data) |
| 987 | + except Exception as e: |
| 988 | + return '', 400 |
| 989 | + |
| 990 | + command = data.get('command') |
| 991 | + if command is None: |
| 992 | + return '', 400 |
| 993 | + |
| 994 | + session = get_session() |
| 995 | + player = session.query(Player).filter(Player.playername==name).first() |
| 996 | + if not player: |
| 997 | + return '', 404 |
| 998 | + |
| 999 | + player.command = command |
| 1000 | + session.commit() |
| 1001 | + return '', 204 |
0 commit comments