feat: P4 metrics collector + /api/metrics; P5 from-url generator (cheerio)

This commit is contained in:
Nik (Claude)
2026-06-08 11:08:59 +03:00
parent 008323fa74
commit 771f964370
7 changed files with 837 additions and 0 deletions
+195
View File
@@ -0,0 +1,195 @@
/**
* fromUrl.js — парсинг URL и генерация поста по содержимому страницы.
*
* Поддерживаемые источники:
* 1. Любая веб-страница — cheerio, og-meta + основной текст
* 2. YouTube — title + description (без транскрипта, yt-dlp не нужен)
* 3. t.me публичный пост — текст сообщения
*/
const axios = require('axios');
const cheerio = require('cheerio');
const ai = require('./ai');
const pb = require('./promptBuilder');
const FETCH_TIMEOUT = 12_000;
const MAX_TEXT_LEN = 4000; // лимит текста для промта
// ── Парсеры ───────────────────────────────────────────────────────────────────
/**
* YouTube: title + description из yt-initial-data или og-meta
*/
async function parseYoutube(url) {
const res = await axios.get(url, {
timeout: FETCH_TIMEOUT,
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1)' },
maxRedirects: 5,
});
const $ = cheerio.load(res.data);
const title = $('meta[name="title"]').attr('content')
|| $('meta[property="og:title"]').attr('content')
|| $('title').text();
const description = $('meta[name="description"]').attr('content')
|| $('meta[property="og:description"]').attr('content')
|| '';
// Пробуем вытащить chapters / chapters из начальных данных
let chapters = '';
const dataMatch = res.data.match(/"chapters":\s*\[([^\]]{1,3000})\]/);
if (dataMatch) {
try {
const arr = JSON.parse('[' + dataMatch[1] + ']');
chapters = arr.map(c => c.title?.simpleText || '').filter(Boolean).join(', ');
} catch {}
}
const imageUrl = $('meta[property="og:image"]').attr('content') || null;
const text = [title, description, chapters ? `Главы: ${chapters}` : '']
.filter(Boolean).join('\n\n').slice(0, MAX_TEXT_LEN);
return { title, text, imageUrl, source: 'youtube' };
}
/**
* t.me публичный пост (embed)
*/
async function parseTelegram(url) {
// Конвертируем t.me/channel/123 → embed
const embedUrl = url.replace('https://t.me/', 'https://t.me/') + '?embed=1&mode=tme';
const res = await axios.get(embedUrl, {
timeout: FETCH_TIMEOUT,
headers: { 'User-Agent': 'Mozilla/5.0' },
});
const $ = cheerio.load(res.data);
const text = $('.tgme_widget_message_text').text().trim()
|| $('meta[property="og:description"]').attr('content')
|| '';
const title = $('meta[property="og:title"]').attr('content') || 'Telegram пост';
const imageUrl = $('meta[property="og:image"]').attr('content') || null;
return { title, text: text.slice(0, MAX_TEXT_LEN), imageUrl, source: 'telegram' };
}
/**
* Универсальная веб-страница
*/
async function parseWeb(url) {
const res = await axios.get(url, {
timeout: FETCH_TIMEOUT,
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Accept-Language': 'ru-RU,ru;q=0.9,en;q=0.8',
},
maxRedirects: 5,
});
const $ = cheerio.load(res.data);
// Убираем мусор
$('script, style, nav, footer, header, aside, form, .cookie, .banner, .popup, .ad').remove();
const title = $('meta[property="og:title"]').attr('content')
|| $('meta[name="title"]').attr('content')
|| $('h1').first().text().trim()
|| $('title').text().trim();
const description = $('meta[property="og:description"]').attr('content')
|| $('meta[name="description"]').attr('content')
|| '';
const imageUrl = $('meta[property="og:image"]').attr('content') || null;
// Основной текст: article > p, или просто все параграфы
const paragraphs = [];
const container = $('article, main, .content, .post, .entry, [role="main"]').first();
const source = container.length ? container : $('body');
source.find('p, h2, h3, li').each((_, el) => {
const t = $(el).text().trim();
if (t.length > 40) paragraphs.push(t);
});
const bodyText = paragraphs.join('\n').slice(0, MAX_TEXT_LEN);
const text = [description, bodyText].filter(Boolean).join('\n\n').slice(0, MAX_TEXT_LEN);
return { title, text, imageUrl, source: 'web' };
}
// ── Роутер источников ─────────────────────────────────────────────────────────
async function parseUrl(url) {
try {
const u = new URL(url);
if (u.hostname.includes('youtube.com') || u.hostname.includes('youtu.be')) {
return await parseYoutube(url);
}
if (u.hostname === 't.me') {
return await parseTelegram(url);
}
return await parseWeb(url);
} catch (err) {
throw new Error(`Не удалось загрузить страницу: ${err.message}`);
}
}
// ── Генерация поста по распарсенному контенту ─────────────────────────────────
async function generateFromUrl({ url, channelId, channel }) {
if (!url) throw new Error('url required');
if (!channel) throw new Error('channel required');
// 1. Парсим страницу
const parsed = await parseUrl(url);
if (!parsed.text && !parsed.title) {
throw new Error('Не удалось извлечь текст со страницы');
}
// 2. Строим промт
const channelContext = pb.buildPostSystemPrompt(channel, '');
const userPrompt = `На основе материала ниже напиши пост для Telegram-канала в стиле этого канала.
ИСТОЧНИК: ${parsed.source === 'youtube' ? 'YouTube-видео' : parsed.source === 'telegram' ? 'Telegram-пост' : 'Веб-статья'}
URL: ${url}
ЗАГОЛОВОК:
${parsed.title || '—'}
СОДЕРЖАНИЕ:
${parsed.text || '(текст не извлечён, опирайся на заголовок)'}
---
ЗАДАЧА:
— Напиши пост в стиле и голосе канала
— Передай суть материала своими словами, не пересказывай дословно
— Добавь свой угол зрения или вывод
— Длина поста: 150–500 символов
— Верни ТОЛЬКО текст поста, без пояснений`;
// 3. Генерируем
const result = await ai.chat(
require('../config').ai.models.post,
channelContext,
userPrompt,
{ maxTokens: 1000, temperature: 0.85 }
);
return {
content: result.text,
title: parsed.title,
imageUrl: parsed.imageUrl,
source: parsed.source,
usage: result.usage,
};
}
module.exports = { generateFromUrl, parseUrl };
+162
View File
@@ -0,0 +1,162 @@
/**
* metricsCollector.js
* Воркер сбора метрик для опубликованных постов.
*
* Что умеет:
* - getMessageReactionCount (Bot API 7+) — реакции на пост
* - forwards — getForwardCount (Bot API 8+, если доступен)
* - views — в Bot API недоступны напрямую; оставляем 0 до MTProto
*
* Запуск: каждые 15 минут через setInterval (из index.js)
* или вручную: POST /api/metrics/collect
*/
const axios = require('axios');
const { query } = require('../config/db');
const COLLECT_WINDOW_DAYS = 30; // собираем метрики для постов за последние N дней
async function getTgApiBase() {
try {
const { rows } = await query(`SELECT value FROM app_settings WHERE key='TELEGRAM_API_BASE'`);
return rows[0]?.value?.replace(/\/$/, '') || 'https://api.telegram.org';
} catch { return 'https://api.telegram.org'; }
}
/**
* Собрать реакции для одного поста.
* Возвращает { reactions: {emoji: count, ...}, forwards: 0, views: 0 }
*/
async function collectForPost({ botToken, tgChannelId, tgMessageId, tgApiBase }) {
const result = { reactions: {}, forwards: 0, views: 0 };
if (!botToken || !tgChannelId || !tgMessageId) return result;
try {
const url = `${tgApiBase}/bot${botToken}/getMessageReactionCount`;
const res = await axios.get(url, {
params: { chat_id: tgChannelId, message_id: tgMessageId },
timeout: 8000,
});
if (res.data?.ok && Array.isArray(res.data.result?.reactions)) {
for (const r of res.data.result.reactions) {
const emoji = r.type?.emoji || r.type?.custom_emoji_id || '?';
result.reactions[emoji] = (result.reactions[emoji] || 0) + (r.count || 0);
}
}
} catch (e) {
// 400 = реакции не включены или пост не найден — не критично
if (e.response?.status !== 400) {
console.warn('[Metrics] getMessageReactionCount error:', e.message);
}
}
return result;
}
/**
* Основная функция сбора метрик.
* Проходит по posts (системные каналы) за последние COLLECT_WINDOW_DAYS дней.
*/
async function collectMetrics() {
const tgApiBase = await getTgApiBase();
const since = new Date(Date.now() - COLLECT_WINDOW_DAYS * 86400_000);
// Берём посты с tg_message_id за последние N дней
const { rows: posts } = await query(`
SELECT p.id, p.tg_message_id, p.channel_id, p.published_at,
c.bot_token, c.tg_channel_id
FROM posts p
JOIN channels c ON c.id = p.channel_id
WHERE p.tg_message_id IS NOT NULL
AND p.published_at > $1
AND c.platform = 'telegram'
AND c.bot_token IS NOT NULL
ORDER BY p.published_at DESC
LIMIT 100
`, [since]);
let updated = 0;
for (const post of posts) {
try {
const metrics = await collectForPost({
botToken: post.bot_token,
tgChannelId: post.tg_channel_id,
tgMessageId: post.tg_message_id,
tgApiBase,
});
const totalReactions = Object.values(metrics.reactions).reduce((s, v) => s + v, 0);
// Обновляем posts — последний снапшот
await query(`
UPDATE posts
SET reactions=$1, forwards=$2, metrics_at=NOW()
WHERE id=$3
`, [JSON.stringify(metrics.reactions), metrics.forwards, post.id]);
// Пишем в историю только если есть хоть что-то
if (totalReactions > 0 || metrics.forwards > 0) {
await query(`
INSERT INTO post_metrics (post_id, captured_at, views, forwards, reactions)
VALUES ($1, NOW(), $2, $3, $4)
`, [post.id, metrics.views, metrics.forwards, JSON.stringify(metrics.reactions)]);
}
updated++;
} catch (e) {
console.error('[Metrics] post', post.id, 'error:', e.message);
}
}
// user_posts — пользовательские посты с tg_message_id
const { rows: userPosts } = await query(`
SELECT up.id, up.tg_message_id, up.channel_id,
c.bot_token, c.tg_channel_id
FROM user_posts up
JOIN channels c ON c.id = up.channel_id
WHERE up.tg_message_id IS NOT NULL
AND up.published_at > $1
AND c.platform = 'telegram'
AND c.bot_token IS NOT NULL
ORDER BY up.published_at DESC
LIMIT 50
`, [since]);
for (const post of userPosts) {
try {
const metrics = await collectForPost({
botToken: post.bot_token,
tgChannelId: post.tg_channel_id,
tgMessageId: post.tg_message_id,
tgApiBase,
});
await query(`
UPDATE user_posts
SET reactions=$1, forwards=$2, metrics_at=NOW()
WHERE id=$3
`, [JSON.stringify(metrics.reactions), metrics.forwards, post.id]);
updated++;
} catch (e) {
console.error('[Metrics] user_post', post.id, 'error:', e.message);
}
}
console.log(`[Metrics] Collected for ${updated} posts`);
return { updated };
}
// Авто-запуск каждые 15 минут
let _timer = null;
function startAutoCollect() {
if (_timer) return;
_timer = setInterval(() => {
collectMetrics().catch(e => console.error('[Metrics] auto-collect error:', e.message));
}, 15 * 60 * 1000);
// Первый запуск через 30 секунд после старта
setTimeout(() => collectMetrics().catch(e => console.error('[Metrics] init error:', e.message)), 30_000);
console.log('[Metrics] Auto-collect started (every 15 min)');
}
module.exports = { collectMetrics, startAutoCollect };