![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/ |
<?php
// Sitewide Lyrics Pull Script
// This script will pull lyrics for all tracks that don't have them
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('max_execution_time', 0); // No time limit
ini_set('memory_limit', '512M');
require_once 'config/database.php';
// Configuration
$BATCH_SIZE = 10; // Process tracks in batches
$DELAY_BETWEEN_REQUESTS = 2; // Seconds between API calls
$MAX_RETRIES = 3;
// Logging
$logFile = 'lyrics_pull_log.txt';
function logMessage($message) {
global $logFile;
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[$timestamp] $message\n";
file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
echo $logEntry;
}
// Function to make API request to get lyrics
function getLyricsFromAPI($trackId, $title, $prompt) {
global $DELAY_BETWEEN_REQUESTS, $MAX_RETRIES;
// API endpoint for lyrics generation using API.box
$apiUrl = 'https://api.api.box/api/v1/lyrics';
// Prepare the request data for API.box
$requestData = [
'prompt' => "Generate lyrics for a song titled '$title'. Style: " . substr($prompt, 0, 200),
'model' => 'v3',
'duration' => 30,
'callBackUrl' => 'https://soundstudiopro.com/callback.php'
];
$headers = [
'Content-Type: application/json',
'Authorization: Bearer 63edba40620216c5aa2c04240ac41dbd',
'User-Agent: SoundStudioPro/1.0'
];
for ($attempt = 1; $attempt <= $MAX_RETRIES; $attempt++) {
try {
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => json_encode($requestData),
'timeout' => 60
]
]);
$response = file_get_contents($apiUrl, false, $context);
if ($response === false) {
throw new Exception("Failed to make API request");
}
$responseData = json_decode($response, true);
// Extract lyrics from API.box response format
if (isset($responseData['data']['lyrics'])) {
return $responseData['data']['lyrics'];
} elseif (isset($responseData['data']['text'])) {
return $responseData['data']['text'];
} elseif (isset($responseData['lyrics'])) {
return $responseData['lyrics'];
} elseif (isset($responseData['text'])) {
return $responseData['text'];
} else {
logMessage("No lyrics found in API response for track $trackId");
return null;
}
} catch (Exception $e) {
logMessage("Attempt $attempt failed for track $trackId: " . $e->getMessage());
if ($attempt < $MAX_RETRIES) {
sleep($DELAY_BETWEEN_REQUESTS);
}
}
}
return null;
}
// Function to update track with lyrics
function updateTrackLyrics($trackId, $lyrics) {
$pdo = getDBConnection();
if (!$pdo) {
logMessage("Database connection failed");
return false;
}
try {
$stmt = $pdo->prepare("
UPDATE music_tracks
SET lyrics = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
");
$result = $stmt->execute([$lyrics, $trackId]);
if ($result) {
logMessage("Successfully updated lyrics for track $trackId");
return true;
} else {
logMessage("Failed to update lyrics for track $trackId");
return false;
}
} catch (Exception $e) {
logMessage("Database error updating track $trackId: " . $e->getMessage());
return false;
}
}
// Main execution
logMessage("=== STARTING SITEWIDE LYRICS PULL ===");
$pdo = getDBConnection();
if (!$pdo) {
logMessage("ERROR: Cannot connect to database");
exit(1);
}
// Get all tracks without lyrics
try {
$stmt = $pdo->prepare("
SELECT id, title, prompt, task_id, status
FROM music_tracks
WHERE (lyrics IS NULL OR lyrics = '')
AND status = 'complete'
AND audio_url IS NOT NULL
AND audio_url != ''
ORDER BY created_at DESC
");
$stmt->execute();
$tracksWithoutLyrics = $stmt->fetchAll();
$totalTracks = count($tracksWithoutLyrics);
logMessage("Found $totalTracks tracks without lyrics");
if ($totalTracks === 0) {
logMessage("No tracks found without lyrics. All tracks already have lyrics!");
exit(0);
}
$successCount = 0;
$errorCount = 0;
$batchCount = 0;
foreach ($tracksWithoutLyrics as $track) {
$batchCount++;
logMessage("Processing track {$track['id']}: {$track['title']} (Batch $batchCount/$totalTracks)");
// Get lyrics from API
$lyrics = getLyricsFromAPI($track['id'], $track['title'], $track['prompt']);
if ($lyrics) {
// Clean up lyrics
$lyrics = str_replace(['[Verse]', '[Chorus]', '[Bridge]', '[Outro]', '[Intro]'], '', $lyrics);
$lyrics = preg_replace('/\[.*?\]/', '', $lyrics); // Remove any [bracketed] content
$lyrics = trim($lyrics);
// Update track with lyrics
if (updateTrackLyrics($track['id'], $lyrics)) {
$successCount++;
logMessage("✅ Successfully added lyrics to track {$track['id']}");
} else {
$errorCount++;
logMessage("❌ Failed to update lyrics for track {$track['id']}");
}
} else {
$errorCount++;
logMessage("❌ Failed to get lyrics for track {$track['id']}");
}
// Add delay between requests to avoid rate limiting
if ($batchCount % $BATCH_SIZE === 0) {
logMessage("Completed batch $batchCount. Taking a short break...");
sleep($DELAY_BETWEEN_REQUESTS * 2);
} else {
sleep($DELAY_BETWEEN_REQUESTS);
}
// Progress update every 10 tracks
if ($batchCount % 10 === 0) {
$progress = round(($batchCount / $totalTracks) * 100, 2);
logMessage("Progress: $progress% ($batchCount/$totalTracks) - Success: $successCount, Errors: $errorCount");
}
}
// Final summary
logMessage("=== LYRICS PULL COMPLETE ===");
logMessage("Total tracks processed: $totalTracks");
logMessage("Successful updates: $successCount");
logMessage("Failed updates: $errorCount");
logMessage("Success rate: " . round(($successCount / $totalTracks) * 100, 2) . "%");
} catch (Exception $e) {
logMessage("ERROR: " . $e->getMessage());
exit(1);
}
logMessage("=== SCRIPT COMPLETED ===");
?>