autobroadcaster/init-data.sh

262 lines
7.9 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "========================================"
echo "NocoDB & Test Data Initialization"
echo "========================================"
NOCODB_URL=${NOCODB_URL:-"http://nocodb:8080"}
NOCODB_EMAIL=${NOCODB_EMAIL:-"admin@test.com"}
NOCODB_PASSWORD=${NOCODB_PASSWORD:-"admin123"}
# Wait for NocoDB to be fully ready
echo "Waiting for NocoDB to be ready..."
for i in {1..30}; do
if curl -sf "${NOCODB_URL}/api/v1/health" > /dev/null 2>&1; then
echo "NocoDB is ready!"
break
fi
echo "Attempt $i/30: NocoDB not ready yet, waiting..."
sleep 2
done
# Download test video (Big Buck Bunny - open source test video)
echo ""
echo "Downloading test video..."
if [ ! -f "/raw_movies/BigBuckBunny.mp4" ]; then
wget -q -O /raw_movies/BigBuckBunny.mp4 \
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" \
|| echo "Failed to download test video, but continuing..."
if [ -f "/raw_movies/BigBuckBunny.mp4" ]; then
echo "✓ Test video downloaded: BigBuckBunny.mp4"
else
echo "⚠ Could not download test video - you'll need to provide one manually"
fi
else
echo "✓ Test video already exists"
fi
# Run Python script to setup NocoDB
echo ""
echo "Setting up NocoDB database and table..."
python3 << 'PYTHON_SCRIPT'
import requests
import json
import sys
import os
from datetime import datetime, timedelta
NOCODB_URL = os.getenv("NOCODB_URL", "http://nocodb:8080")
EMAIL = os.getenv("NOCODB_EMAIL", "admin@test.com")
PASSWORD = os.getenv("NOCODB_PASSWORD", "admin123")
print(f"Connecting to NocoDB at {NOCODB_URL}")
# Step 1: Sign up (first time) or sign in
try:
# Try to sign up first
response = requests.post(
f"{NOCODB_URL}/api/v1/auth/user/signup",
json={
"email": EMAIL,
"password": PASSWORD
},
timeout=10
)
if response.status_code == 200:
print("✓ New user created successfully")
auth_data = response.json()
else:
# If signup fails, try signin
response = requests.post(
f"{NOCODB_URL}/api/v1/auth/user/signin",
json={
"email": EMAIL,
"password": PASSWORD
},
timeout=10
)
response.raise_for_status()
print("✓ Signed in successfully")
auth_data = response.json()
except Exception as e:
print(f"✗ Authentication failed: {e}")
sys.exit(1)
token = auth_data.get("token")
if not token:
print("✗ No token received from NocoDB")
sys.exit(1)
print(f"✓ Got authentication token")
headers = {
"xc-auth": token,
"Content-Type": "application/json"
}
# Step 2: Create a new base (project)
try:
response = requests.post(
f"{NOCODB_URL}/api/v1/db/meta/projects/",
headers=headers,
json={
"title": "MovieScheduler",
"type": "database"
},
timeout=10
)
if response.status_code in [200, 201]:
base = response.json()
print(f"✓ Created base: {base.get('title')}")
else:
# Base might already exist, try to get it
response = requests.get(
f"{NOCODB_URL}/api/v1/db/meta/projects/",
headers=headers,
timeout=10
)
bases = response.json().get("list", [])
base = next((b for b in bases if b.get("title") == "MovieScheduler"), bases[0] if bases else None)
if not base:
print("✗ Could not create or find base")
sys.exit(1)
print(f"✓ Using existing base: {base.get('title')}")
except Exception as e:
print(f"✗ Base creation failed: {e}")
sys.exit(1)
base_id = base.get("id")
print(f"Base ID: {base_id}")
# Step 3: Get or create table
try:
# List existing tables
response = requests.get(
f"{NOCODB_URL}/api/v1/db/meta/projects/{base_id}/tables",
headers=headers,
timeout=10
)
tables = response.json().get("list", [])
# Check if Movies table exists
movies_table = next((t for t in tables if t.get("title") == "Movies"), None)
if not movies_table:
# Create Movies table
response = requests.post(
f"{NOCODB_URL}/api/v1/db/meta/projects/{base_id}/tables",
headers=headers,
json={
"table_name": "Movies",
"title": "Movies",
"columns": [
{"column_name": "Id", "title": "Id", "uidt": "ID"},
{"column_name": "Title", "title": "Title", "uidt": "SingleLineText"},
{"column_name": "Language", "title": "Language", "uidt": "SingleLineText"},
{"column_name": "Year", "title": "Year", "uidt": "Year"},
{"column_name": "Date", "title": "Date", "uidt": "DateTime"},
{"column_name": "Image", "title": "Image", "uidt": "URL"},
{"column_name": "IMDB", "title": "IMDB", "uidt": "URL"},
{"column_name": "Time", "title": "Time", "uidt": "Duration"},
{"column_name": "Kanal", "title": "Kanal", "uidt": "SingleLineText"},
{"column_name": "Obejrzane", "title": "Obejrzane", "uidt": "Checkbox"}
]
},
timeout=10
)
response.raise_for_status()
movies_table = response.json()
print(f"✓ Created table: Movies")
else:
print(f"✓ Using existing table: Movies")
except Exception as e:
print(f"✗ Table creation failed: {e}")
sys.exit(1)
table_id = movies_table.get("id")
print(f"Table ID: {table_id}")
# Step 4: Add test movie entries
test_movies = [
{
"Title": "BigBuckBunny",
"Language": "po angielsku; z napisami",
"Year": 2008,
"Date": (datetime.now() + timedelta(minutes=5)).isoformat(),
"Image": "https://peach.blender.org/wp-content/uploads/title_anouncement.jpg",
"IMDB": "https://www.imdb.com/title/tt1254207/",
"Time": "9:56",
"Kanal": "TestTV",
"Obejrzane": False
},
{
"Title": "TestMovie",
"Language": "po angielsku; z napisami",
"Year": 2024,
"Date": (datetime.now() + timedelta(hours=1)).isoformat(),
"Image": "https://example.com/test.jpg",
"IMDB": "https://www.imdb.com/",
"Time": "1:30:00",
"Kanal": "TestTV",
"Obejrzane": False
}
]
try:
for movie in test_movies:
response = requests.post(
f"{NOCODB_URL}/api/v2/tables/{table_id}/records",
headers={"xc-token": token, "Content-Type": "application/json"},
json=movie,
timeout=10
)
if response.status_code in [200, 201]:
print(f"✓ Added movie: {movie['Title']}")
else:
print(f"⚠ Could not add movie {movie['Title']}: {response.text}")
except Exception as e:
print(f"⚠ Some movies could not be added: {e}")
# Step 5: Save configuration for scheduler
print("\n" + "="*50)
print("CONFIGURATION FOR SCHEDULER")
print("="*50)
print(f"NOCODB_URL: http://nocodb:8080/api/v2/tables/{table_id}/records")
print(f"NOCODB_TOKEN: {token}")
print(f"RTMP_SERVER: rtmp://rtmp:1935/live/stream")
print("="*50)
# Save to file that docker-compose can read
with open("/tmp/nocodb_config.env", "w") as f:
f.write(f"NOCODB_TABLE_ID={table_id}\n")
f.write(f"NOCODB_TOKEN={token}\n")
print("\n✓ Configuration saved to /tmp/nocodb_config.env")
print("\n✓ Initialization complete!")
PYTHON_SCRIPT
echo ""
echo "========================================"
echo "✓ All initialization steps completed!"
echo "========================================"
echo ""
echo "NocoDB Dashboard: http://localhost:8080"
echo " Email: ${NOCODB_EMAIL}"
echo " Password: ${NOCODB_PASSWORD}"
echo ""
echo "RTMP Server: rtmp://localhost:1935/live/stream"
echo "RTMP Stats: http://localhost:8081/stat"
echo ""
echo "The scheduler will start automatically."
echo "========================================"