The iMessages Transaction Parser is a Python-based project designed to extract meaningful financial information from iMessage text messages. It dynamically adapts to new message formats by leveraging Grok patterns stored in a database. This project is ideal for personal use to compute bank balances, credit card spendings, and other financial transactions. It also supports community contributions for enhancements and bug fixes.
-
Dynamic Grok Pattern Loading:
- Grok patterns are stored in a database and dynamically loaded at runtime, allowing the system to adapt to new message formats without code changes.
-
Field Extraction:
- Extracts key fields such as
amount,account,upi_id,timestamp, andmerchantfrom text messages.
- Extracts key fields such as
-
Handles Multiple Matches:
- Supports multiple patterns for a single field (e.g.,
amount) and aggregates all matches into a list.
- Supports multiple patterns for a single field (e.g.,
-
Scalable and Maintainable:
- Centralized pattern management in the database ensures scalability and ease of maintenance.
-
Developer-Friendly:
- Designed for the developer community to use, enhance, and contribute.
fastapi-postgres-docker/
├── apis/
│ ├── base.py # API routing
│ ├── v1/
│ │ ├── route_txnPatterns.py # API endpoints for managing Grok patterns
├── business/
│ ├── definitions/
│ │ ├── iMessages.py # Core logic for parsing iMessages
├── db/
│ ├── models/
│ │ ├── grokPatterns.py # Database model for Grok patterns
│ │ ├── txnPatterns.py # Database model for transaction patterns
│ ├── repository/
│ │ ├── grokPatterns.py # Repository for managing Grok patterns
│ │ ├── txnPatterns.py # Repository for managing transaction patterns
│ ├── session.py # Database session management
├── scripts/
│ ├── load_txn_patterns.sh # Shell script to load transaction patterns into the database
├── core/
│ ├── config.py # Logging and configuration
├── README.md # Project documentation
- Python 3.9+
- PostgreSQL
- Docker (optional, for containerized deployment)
-
Clone the Repository:
git clone https://github.com/your-username/imessages-transaction-parser.git cd imessages-transaction-parser -
Set Up a Virtual Environment:
python3 -m venv venv source venv/bin/activate -
Install Dependencies:
pip install -r requirements.txt
-
Set Up the Database:
- Create a PostgreSQL database.
- Update the database connection string in config.py.
-
Run Migrations:
alembic upgrade head
-
Start the Application:
uvicorn main:app --reload
Use the /grokpatterns API endpoint to add Grok patterns for extracting fields.
curl --location 'http://localhost:8000/grokpatterns' \
--header 'Content-Type: application/json' \
--data '{
"field": "amount",
"pattern": "(?:Rs\\.|INR)\\s?(\\d{1,3}(?:,\\d{3})*(?:\\.\\d{1,2})?)",
"description": "Extracts amounts in formats like Rs.1234.56 or INR 1,234.56"
}'Run the load_txn_patterns.sh script to load predefined transaction patterns into the database.
./scripts/load_txn_patterns.shUse the iMessages class to parse iMessages and extract key fields.
from db.session import get_db
from business.definitions.iMessages import iMessages
# Get the database session
db = next(get_db())
# Initialize iMessages
imessages = iMessages(db)
# Get processed messages
processed_messages = imessages.get_messages()
print(processed_messages)- Grok patterns are stored in the
GrokPatterntable. - At runtime, the
iMessagesclass fetches these patterns and applies them to extract fields dynamically.
- The
extract_key_fieldsmethod uses regex patterns to extract fields likeamount,account,upi_id, etc. - Multiple matches for a single field are stored in a list.
- Messages are filtered using transaction patterns stored in the
txnPatternstable.
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and push them to your fork.
- Submit a pull request.
- Follow PEP 8 for Python code.
- Write clear and concise commit messages.
- Add tests for new features or bug fixes.
-
Support for Additional Message Formats:
- Add more Grok patterns to handle diverse message formats.
-
Improved Error Handling:
- Enhance error handling for edge cases.
-
Web Interface:
- Build a web interface for managing patterns and viewing parsed messages.
-
Integration with Financial Tools:
- Export parsed data to financial tools like Excel or budgeting apps.
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by the need for better personal finance tracking tools.
- Thanks to the developer community for their contributions and feedback.