Overview
TheViewer class (core/viewer.py:8) provides comprehensive functionality for parsing decrypted WhatsApp SQLite databases, retrieving chat conversations, and exporting data in multiple formats (HTML, CSV, JSON, TXT).
Class Definition
core/viewer.py
Constructor
__init__()
Initializes the Viewer with a database file path.
Path to the decrypted WhatsApp SQLite database file
db_path: Path to the database fileconn: SQLite connection object (initialized on connect)cursor: SQLite cursor for executing queriescontact_map: Dictionary mapping JIDs to contact display names
Core Methods
connect()
Establishes a connection to the SQLite database and loads contact information.
True if connection succeeded, False otherwise
- Validates database file exists
- Creates SQLite connection
- Automatically loads contacts from
wa_contactstable - Returns success status
list_chats()
Retrieves a list of all chats from the database with metadata.
Maximum number of chats to return. 0 returns all chats.
List of chat dictionaries containing:
id: Chat row ID (int)jid: Chat JID identifier (str)subject: Chat name/subject (str)timestamp: Last activity timestamp (int)
- Modern schema: Uses
chattable withjidtable join - Legacy schema: Falls back to
chattable only
get_messages()
Retrieves messages for a specific chat with sender information and media metadata.
Chat row ID from the database
Chat JID identifier (e.g., “[email protected]”)
Maximum number of messages to return. 0 returns all messages.
List of message dictionaries in chronological order containing:
id: Message row ID (int)text: Display text with media placeholders (str)raw_text: Original message text without media info (str)timestamp: Unix timestamp in milliseconds (int)from_me: Whether message was sent by account owner (bool)sender: Display name or JID of sender (str)jid: Sender’s JID identifier (str)media: Media information dict or None (Dict or None)path: File path to media (str)type: MIME type (str)
- Checks
messagetable (primary, modern schema) - Falls back to
available_message_view(view aggregation) - Falls back to
messagestable (legacy schema)
- Resolves sender JIDs to display names using
contact_map - Extracts sender information from
jidtable for group chats - Handles both individual and group chat formats
get_message_count()
Returns the total number of messages in the database.
Total message count across all chats
export_chats()
Exports chat conversations to various file formats.
Directory where export file will be saved
Export format:
'csv', 'json', 'txt', or 'html'Export only a specific chat by ID. None exports all chats.
- CSV: Flat format with columns: ID, Chat JID, Chat Name, Timestamp, Message Timestamp, Sender, Message
- JSON: Hierarchical format with nested messages in each chat object
- TXT: Human-readable plain text with formatted timestamps
- HTML: WhatsApp-styled web page with bubbles, colors, and media links
- WhatsApp-inspired UI with message bubbles
- Sent/received message styling
- Group chat sender colors (19 distinct colors)
- Date dividers
- Media support (images, videos, audio, documents)
- Sticky chat header
- Mobile responsive design
close()
Closes the database connection.
Internal Methods
_load_contacts()
Loads contact names from the wa_contacts table into the contact_map.
- Checks if
wa_contactstable exists - Detects available name columns (
display_nameorwa_name) - Populates
contact_mapdictionary with JID → name mappings - Called automatically during
connect()
_export_html()
Generates WhatsApp-styled HTML export with embedded CSS.
List of chat dictionaries from
list_chats()Output HTML file path
- Responsive WhatsApp UI design
- Message bubble styling (green for sent, white for received)
- Date dividers between different days
- Group chat sender color coding
- Media embed support (images, videos, audio)
- Sticky header with chat metadata
Database Schema Compatibility
The Viewer supports multiple WhatsApp database schemas: Modern Schema (crypt14/15):chattable withjid_row_idforeign keyjidtable for user/server informationmessagetable withsender_jid_row_idmessage_mediatable for media metadatawa_contactstable for contact names
messagestable withkey_remote_jid- Direct JID strings instead of table joins
- Limited media metadata
Complete Usage Example
Related
Viewing Chats
Learn how to browse WhatsApp conversations
Export Formats
Understanding export options
Database Schema
WhatsApp database structure reference
CryptoManager
Decrypt backups before viewing
