Files
whatsappNucleo/scripts/scrape-baileys-docs.ts
josedario87 9f2f3ac510
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m3s
Docs: Script para scrapear documentacion de Baileys API
- Script en scripts/scrape-baileys-docs.ts
- Genera docs/baileys-api-reference.md con 6433 lineas
- 79 secciones: interfaces, types, functions, variables, enums
- Referencia completa para desarrollo de mensajes
2025-12-02 20:49:59 -06:00

301 lines
13 KiB
TypeScript

/**
* Script to scrape Baileys documentation from baileys.wiki
* Creates a comprehensive markdown file for Claude Code reference
*/
import * as fs from 'fs'
const BASE_URL = 'https://baileys.wiki/docs/api'
interface DocSection {
name: string
path: string
type: 'interface' | 'type' | 'function' | 'variable' | 'class' | 'enum'
}
// Relevant documentation sections to scrape
const SECTIONS: DocSection[] = [
// Interfaces
{ name: 'Contact', path: '/interfaces/Contact', type: 'interface' },
{ name: 'GroupMetadata', path: '/interfaces/GroupMetadata', type: 'interface' },
{ name: 'GroupModificationResponse', path: '/interfaces/GroupModificationResponse', type: 'interface' },
{ name: 'PresenceData', path: '/interfaces/PresenceData', type: 'interface' },
{ name: 'BaileysEventEmitter', path: '/interfaces/BaileysEventEmitter', type: 'interface' },
{ name: 'WAUrlInfo', path: '/interfaces/WAUrlInfo', type: 'interface' },
{ name: 'RecentMessage', path: '/interfaces/RecentMessage', type: 'interface' },
// Type Aliases - Chat
{ name: 'Chat', path: '/type-aliases/Chat', type: 'type' },
{ name: 'ChatModification', path: '/type-aliases/ChatModification', type: 'type' },
{ name: 'ChatUpdate', path: '/type-aliases/ChatUpdate', type: 'type' },
{ name: 'ChatMutation', path: '/type-aliases/ChatMutation', type: 'type' },
// Type Aliases - Messages
{ name: 'WAMessage', path: '/type-aliases/WAMessage', type: 'type' },
{ name: 'WAMessageContent', path: '/type-aliases/WAMessageContent', type: 'type' },
{ name: 'WAMessageKey', path: '/type-aliases/WAMessageKey', type: 'type' },
{ name: 'WAMessageUpdate', path: '/type-aliases/WAMessageUpdate', type: 'type' },
{ name: 'MessageType', path: '/type-aliases/MessageType', type: 'type' },
{ name: 'MessageUpsertType', path: '/type-aliases/MessageUpsertType', type: 'type' },
{ name: 'AnyMessageContent', path: '/type-aliases/AnyMessageContent', type: 'type' },
{ name: 'AnyMediaMessageContent', path: '/type-aliases/AnyMediaMessageContent', type: 'type' },
{ name: 'AnyRegularMessageContent', path: '/type-aliases/AnyRegularMessageContent', type: 'type' },
{ name: 'MessageGenerationOptions', path: '/type-aliases/MessageGenerationOptions', type: 'type' },
{ name: 'MessageContentGenerationOptions', path: '/type-aliases/MessageContentGenerationOptions', type: 'type' },
{ name: 'MinimalMessage', path: '/type-aliases/MinimalMessage', type: 'type' },
// Type Aliases - Media
{ name: 'MediaType', path: '/type-aliases/MediaType', type: 'type' },
{ name: 'MediaDownloadOptions', path: '/type-aliases/MediaDownloadOptions', type: 'type' },
{ name: 'WAMediaUpload', path: '/type-aliases/WAMediaUpload', type: 'type' },
{ name: 'DownloadableMessage', path: '/type-aliases/DownloadableMessage', type: 'type' },
{ name: 'MediaGenerationOptions', path: '/type-aliases/MediaGenerationOptions', type: 'type' },
// Type Aliases - Groups
{ name: 'GroupParticipant', path: '/type-aliases/GroupParticipant', type: 'type' },
{ name: 'GroupInviteInfo', path: '/type-aliases/GroupInviteInfo', type: 'type' },
{ name: 'ParticipantAction', path: '/type-aliases/ParticipantAction', type: 'type' },
// Type Aliases - Presence & Status
{ name: 'WAPresence', path: '/type-aliases/WAPresence', type: 'type' },
{ name: 'ConnectionState', path: '/type-aliases/ConnectionState', type: 'type' },
// Type Aliases - Events
{ name: 'BaileysEventMap', path: '/type-aliases/BaileysEventMap', type: 'type' },
{ name: 'BaileysEvent', path: '/type-aliases/BaileysEvent', type: 'type' },
// Type Aliases - Auth
{ name: 'AuthenticationCreds', path: '/type-aliases/AuthenticationCreds', type: 'type' },
{ name: 'AuthenticationState', path: '/type-aliases/AuthenticationState', type: 'type' },
// Type Aliases - Socket
{ name: 'WASocket', path: '/type-aliases/WASocket', type: 'type' },
{ name: 'SocketConfig', path: '/type-aliases/SocketConfig', type: 'type' },
{ name: 'UserFacingSocketConfig', path: '/type-aliases/UserFacingSocketConfig', type: 'type' },
// Functions - Messages
{ name: 'generateWAMessage', path: '/functions/generateWAMessage', type: 'function' },
{ name: 'generateWAMessageContent', path: '/functions/generateWAMessageContent', type: 'function' },
{ name: 'generateWAMessageFromContent', path: '/functions/generateWAMessageFromContent', type: 'function' },
{ name: 'extractMessageContent', path: '/functions/extractMessageContent', type: 'function' },
{ name: 'getContentType', path: '/functions/getContentType', type: 'function' },
{ name: 'normalizeMessageContent', path: '/functions/normalizeMessageContent', type: 'function' },
// Functions - Media
{ name: 'downloadMediaMessage', path: '/functions/downloadMediaMessage', type: 'function' },
{ name: 'downloadContentFromMessage', path: '/functions/downloadContentFromMessage', type: 'function' },
{ name: 'prepareWAMessageMedia', path: '/functions/prepareWAMessageMedia', type: 'function' },
{ name: 'extractImageThumb', path: '/functions/extractImageThumb', type: 'function' },
{ name: 'generateThumbnail', path: '/functions/generateThumbnail', type: 'function' },
{ name: 'generateProfilePicture', path: '/functions/generateProfilePicture', type: 'function' },
{ name: 'getMediaKeys', path: '/functions/getMediaKeys', type: 'function' },
{ name: 'encryptedStream', path: '/functions/encryptedStream', type: 'function' },
// Functions - JID
{ name: 'jidDecode', path: '/functions/jidDecode', type: 'function' },
{ name: 'jidEncode', path: '/functions/jidEncode', type: 'function' },
{ name: 'jidNormalizedUser', path: '/functions/jidNormalizedUser', type: 'function' },
{ name: 'isJidGroup', path: '/functions/isJidGroup', type: 'function' },
{ name: 'isJidBroadcast', path: '/functions/isJidBroadcast', type: 'function' },
{ name: 'isJidStatusBroadcast', path: '/functions/isJidStatusBroadcast', type: 'function' },
{ name: 'areJidsSameUser', path: '/functions/areJidsSameUser', type: 'function' },
{ name: 'isLidUser', path: '/functions/isLidUser', type: 'function' },
// Functions - History
{ name: 'downloadAndProcessHistorySyncNotification', path: '/functions/downloadAndProcessHistorySyncNotification', type: 'function' },
{ name: 'processHistoryMessage', path: '/functions/processHistoryMessage', type: 'function' },
{ name: 'getHistoryMsg', path: '/functions/getHistoryMsg', type: 'function' },
// Functions - Socket
{ name: 'makeWASocket', path: '/functions/makeWASocket', type: 'function' },
{ name: 'makeCacheableSignalKeyStore', path: '/functions/makeCacheableSignalKeyStore', type: 'function' },
{ name: 'useMultiFileAuthState', path: '/functions/useMultiFileAuthState', type: 'function' },
// Functions - Utility
{ name: 'delay', path: '/functions/delay', type: 'function' },
{ name: 'toNumber', path: '/functions/toNumber', type: 'function' },
{ name: 'toBuffer', path: '/functions/toBuffer', type: 'function' },
{ name: 'unixTimestampSeconds', path: '/functions/unixTimestampSeconds', type: 'function' },
// Variables
{ name: 'WAMessageStatus', path: '/variables/WAMessageStatus', type: 'variable' },
{ name: 'WAMessageStubType', path: '/variables/WAMessageStubType', type: 'variable' },
{ name: 'S_WHATSAPP_NET', path: '/variables/S_WHATSAPP_NET', type: 'variable' },
{ name: 'Browsers', path: '/variables/Browsers', type: 'variable' },
{ name: 'MEDIA_KEYS', path: '/variables/MEDIA_KEYS', type: 'variable' },
{ name: 'DEFAULT_CONNECTION_CONFIG', path: '/variables/DEFAULT_CONNECTION_CONFIG', type: 'variable' },
// Enums
{ name: 'DisconnectReason', path: '/enumerations/DisconnectReason', type: 'enum' },
]
async function fetchPage(url: string): Promise<string> {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.status}`)
}
return response.text()
}
function extractContent(html: string, name: string, type: string): string {
// Remove script tags and style tags
html = html.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
html = html.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
html = html.replace(/<nav[^>]*>[\s\S]*?<\/nav>/gi, '')
html = html.replace(/<footer[^>]*>[\s\S]*?<\/footer>/gi, '')
html = html.replace(/<header[^>]*>[\s\S]*?<\/header>/gi, '')
html = html.replace(/<aside[^>]*>[\s\S]*?<\/aside>/gi, '')
// Extract main/article content
let mainMatch = html.match(/<main[^>]*>([\s\S]*?)<\/main>/i)
if (!mainMatch) {
mainMatch = html.match(/<article[^>]*>([\s\S]*?)<\/article>/i)
}
const content = mainMatch ? mainMatch[1] : html
// Convert HTML to markdown-like text
let text = content
// Headers
.replace(/<h1[^>]*>([\s\S]*?)<\/h1>/gi, '\n# $1\n')
.replace(/<h2[^>]*>([\s\S]*?)<\/h2>/gi, '\n## $1\n')
.replace(/<h3[^>]*>([\s\S]*?)<\/h3>/gi, '\n### $1\n')
.replace(/<h4[^>]*>([\s\S]*?)<\/h4>/gi, '\n#### $1\n')
// Code blocks
.replace(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/gi, '\n```typescript\n$1\n```\n')
.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, '`$1`')
// Lists
.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, '- $1\n')
.replace(/<ul[^>]*>/gi, '\n')
.replace(/<\/ul>/gi, '\n')
// Paragraphs
.replace(/<p[^>]*>([\s\S]*?)<\/p>/gi, '\n$1\n')
// Links - keep only text for cleaner output
.replace(/<a[^>]*>([\s\S]*?)<\/a>/gi, '$1')
// Bold/Italic
.replace(/<strong[^>]*>([\s\S]*?)<\/strong>/gi, '**$1**')
.replace(/<em[^>]*>([\s\S]*?)<\/em>/gi, '*$1*')
// Line breaks
.replace(/<br\s*\/?>/gi, '\n')
// Divs and spans
.replace(/<div[^>]*>/gi, '\n')
.replace(/<\/div>/gi, '\n')
.replace(/<span[^>]*>/gi, '')
.replace(/<\/span>/gi, '')
// Tables (simplified)
.replace(/<table[^>]*>/gi, '\n')
.replace(/<\/table>/gi, '\n')
.replace(/<tr[^>]*>/gi, '')
.replace(/<\/tr>/gi, '\n')
.replace(/<td[^>]*>([\s\S]*?)<\/td>/gi, '| $1 ')
.replace(/<th[^>]*>([\s\S]*?)<\/th>/gi, '| **$1** ')
// Remove remaining tags
.replace(/<[^>]+>/g, '')
// Decode HTML entities
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&nbsp;/g, ' ')
// Clean up whitespace
.replace(/\n\s*\n\s*\n/g, '\n\n')
.replace(/^\s+|\s+$/g, '')
return text
}
async function scrapeSection(section: DocSection): Promise<string> {
const url = `${BASE_URL}${section.path}`
console.log(`Fetching ${section.name}...`)
try {
const html = await fetchPage(url)
const content = extractContent(html, section.name, section.type)
return `
---
## ${section.type.charAt(0).toUpperCase() + section.type.slice(1)}: ${section.name}
**Source:** ${url}
${content}
`
} catch (error) {
console.error(`Error fetching ${section.name}:`, (error as Error).message)
return `
---
## ${section.type.charAt(0).toUpperCase() + section.type.slice(1)}: ${section.name}
**Source:** ${url}
*Error: Could not fetch documentation*
`
}
}
async function main() {
console.log('Starting Baileys documentation scrape...\n')
const markdown: string[] = [
`# Baileys API Documentation Reference
> Auto-generated documentation for WhatsApp Nucleo development
> Source: https://baileys.wiki
> Generated: ${new Date().toISOString()}
This document contains the relevant Baileys API documentation for developing the WhatsApp Nucleo messaging features.
## Table of Contents
### Interfaces
${SECTIONS.filter(s => s.type === 'interface').map(s => `- [${s.name}](#interface-${s.name.toLowerCase()})`).join('\n')}
### Type Aliases
${SECTIONS.filter(s => s.type === 'type').map(s => `- [${s.name}](#type-${s.name.toLowerCase()})`).join('\n')}
### Functions
${SECTIONS.filter(s => s.type === 'function').map(s => `- [${s.name}](#function-${s.name.toLowerCase()})`).join('\n')}
### Variables
${SECTIONS.filter(s => s.type === 'variable').map(s => `- [${s.name}](#variable-${s.name.toLowerCase()})`).join('\n')}
### Enumerations
${SECTIONS.filter(s => s.type === 'enum').map(s => `- [${s.name}](#enum-${s.name.toLowerCase()})`).join('\n')}
`
]
// Group sections by type
const groupedSections = {
interface: SECTIONS.filter(s => s.type === 'interface'),
type: SECTIONS.filter(s => s.type === 'type'),
function: SECTIONS.filter(s => s.type === 'function'),
variable: SECTIONS.filter(s => s.type === 'variable'),
enum: SECTIONS.filter(s => s.type === 'enum'),
}
// Process each group
for (const [type, sections] of Object.entries(groupedSections)) {
markdown.push(`\n# ${type.charAt(0).toUpperCase() + type.slice(1)}s\n`)
for (const section of sections) {
const content = await scrapeSection(section)
markdown.push(content)
// Small delay to be nice to the server
await new Promise(r => setTimeout(r, 300))
}
}
// Write to file
const outputPath = './docs/baileys-api-reference.md'
fs.writeFileSync(outputPath, markdown.join('\n'))
console.log(`\nDocumentation saved to ${outputPath}`)
console.log(`Total sections: ${SECTIONS.length}`)
}
main().catch(console.error)