updated to download
This commit is contained in:
BIN
.test_api.py.swp
Normal file
BIN
.test_api.py.swp
Normal file
Binary file not shown.
Binary file not shown.
41
app.py
41
app.py
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify
|
||||
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify, send_from_directory
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||||
from flask_bcrypt import Bcrypt
|
||||
@@ -41,12 +41,13 @@ migrate = Migrate(app, db)
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return db.session.get(User, int(user_id))
|
||||
return User.query.get(int(user_id))
|
||||
|
||||
# Modele pentru baza de date
|
||||
class User(db.Model, UserMixin):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||
password = db.Column(db.String(120), nullable=False)
|
||||
role = db.Column(db.String(20), nullable=False, default='user')
|
||||
theme = db.Column(db.String(10), nullable=False, default='light')
|
||||
@@ -58,6 +59,9 @@ class Player(db.Model):
|
||||
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)
|
||||
|
||||
class Group(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(80), unique=True, nullable=False)
|
||||
@@ -99,9 +103,10 @@ def dashboard():
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
email = request.form['email']
|
||||
password = request.form['password']
|
||||
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
|
||||
new_user = User(username=username, password=hashed_password, role='user')
|
||||
new_user = User(username=username, email=email, password=hashed_password, role='user')
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return redirect(url_for('login'))
|
||||
@@ -217,10 +222,11 @@ def delete_user(user_id):
|
||||
@admin_required
|
||||
def create_user():
|
||||
username = request.form['username']
|
||||
email = request.form['email']
|
||||
password = request.form['password']
|
||||
role = request.form['role']
|
||||
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
|
||||
new_user = User(username=username, password=hashed_password, role=role)
|
||||
new_user = User(username=username, email=email, password=hashed_password, role=role)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return redirect(url_for('admin'))
|
||||
@@ -534,19 +540,14 @@ def clean_unused_files():
|
||||
|
||||
@app.route('/api/playlists', methods=['GET'])
|
||||
def get_playlist():
|
||||
hostname = request.args.get('hostname')
|
||||
quickconnect_code = request.args.get('quickconnect_code')
|
||||
if not quickconnect_code:
|
||||
return jsonify({'error': 'Quick connect code is required'}), 400
|
||||
if not hostname or not quickconnect_code:
|
||||
return jsonify({'error': 'Hostname and quick connect code are 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
|
||||
player = Player.query.filter_by(hostname=hostname).first()
|
||||
if not player or not player.verify_quickconnect_code(quickconnect_code):
|
||||
return jsonify({'error': 'Invalid hostname or quick connect code'}), 404
|
||||
|
||||
if player.groups:
|
||||
# If the player is part of a group, get the group's content
|
||||
@@ -556,9 +557,13 @@ def get_playlist():
|
||||
# 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]
|
||||
playlist = [{'file_name': item.file_name, 'duration': item.duration, 'url': url_for('media', filename=item.file_name, _external=True)} for item in content]
|
||||
return jsonify({'playlist': playlist})
|
||||
|
||||
@app.route('/media/<path:filename>')
|
||||
def media(filename):
|
||||
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
||||
|
||||
@app.context_processor
|
||||
def inject_theme():
|
||||
if current_user.is_authenticated:
|
||||
@@ -570,4 +575,6 @@ def inject_theme():
|
||||
if __name__ == '__main__':
|
||||
with app.app_context():
|
||||
db.create_all() # Creează toate tabelele
|
||||
app.run(debug=True)
|
||||
app.run(debug=True, host='0.0.0.0')
|
||||
|
||||
|
||||
|
||||
19
import requests.py
Normal file
19
import requests.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:5000'
|
||||
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())
|
||||
Binary file not shown.
BIN
static/uploads/car_modular.jpg
Normal file
BIN
static/uploads/car_modular.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
BIN
static/uploads/harting_contact_2.jpg
Normal file
BIN
static/uploads/harting_contact_2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 312 KiB |
43
test_api.py
43
test_api.py
@@ -1,19 +1,44 @@
|
||||
import requests
|
||||
import os
|
||||
|
||||
# Replace with the actual server IP address or domain name and quick connect code
|
||||
server_ip = 'Http://192.168.0.115'
|
||||
# Replace with the actual server IP address or domain name, hostname, and quick connect code
|
||||
server_ip = 'http://192.168.0.115:5000'
|
||||
hostname = 'TvHolBa1'
|
||||
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}'
|
||||
# Construct the URL with the hostname and quick connect code as query parameters
|
||||
url = f'{server_ip}/api/playlists'
|
||||
params = {
|
||||
'hostname': hostname,
|
||||
'quickconnect_code': quickconnect_code
|
||||
}
|
||||
|
||||
# Make the GET request to the API
|
||||
response = requests.get(url)
|
||||
response = requests.get(url, params=params)
|
||||
|
||||
# Print the raw response content and status code for debugging
|
||||
print(f'Status Code: {response.status_code}')
|
||||
print(f'Response Content: {response.text}')
|
||||
|
||||
# Check if the request was successful
|
||||
if response.status_code == 200:
|
||||
# Parse the JSON response
|
||||
playlist = response.json().get('playlist', [])
|
||||
print('Playlist:', playlist)
|
||||
try:
|
||||
# Parse the JSON response
|
||||
playlist = response.json().get('playlist', [])
|
||||
print('Playlist:', playlist)
|
||||
|
||||
# Download each file in the playlist
|
||||
for item in playlist:
|
||||
file_url = item['url']
|
||||
file_name = item['file_name']
|
||||
response = requests.get(file_url)
|
||||
if response.status_code == 200:
|
||||
with open(file_name, 'wb') as f:
|
||||
f.write(response.content)
|
||||
print(f'Downloaded {file_name}')
|
||||
else:
|
||||
print(f'Failed to download {file_name}: {response.status_code}')
|
||||
except requests.exceptions.JSONDecodeError as e:
|
||||
print(f'Failed to parse JSON response: {e}')
|
||||
else:
|
||||
print('Failed to retrieve playlist:', response.json())
|
||||
print('Failed to retrieve playlist:', response.text)
|
||||
Reference in New Issue
Block a user