Expense Tracker is a Python-based application that fetches transaction-related emails, processes the data, and stores it in an SQLite database for generating reports. It helps track expenses efficiently by categorizing transactions and summarizing them in daily, monthly, and yearly reports.
- OAuth Authentication: Securely access Gmail using OAuth2.
- Email Fetching: Fetch unread emails from Gmail based on specific subjects & specific email.
- Data Parsing: Extract transaction details such as amount, date, and note from email content.
- Database Storage: Store transaction data in an SQLite database with indexing for faster queries.
- Reporting:
- Daily, monthly, and yearly transaction summaries.
- Can generate previous month transaction data.
- Can add
--intervaloption to get the transaction data for a specific interval. - Total expense calculation per category.
- Display: Generate reports in a tabular format using the
tabulatelibrary. - Environment Variables: Securely configure sensitive data like email credentials and target subjects.
ExpenseTracker/
├── tracker/
├── __init__.py
├── __version__.py # Version
├── expense_tracker.py # Main application script
├── db.py # SQLiteHandler for database interactions
├── email_fetcher.py # Handles email fetching from Gmail
├── email_parser.py # Parses email content
├── etd.py # Directory looker
├── logs_config.py # Logging configuration
├── display.py # Handles data display
├── gmail_authenticator.py # Gmail OAuth authentication
├── validators.py # Validates user's input
├── tests/ # Unit tests for the application
│ ├── __init__.py
│ ├── test_db.py
│ ├── test_display.py
│ ├── test_email_fetcher.py
│ ├── test_email_parser.py
│ ├── test_etd.py
│ └── test_tracker.py.
│ └── test_validators.py.
│
└── README.md # Project documentation
- Python 3.8+
- Gmail account with API access enabled
- Google Cloud project with OAuth credentials
-
Clone the repository:
git clone https://github.com/vubon/ExpenseTracker.git cd ExpenseTracker -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # For Linux/macOS venv\Scripts\activate # For Windows
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: You need to export env variables
# Mandatory ET_SENDER_EMAIL=your-sender-email@gmail.com # Optional ET_EMAIL_FIELD_RULES={"Amount": {"type": "amount"}, "Note": {"type": "default"},"Date": {"type": "date", "format": "%d %B %Y at %H:%M:%S"}} ET_TARGET_SUBJECT="Payments confirmation,Confirmation of funds transfer,Pay with QR transaction"
-
Set up Google OAuth credentials:
- Create credentials in the Google Cloud Console.
- Download the
credentials.jsonfile and place it at.etdlocation.
-
Start the Expense Tracker:
python expense_tracker.py
-
Reports will be displayed periodically, and unread emails will be processed and marked as read.
You can make a binary file for Linux/macOS using the command below. After building the binary you can move it inside /usr/local/bin location or your favorite location.
pyinstaller --onefile tracker/expense_tracker.py -n etrackerCopy the binary file to /usr/local/bin location
sudo cp dist/etracker /usr/local/binNow you can run the binary file from anywhere in the terminal.
etrackerBut before running the binary file you need to set the environment variables.
Run unit tests using unittest:
python -m unittest discover testsWith coverage:
coverage run -m unittest discover testsTo view the coverage report:
coverage report -mGenerate HTML report:
coverage html- OAuth Credentials: Ensure
credentials.jsonandtoken.pickleare stored in a secure directory outside the codebase. - Avoid hard-coding sensitive information in the source code.
google-authfor Gmail OAuth authenticationsqlite3for database storagetabulatefor displaying data in tabular format
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to fork the repository, create issues, or submit pull requests to contribute to the project.
- Google Gmail API documentation
- Python SQLite documentation