Add web admin features + fix scraper & translator

Frontend changes:
- Add /admin dashboard for article management
- Add AdminButton component (Alt+Shift+A on articles)
- Add /api/admin/article API endpoints

Backend improvements:
- scraper_v2.py: Multi-layer fallback extraction (newspaper → trafilatura → readability)
- translator_v2.py: Better chunking, repetition detection, validation
- admin_tools.py: CLI admin commands
- test_scraper.py: Individual source testing

Docs:
- WEB-ADMIN-GUIDE.md: Web admin usage
- ADMIN-GUIDE.md: CLI admin usage
- SCRAPER-IMPROVEMENT-PLAN.md: Scraper fixes details
- TRANSLATION-FIX.md: Translation improvements
- ADMIN-FEATURES-SUMMARY.md: Implementation summary

Fixes:
- Article scraping from 0 → 96+ articles working
- Translation quality issues (repetition, truncation)
- Added 13 new RSS sources
This commit is contained in:
Zeya Phyo
2026-02-26 09:17:50 +00:00
parent 8bf5f342cd
commit f51ac4afa4
20 changed files with 4769 additions and 23 deletions

277
frontend/app/admin/page.tsx Normal file
View File

@@ -0,0 +1,277 @@
'use client';
import { useState, useEffect } from 'react';
import Link from 'next/link';
interface Article {
id: number;
title: string;
title_burmese: string;
slug: string;
status: string;
content_length: number;
burmese_length: number;
published_at: string;
view_count: number;
}
export default function AdminDashboard() {
const [password, setPassword] = useState('');
const [isAuthed, setIsAuthed] = useState(false);
const [articles, setArticles] = useState<Article[]>([]);
const [loading, setLoading] = useState(false);
const [message, setMessage] = useState('');
const [statusFilter, setStatusFilter] = useState('published');
useEffect(() => {
// Check if already authenticated
const stored = sessionStorage.getItem('adminAuth');
if (stored) {
setIsAuthed(true);
setPassword(stored);
loadArticles(stored, statusFilter);
}
}, []);
const handleAuth = () => {
sessionStorage.setItem('adminAuth', password);
setIsAuthed(true);
loadArticles(password, statusFilter);
};
const loadArticles = async (authToken: string, status: string) => {
setLoading(true);
try {
const response = await fetch(`/api/admin/article?status=${status}&limit=50`, {
headers: {
'Authorization': `Bearer ${authToken}`
}
});
if (response.ok) {
const data = await response.json();
setArticles(data.articles);
} else {
setMessage('❌ Authentication failed');
sessionStorage.removeItem('adminAuth');
setIsAuthed(false);
}
} catch (error) {
setMessage('❌ Error loading articles');
} finally {
setLoading(false);
}
};
const handleAction = async (articleId: number, action: string) => {
if (!confirm(`Are you sure you want to ${action} article #${articleId}?`)) {
return;
}
try {
const response = await fetch('/api/admin/article', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${password}`
},
body: JSON.stringify({ articleId, action })
});
if (response.ok) {
setMessage(`✅ Article ${action}ed successfully`);
loadArticles(password, statusFilter);
} else {
const data = await response.json();
setMessage(`${data.error}`);
}
} catch (error) {
setMessage('❌ Error: ' + error);
}
};
if (!isAuthed) {
return (
<div className="min-h-screen bg-gray-100 flex items-center justify-center">
<div className="bg-white p-8 rounded-lg shadow-lg max-w-md w-full">
<h1 className="text-3xl font-bold mb-6 text-center">🔒 Admin Login</h1>
<input
type="password"
placeholder="Admin Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleAuth()}
className="w-full px-4 py-3 border rounded-lg mb-4 text-lg"
/>
<button
onClick={handleAuth}
className="w-full bg-blue-600 text-white py-3 rounded-lg font-bold hover:bg-blue-700"
>
Login
</button>
<p className="mt-4 text-sm text-gray-600 text-center">
Enter admin password to access dashboard
</p>
</div>
</div>
);
}
const translationRatio = (article: Article) => {
if (article.content_length === 0) return 0;
return Math.round((article.burmese_length / article.content_length) * 100);
};
const getStatusColor = (status: string) => {
return status === 'published' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800';
};
const getRatioColor = (ratio: number) => {
if (ratio >= 40) return 'text-green-600';
if (ratio >= 20) return 'text-yellow-600';
return 'text-red-600';
};
return (
<div className="min-h-screen bg-gray-100">
<div className="bg-white shadow">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
<div className="flex justify-between items-center">
<h1 className="text-3xl font-bold text-gray-900">Admin Dashboard</h1>
<div className="flex gap-4">
<select
value={statusFilter}
onChange={(e) => {
setStatusFilter(e.target.value);
loadArticles(password, e.target.value);
}}
className="px-4 py-2 border rounded-lg"
>
<option value="published">Published</option>
<option value="draft">Draft</option>
</select>
<button
onClick={() => {
sessionStorage.removeItem('adminAuth');
setIsAuthed(false);
}}
className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700"
>
Logout
</button>
</div>
</div>
</div>
</div>
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{message && (
<div className="mb-6 p-4 bg-blue-50 border border-blue-200 rounded-lg">
{message}
</div>
)}
{loading ? (
<div className="text-center py-12">
<div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
<p className="mt-4 text-gray-600">Loading articles...</p>
</div>
) : (
<>
<div className="bg-white rounded-lg shadow overflow-hidden">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">ID</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Title</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Translation</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Views</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Actions</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{articles.map((article) => {
const ratio = translationRatio(article);
return (
<tr key={article.id} className="hover:bg-gray-50">
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{article.id}
</td>
<td className="px-6 py-4 text-sm text-gray-900">
<Link
href={`/article/${article.slug}`}
target="_blank"
className="hover:text-blue-600 hover:underline"
>
{article.title_burmese.substring(0, 80)}...
</Link>
</td>
<td className="px-6 py-4 whitespace-nowrap">
<span className={`px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${getStatusColor(article.status)}`}>
{article.status}
</span>
</td>
<td className="px-6 py-4 whitespace-nowrap">
<span className={`text-sm font-semibold ${getRatioColor(ratio)}`}>
{ratio}%
</span>
<span className="text-xs text-gray-500 ml-2">
({article.burmese_length.toLocaleString()} / {article.content_length.toLocaleString()})
</span>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{article.view_count || 0}
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2">
<Link
href={`/article/${article.slug}`}
target="_blank"
className="text-blue-600 hover:text-blue-900"
>
View
</Link>
{article.status === 'published' ? (
<button
onClick={() => handleAction(article.id, 'unpublish')}
className="text-yellow-600 hover:text-yellow-900"
>
Unpublish
</button>
) : (
<button
onClick={() => handleAction(article.id, 'publish')}
className="text-green-600 hover:text-green-900"
>
Publish
</button>
)}
<button
onClick={() => handleAction(article.id, 'delete')}
className="text-red-600 hover:text-red-900"
>
Delete
</button>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
<div className="mt-6 text-sm text-gray-600">
<p>Showing {articles.length} {statusFilter} articles</p>
<p className="mt-2">
<strong>Translation Quality:</strong>{' '}
<span className="text-green-600">40%+ = Good</span>,{' '}
<span className="text-yellow-600">20-40% = Check</span>,{' '}
<span className="text-red-600">&lt;20% = Poor</span>
</p>
</div>
</>
)}
</div>
</div>
);
}

View File

@@ -0,0 +1,122 @@
// Admin API for managing articles
import { NextRequest, NextResponse } from 'next/server';
import { Pool } from 'pg';
// Simple password auth (you can change this in .env)
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'burmddit2026';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// Helper to check admin auth
function checkAuth(request: NextRequest): boolean {
const authHeader = request.headers.get('authorization');
if (!authHeader) return false;
const password = authHeader.replace('Bearer ', '');
return password === ADMIN_PASSWORD;
}
// GET /api/admin/article - List articles
export async function GET(request: NextRequest) {
if (!checkAuth(request)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const status = searchParams.get('status') || 'published';
const limit = parseInt(searchParams.get('limit') || '50');
try {
const client = await pool.connect();
const result = await client.query(
`SELECT id, title, title_burmese, slug, status,
LENGTH(content) as content_length,
LENGTH(content_burmese) as burmese_length,
published_at, view_count
FROM articles
WHERE status = $1
ORDER BY published_at DESC
LIMIT $2`,
[status, limit]
);
client.release();
return NextResponse.json({ articles: result.rows });
} catch (error) {
console.error('Database error:', error);
return NextResponse.json({ error: 'Database error' }, { status: 500 });
}
}
// POST /api/admin/article - Update article status
export async function POST(request: NextRequest) {
if (!checkAuth(request)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const body = await request.json();
const { articleId, action, reason } = body;
if (!articleId || !action) {
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
}
const client = await pool.connect();
if (action === 'unpublish') {
await client.query(
`UPDATE articles
SET status = 'draft', updated_at = NOW()
WHERE id = $1`,
[articleId]
);
client.release();
return NextResponse.json({
success: true,
message: `Article ${articleId} unpublished`,
reason
});
} else if (action === 'publish') {
await client.query(
`UPDATE articles
SET status = 'published', updated_at = NOW()
WHERE id = $1`,
[articleId]
);
client.release();
return NextResponse.json({
success: true,
message: `Article ${articleId} published`
});
} else if (action === 'delete') {
await client.query(
`DELETE FROM articles WHERE id = $1`,
[articleId]
);
client.release();
return NextResponse.json({
success: true,
message: `Article ${articleId} deleted permanently`
});
}
return NextResponse.json({ error: 'Invalid action' }, { status: 400 });
} catch (error) {
console.error('Database error:', error);
return NextResponse.json({ error: 'Database error' }, { status: 500 });
}
}

View File

@@ -3,6 +3,7 @@ export const dynamic = "force-dynamic"
import { notFound } from 'next/navigation'
import Link from 'next/link'
import Image from 'next/image'
import AdminButton from '@/components/AdminButton'
async function getArticleWithTags(slug: string) {
try {
@@ -267,6 +268,9 @@ export default async function ImprovedArticlePage({ params }: { params: { slug:
</div>
</section>
)}
{/* Admin Button (hidden, press Alt+Shift+A to show) */}
<AdminButton articleId={article.id} articleTitle={article.title_burmese} />
</div>
)
}

View File

@@ -0,0 +1,175 @@
'use client';
import { useState } from 'react';
interface AdminButtonProps {
articleId: number;
articleTitle: string;
}
export default function AdminButton({ articleId, articleTitle }: AdminButtonProps) {
const [showPanel, setShowPanel] = useState(false);
const [isAdmin, setIsAdmin] = useState(false);
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [message, setMessage] = useState('');
// Check if admin mode is enabled (password in sessionStorage)
const checkAdmin = () => {
if (typeof window !== 'undefined') {
const stored = sessionStorage.getItem('adminAuth');
if (stored) {
setIsAdmin(true);
return true;
}
}
return false;
};
const handleAuth = () => {
if (password) {
sessionStorage.setItem('adminAuth', password);
setIsAdmin(true);
setMessage('');
}
};
const handleAction = async (action: string) => {
if (!checkAdmin() && !password) {
setMessage('Please enter admin password');
return;
}
setLoading(true);
setMessage('');
const authToken = sessionStorage.getItem('adminAuth') || password;
try {
const response = await fetch('/api/admin/article', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
},
body: JSON.stringify({
articleId,
action,
reason: action === 'unpublish' ? 'Flagged by admin' : undefined
})
});
const data = await response.json();
if (response.ok) {
setMessage(`${data.message}`);
// Reload page after 1 second
setTimeout(() => {
window.location.reload();
}, 1000);
} else {
setMessage(`${data.error}`);
if (response.status === 401) {
sessionStorage.removeItem('adminAuth');
setIsAdmin(false);
}
}
} catch (error) {
setMessage('❌ Error: ' + error);
} finally {
setLoading(false);
}
};
// Show admin button only when Alt+Shift+A is pressed
if (typeof window !== 'undefined') {
if (!showPanel) {
window.addEventListener('keydown', (e) => {
if (e.altKey && e.shiftKey && e.key === 'A') {
setShowPanel(true);
checkAdmin();
}
});
}
}
if (!showPanel) return null;
return (
<div className="fixed bottom-4 right-4 bg-red-600 text-white p-4 rounded-lg shadow-lg max-w-sm z-50">
<div className="flex justify-between items-start mb-3">
<h3 className="font-bold text-sm">Admin Controls</h3>
<button
onClick={() => setShowPanel(false)}
className="text-white hover:text-gray-200"
>
</button>
</div>
<div className="text-xs mb-3">
<strong>Article #{articleId}</strong><br/>
{articleTitle.substring(0, 50)}...
</div>
{!isAdmin ? (
<div className="mb-3">
<input
type="password"
placeholder="Admin password"
value={password}
onChange={(e) => setPassword(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleAuth()}
className="w-full px-3 py-2 text-sm text-black rounded border"
/>
<button
onClick={handleAuth}
className="w-full mt-2 px-3 py-2 bg-white text-red-600 rounded text-sm font-bold hover:bg-gray-100"
>
Unlock Admin
</button>
</div>
) : (
<div className="space-y-2">
<button
onClick={() => handleAction('unpublish')}
disabled={loading}
className="w-full px-3 py-2 bg-yellow-500 text-black rounded text-sm font-bold hover:bg-yellow-400 disabled:opacity-50"
>
{loading ? 'Processing...' : '🚫 Unpublish (Hide)'}
</button>
<button
onClick={() => handleAction('delete')}
disabled={loading}
className="w-full px-3 py-2 bg-red-800 text-white rounded text-sm font-bold hover:bg-red-700 disabled:opacity-50"
>
{loading ? 'Processing...' : '🗑️ Delete Forever'}
</button>
<button
onClick={() => {
sessionStorage.removeItem('adminAuth');
setIsAdmin(false);
setPassword('');
}}
className="w-full px-3 py-2 bg-gray-700 text-white rounded text-sm hover:bg-gray-600"
>
Lock Admin
</button>
</div>
)}
{message && (
<div className="mt-3 text-xs p-2 bg-white text-black rounded">
{message}
</div>
)}
<div className="mt-3 text-xs text-gray-300">
Press Alt+Shift+A to toggle
</div>
</div>
);
}