import { Router } from 'express'; import axios from 'axios'; import { authenticate, AuthenticatedRequest, requireAdmin } from '../middleware/auth'; import { asyncHandler } from '../middleware/errorHandler'; const router = Router(); const TAUTULLI_URL = process.env.TAUTULLI_URL || ''; const TAUTULLI_API_KEY = process.env.TAUTULLI_API_KEY || ''; // Get Tautulli connection status router.get('/status', authenticate, requireAdmin, asyncHandler(async (_req: AuthenticatedRequest, res) => { try { const response = await axios.get(`${TAUTULLI_URL}/api/v2`, { params: { apikey: TAUTULLI_API_KEY, cmd: 'get_server_info' } }); res.json({ connected: true, data: response.data.response.data }); } catch (error) { res.status(500).json({ connected: false, error: 'Failed to connect to Tautulli' }); } }) ); // Get watch statistics router.get('/stats', authenticate, requireAdmin, asyncHandler(async (_req: AuthenticatedRequest, res) => { try { const response = await axios.get(`${TAUTULLI_URL}/api/v2`, { params: { apikey: TAUTULLI_API_KEY, cmd: 'get_libraries' } }); res.json(response.data.response.data); } catch (error) { res.status(500).json({ error: 'Failed to fetch stats' }); } }) ); // Get webhook configuration guide router.get('/webhook-config', authenticate, requireAdmin, asyncHandler(async (_req: AuthenticatedRequest, res) => { const webhookUrl = `${process.env.API_URL}/webhooks/tautulli`; res.json({ webhookUrl, instructions: [ '1. Open Tautulli Settings', '2. Go to Notification Agents', '3. Add Webhook', '4. Set Webhook URL to the URL above', '5. Set Webhook Method to POST', '6. Configure triggers for "Watched" events', '7. Set payload to JSON format' ], payloadTemplate: { action: 'watched', user_id: '{user_id}', username: '{username}', rating_key: '{rating_key}', session_key: '{session_key}', media_type: '{media_type}', title: '{title}', grandparent_title: '{grandparent_title}', started: '{started}', stopped: '{stopped}', percent_complete: '{percent_complete}', is_new: '{is_new}' } }); }) ); export { router as tautulliRouter };