const express = require('express');
const cors = require('cors');
const multer = require('multer');
const pdfParse = require('pdf-parse');
const app = express();
app.use(cors());
app.use(express.json());
const upload = multer({ storage: multer.memoryStorage() });
const uploads = {};
// PDF upload and text extraction
app.post('/api/upload', upload.single('file'), async (req, res) => {
try {
console.log('📄 Processing PDF upload...');
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
if (req.file.mimetype !== 'application/pdf') {
return res.status(400).json({ error: 'Please upload a PDF file' });
}
// Extract text from PDF using pdf-parse
console.log('🔍 Extracting text from PDF...');
const pdfData = await pdfParse(req.file.buffer);
const sceneText = pdfData.text;
if (!sceneText || sceneText.trim().length < 10) {
return res.status(400).json({ error: 'Could not extract readable text from PDF' });
}
const uploadId = Date.now().toString();
uploads[uploadId] = {
filename: req.file.originalname,
sceneText: sceneText.trim(),
uploadTime: new Date()
};
console.log(`✅ PDF processed: ${sceneText.length} characters extracted from ${req.file.originalname}`);
res.json({
uploadId,
filename: req.file.originalname,
textLength: sceneText.length,
preview: sceneText.substring(0, 200) + '...'
});
} catch (error) {
console.error('❌ PDF processing error:', error);
res.status(500).json({
error: 'Failed to process PDF',
details: error.message
});
}
});
// Generate acting guide
app.post('/api/guides/generate', async (req, res) => {
try {
const { characterName, productionTitle, productionType, uploadId } = req.body;
console.log(`🎭 Generating guide for: ${characterName}`);
if (!uploadId || !uploads[uploadId]) {
return res.status(400).json({ error: 'No script uploaded' });
}
const script = uploads[uploadId].sceneText;
// Analyze the script for character insights
const lines = script.split('\n').filter(line => line.trim());
const characterLines = lines.filter(line =>
line.toUpperCase().includes(characterName.toUpperCase())
);
const dialogueLines = lines.filter(line =>
line.includes(':') ||
(line.length > 10 && !line.match(/^[A-Z\s]+$/))
);
// Create comprehensive acting guide
const guideHtml = `
📊 Script Analysis
${script.length}
Total Characters
${lines.length}
Total Lines
${characterLines.length}
${characterName} References
${dialogueLines.length}
Dialogue Lines
📝 Script Preview
${script.substring(0, 1500)}${script.length > 1500 ? '\n\n[Full script continues...]' : ''}
🎭 Character Analysis for ${characterName}
🎯 Key Focus Areas
- Character Voice: Pay attention to how ${characterName} speaks compared to other characters
- Emotional Range: Look for moments of vulnerability, strength, humor, or intensity
- Relationships: How does ${characterName} interact with different characters?
- Character Arc: What journey does ${characterName} go through in these scenes?
💬 Dialogue Analysis
Based on the script, consider these aspects of ${characterName}'s communication style:
- Formality Level: Does the character speak formally or casually?
- Emotional Subtext: What might they be feeling but not saying directly?
- Speech Patterns: Do they speak quickly when excited? Slowly when thinking?
- Vocabulary: What kind of words does this character choose?
🎬 Scene Objectives
For each scene, identify:
- What does ${characterName} want? (Their objective)
- Why do they want it? (Their motivation)
- How do they try to get it? (Their tactics)
- What's stopping them? (Obstacles)
🎯 Acting Preparation Tips
🧠 Character Development
- Create a detailed backstory for ${characterName} that explains their behavior
- Define their relationships with every other character in the script
- Identify their biggest fear and greatest desire
- Consider how they would behave in situations not shown in the script
🗣️ Voice & Physicality
- Vocal Choices: Develop a unique vocal signature for ${characterName}
- Physical Gestures: How do they move? Stand? Sit?
- Nervous Habits: What do they do when uncomfortable?
- Confident Moments: How does their posture change when they feel strong?
🎭 Scene Work
Before each take or performance:
- Review your character's objective for this specific scene
- Consider what happened to them right before this scene starts
- Identify the emotional temperature at the beginning and end
- Practice different ways to achieve your character's goal
📚 Next Steps
- Read through the full script multiple times, focusing on ${characterName}
- Highlight every line your character speaks
- Make notes about their emotional state in each moment
- Practice with scene partners if possible
- Record yourself reading the lines and listen back
✨ Generated by PREP101 • ${new Date().toLocaleDateString()} ✨
Professional Acting Guide from PDF Audition Sides
`;
console.log('✅ Acting guide generated successfully!');
res.json({
ok: true,
guideHtml: guideHtml
});
} catch (error) {
console.error('❌ Guide generation error:', error);
res.status(500).json({
error: 'Failed to generate guide',
message: error.message
});
}
});
// Auth endpoints (keep these for login)
app.post('/api/auth/login', (req, res) => {
res.json({
token: 'fake-token',
user: { id: 1, name: 'Test User', email: req.body.email, subscription: 'free', guidesUsed: 0, guidesLimit: 3 }
});
});
app.get('/api/auth/me', (req, res) => {
res.json({ id: 1, name: 'Test User', email: 'test@test.com', subscription: 'free', guidesUsed: 0, guidesLimit: 3 });
});
app.get('/api/guides/my-guides', (req, res) => {
res.json([]);
});
app.listen(5000, () => {
console.log('🚀 PREP101 AI Backend running on port 5000');
console.log('📄 Ready to process PDF audition sides!');
console.log('🎭 Generating professional acting guides');
});