Files
MediaCollectorLibary/app/Services/AdultSyncService.php
Lars Behrends 0f95458466 sync logs :D
2025-10-19 14:54:33 +02:00

212 lines
7.7 KiB
PHP

<?php
namespace App\Services;
use App\Models\AdultVideo;
use GuzzleHttp\Client;
use PDO;
use Exception;
class AdultSyncService extends BaseSyncService
{
private Client $httpClient;
private array $xbvrSource;
private array $stashSource;
private int $processedCount = 0;
private int $newCount = 0;
private int $updatedCount = 0;
public function __construct(PDO $pdo, array $source, ?int $existingSyncLogId = null)
{
parent::__construct($pdo, $source, $existingSyncLogId);
// Find XBVR and Stash sources
$this->xbvrSource = $this->findSourceByName('xbvr');
$this->stashSource = $this->findSourceByName('stash');
$this->httpClient = new Client([
'timeout' => 60,
'headers' => [
'User-Agent' => 'MediaCollector/1.0',
'Content-Type' => 'application/json'
]
]);
}
private function findSourceByName(string $name): ?array
{
$stmt = $this->pdo->prepare("SELECT * FROM sources WHERE name = :name AND is_active = 1 LIMIT 1");
$stmt->execute(['name' => $name]);
return $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
}
protected function executeSync(string $syncType): void
{
if (!$this->xbvrSource && !$this->stashSource) {
throw new Exception('No active XBVR or Stash sources found');
}
$this->logProgress('Starting adult content library sync...');
// Sync XBVR content
if ($this->xbvrSource) {
$this->syncXbvrContent();
}
// Sync Stash content
if ($this->stashSource) {
$this->syncStashContent();
}
$this->logProgress("Processed {$this->processedCount} adult content items");
}
private function syncXbvrContent(): void
{
if (!$this->xbvrSource) return;
try {
$xbvrService = new XbvrSyncService($this->pdo, $this->xbvrSource);
$xbvrService->startSync('full');
$this->logProgress("XBVR content synced directly to adult videos");
} catch (Exception $e) {
$this->logProgress("Error syncing XBVR content: " . $e->getMessage());
}
}
private function syncStashContent(): void
{
if (!$this->stashSource) return;
try {
$stashService = new StashSyncService($this->pdo, $this->stashSource);
$stashService->startSync('full');
$this->logProgress("Stash content synced directly to adult videos");
} catch (Exception $e) {
$this->logProgress("Error syncing Stash content: " . $e->getMessage());
}
}
private function migrateXbvrToAdultVideos(): void
{
// Get all movies from XBVR source
$stmt = $this->pdo->prepare("
SELECT m.* FROM movies m
WHERE m.source_id = :xbvr_source_id
");
$stmt->execute(['xbvr_source_id' => $this->xbvrSource['id']]);
$xbvrMovies = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($xbvrMovies as $movie) {
// Check if adult video already exists
$existingStmt = $this->pdo->prepare("
SELECT id FROM adult_videos
WHERE source_id = :adult_source_id AND external_id = :external_id
LIMIT 1
");
$existingStmt->execute([
'adult_source_id' => $this->source['id'],
'external_id' => $movie['id']
]);
if (!$existingStmt->fetch(PDO::FETCH_ASSOC)) {
// Create adult video from XBVR movie
$adultVideoData = [
'title' => $movie['title'],
'overview' => $movie['overview'],
'poster_url' => $movie['poster_url'],
'backdrop_url' => $movie['backdrop_url'],
'rating' => $movie['rating'],
'runtime_minutes' => $movie['runtime_minutes'],
'release_date' => $movie['release_date'],
'director' => $movie['director'],
'writer' => $movie['writer'],
'cast' => $movie['cast'],
'genre' => $movie['genre'],
'metadata' => $movie['metadata'],
'watched' => $movie['watched'],
'watch_count' => $movie['watch_count'],
'is_favorite' => $movie['is_favorite'],
'source_id' => $this->source['id'],
'external_id' => $movie['id'],
'created_at' => $movie['created_at'],
'updated_at' => $movie['updated_at']
];
$columns = array_keys($adultVideoData);
$placeholders = array_map(fn($col) => ":$col", $columns);
$sql = "INSERT INTO adult_videos (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
$insertStmt = $this->pdo->prepare($sql);
$insertStmt->execute($adultVideoData);
$this->processedCount++;
}
}
$this->logProgress("Processed {$this->processedCount} XBVR items");
}
private function migrateStashToAdultVideos(): void
{
// Get all movies from Stash source
$stmt = $this->pdo->prepare("
SELECT m.* FROM movies m
WHERE m.source_id = :stash_source_id
");
$stmt->execute(['stash_source_id' => $this->stashSource['id']]);
$stashMovies = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($stashMovies as $movie) {
// Check if adult video already exists
$existingStmt = $this->pdo->prepare("
SELECT id FROM adult_videos
WHERE source_id = :adult_source_id AND external_id = :external_id
LIMIT 1
");
$existingStmt->execute([
'adult_source_id' => $this->source['id'],
'external_id' => $movie['id']
]);
if (!$existingStmt->fetch(PDO::FETCH_ASSOC)) {
// Create adult video from Stash movie
$adultVideoData = [
'title' => $movie['title'],
'overview' => $movie['overview'],
'poster_url' => $movie['poster_url'],
'backdrop_url' => $movie['backdrop_url'],
'rating' => $movie['rating'],
'runtime_minutes' => $movie['runtime_minutes'],
'release_date' => $movie['release_date'],
'director' => $movie['director'],
'writer' => $movie['writer'],
'cast' => $movie['cast'],
'genre' => $movie['genre'],
'metadata' => $movie['metadata'],
'watched' => $movie['watched'],
'watch_count' => $movie['watch_count'],
'is_favorite' => $movie['is_favorite'],
'source_id' => $this->source['id'],
'external_id' => $movie['id'],
'created_at' => $movie['created_at'],
'updated_at' => $movie['updated_at']
];
$columns = array_keys($adultVideoData);
$placeholders = array_map(fn($col) => ":$col", $columns);
$sql = "INSERT INTO adult_videos (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
$insertStmt = $this->pdo->prepare($sql);
$insertStmt->execute($adultVideoData);
$this->processedCount++;
}
}
$this->logProgress("Processed {$this->processedCount} Stash items");
}
}