api added
This commit is contained in:
Binary file not shown.
29
app.py
29
app.py
@@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||||||
from flask_bcrypt import Bcrypt
|
from flask_bcrypt import Bcrypt
|
||||||
@@ -532,6 +532,33 @@ def clean_unused_files():
|
|||||||
return redirect(url_for('admin'))
|
return redirect(url_for('admin'))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/playlists', methods=['GET'])
|
||||||
|
def get_playlist():
|
||||||
|
quickconnect_code = request.args.get('quickconnect_code')
|
||||||
|
if not quickconnect_code:
|
||||||
|
return jsonify({'error': 'Quick connect code is required'}), 400
|
||||||
|
|
||||||
|
players = Player.query.all()
|
||||||
|
player = None
|
||||||
|
for p in players:
|
||||||
|
if p.verify_quickconnect_code(quickconnect_code):
|
||||||
|
player = p
|
||||||
|
break
|
||||||
|
|
||||||
|
if not player:
|
||||||
|
return jsonify({'error': 'Invalid quick connect code'}), 404
|
||||||
|
|
||||||
|
if player.groups:
|
||||||
|
# If the player is part of a group, get the group's content
|
||||||
|
group = player.groups[0] # Assuming a player can only be in one group
|
||||||
|
content = Content.query.filter_by(group_id=group.id).all()
|
||||||
|
else:
|
||||||
|
# If the player is not part of a group, get the player's content
|
||||||
|
content = Content.query.filter_by(player_id=player.id).all()
|
||||||
|
|
||||||
|
playlist = [{'file_name': item.file_name, 'duration': item.duration} for item in content]
|
||||||
|
return jsonify({'playlist': playlist})
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_theme():
|
def inject_theme():
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
|
|||||||
@@ -5,17 +5,17 @@ services:
|
|||||||
|
|
||||||
image: digi-server:latest
|
image: digi-server:latest
|
||||||
ports:
|
ports:
|
||||||
- "7100:5000"
|
- "80:5000"
|
||||||
environment:
|
environment:
|
||||||
- FLASK_APP=app.py
|
- FLASK_APP=app.py
|
||||||
- FLASK_RUN_HOST=0.0.0.0
|
- FLASK_RUN_HOST=0.0.0.0
|
||||||
- ADMIN_USER=admin
|
- ADMIN_USER=admin
|
||||||
- ADMIN_PASSWORD=Matei
|
- ADMIN_PASSWORD=Initial01!
|
||||||
- SECRET_KEY=Ma_Duc_Dupa_Merele_Lui_Ana
|
- SECRET_KEY=Ma_Duc_Dupa_Merele_Lui_Ana
|
||||||
volumes:
|
volumes:
|
||||||
- .:/app
|
- .:/app
|
||||||
- /home/ske087/digi-server/db:/app/instance
|
- /home/pi/Desktop/digi-server/db:/app/instance
|
||||||
- /home/ske087/digi-server/static:/app/static/uploads
|
- /home/pi/Desktop/digi-server/static:/app/static/uploads
|
||||||
# when setting allready exist and data are setted and is performed an update use second line of command
|
# when setting allready exist and data are setted and is performed an update use second line of command
|
||||||
command: sh -c "python clear_db.py && python init_db.py && gunicorn -w 4 -b 0.0.0.0:5000 app:app"
|
#command: sh -c "python clear_db.py && python init_db.py && gunicorn -w 4 -b 0.0.0.0:5000 app:app"
|
||||||
#command: sh -c "python init_db.py && gunicorn -w 4 -b 0.0.0.0:5000 app:app"
|
command: sh -c "python init_db.py && gunicorn -w 4 -b 0.0.0.0:5000 app:app"
|
||||||
13
models.py
13
models.py
@@ -1,4 +1,7 @@
|
|||||||
from app import db
|
from app import db
|
||||||
|
from flask_bcrypt import Bcrypt
|
||||||
|
|
||||||
|
bcrypt = Bcrypt()
|
||||||
|
|
||||||
class Content(db.Model):
|
class Content(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
@@ -7,4 +10,14 @@ class Content(db.Model):
|
|||||||
player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=True)
|
player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=True)
|
||||||
group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=True)
|
group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=True)
|
||||||
|
|
||||||
|
class Player(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
|
hostname = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
|
password = db.Column(db.String(120), nullable=False)
|
||||||
|
quickconnect_password = db.Column(db.String(120), nullable=False)
|
||||||
|
|
||||||
|
def verify_quickconnect_code(self, code):
|
||||||
|
return bcrypt.check_password_hash(self.quickconnect_password, code)
|
||||||
|
|
||||||
# other models...
|
# other models...
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 150 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 18 KiB |
19
test_api.py
Normal file
19
test_api.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
# Replace with the actual server IP address or domain name and quick connect code
|
||||||
|
server_ip = 'Http://192.168.0.115'
|
||||||
|
quickconnect_code = 'Initial01!'
|
||||||
|
|
||||||
|
# Construct the URL with the quick connect code as a query parameter
|
||||||
|
url = f'{server_ip}/api/playlists?quickconnect_code={quickconnect_code}'
|
||||||
|
|
||||||
|
# Make the GET request to the API
|
||||||
|
response = requests.get(url)
|
||||||
|
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status_code == 200:
|
||||||
|
# Parse the JSON response
|
||||||
|
playlist = response.json().get('playlist', [])
|
||||||
|
print('Playlist:', playlist)
|
||||||
|
else:
|
||||||
|
print('Failed to retrieve playlist:', response.json())
|
||||||
Reference in New Issue
Block a user