Skip to content

Python command-line tool that automatically forwards emails, intended to make family sharing of streaming accounts more accessible.

License

Notifications You must be signed in to change notification settings

Iscolering/code-extractor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Gmail Code Extractor

A Python command-line tool for automatically extracting code snippets from emails using OpenAI's API. Originally designed for forwarding emails, now enhanced with intelligent code detection and extraction capabilities for developers and teams.

Features

  • ๐Ÿค– AI-Powered Code Extraction - Uses OpenAI to identify and extract code from emails
  • ๐Ÿ“ง Extract code from latest email or search by criteria
  • ๐Ÿ” Search by subject line (perfect for code review emails, patches, etc.)
  • ๐Ÿ“ฎ Search by sender (GitHub, GitLab, code sharing services)
  • ๐Ÿ‘๏ธ Filter unread emails only
  • ๐Ÿ”„ Watch mode for continuous monitoring
  • ๐Ÿ”’ Secure Gmail authentication
  • ๐Ÿ“ค Optional forwarding to team members
  • ๐Ÿ’ป Terminal-friendly code display with syntax highlighting
  • ๐Ÿ›ก๏ธ Regex fallback when AI is unavailable

Installation

  1. Clone the repository:
git clone https://github.com/iscolering/gmail-code-extractor.git
cd gmail-code-extractor
  1. Install required dependencies:
pip install -r requirements.txt
  1. Set up environment variables by creating a .env file:
cp .env.example .env
  1. Edit .env with your credentials:
EMAIL_USERNAME=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
OPENAI_API_KEY=your-openai-api-key
FORWARD_TO=teammate1@email.com,teammate2@email.com  # Optional

Setup Requirements

Gmail Setup

  1. Enable 2-factor authentication on your Gmail account
  2. Generate an App Password:
    • Go to Google Account Settings
    • Security โ†’ 2-Step Verification โ†’ App passwords
    • Generate a new app password for "Mail"
    • Use this password in your .env file

OpenAI Setup

  1. Get an OpenAI API key from OpenAI Platform
  2. Add the key to your .env file as OPENAI_API_KEY
  3. Ensure you have API credits available

Usage

Basic Code Extraction

Extract code from the latest email:

python getCodeCLI.py --code-only

Extract code from unread emails only:

python getCodeCLI.py --unseen --code-only

Search and Extract

Extract code from emails with specific subject:

python getCodeCLI.py --subject "code review" --code-only

Extract code from specific sender:

python getCodeCLI.py --from "noreply@github.com" --code-only

Watch Mode

Continuously monitor for new emails with code:

python getCodeCLI.py --watch 60 --code-only

Monitor specific code-related emails:

python getCodeCLI.py --watch 30 --subject "pull request" --code-only

Use Cases

Perfect for development teams and individual developers:

Code Review Monitoring:

# Extract code from GitHub notifications
python getCodeCLI.py --from "notifications@github.com" --code-only

# Monitor GitLab merge request emails
python getCodeCLI.py --subject "merge request" --watch 300 --code-only

# Extract code snippets from team emails
python getCodeCLI.py --subject "code snippet" --code-only

Development Workflow:

# Monitor code sharing emails
python getCodeCLI.py --watch 120 --subject "code" --code-only

# Extract patches and diffs
python getCodeCLI.py --subject "patch" --code-only

# Monitor Stack Overflow notifications with code
python getCodeCLI.py --from "noreply@stackoverflow.com" --code-only

Team Collaboration:

# Extract code from team discussions
python getCodeCLI.py --unseen --code-only

# Monitor shared code via email
python getCodeCLI.py --watch 60 --from "team@company.com" --code-only

Legacy Forwarding Mode

The tool still supports the original forwarding functionality:

# Forward emails (original behavior)
python getCodeCLI.py --subject "verification code"

# Forward and extract code
python getCodeCLI.py --from "noreply@service.com"

Test Connection

Verify your Gmail credentials:

python getCodeCLI.py --test-login

Command Line Options

Option Description
--code-only Only extract and display code, don't forward emails
--subject TEXT Search for emails with specific subject
--from EMAIL Search for emails from specific sender
--unseen Only search unread emails
--watch SECONDS Watch for new emails (check every N seconds)
--test-login Test IMAP login credentials

Environment Variables

Variable Description Required
EMAIL_USERNAME Your Gmail address Yes
EMAIL_PASSWORD Gmail App Password Yes
OPENAI_API_KEY OpenAI API key for code extraction Yes
FORWARD_TO Comma-separated emails for forwarding No

Code Extraction Features

AI-Powered Detection

  • Uses OpenAI GPT models to intelligently identify code snippets
  • Recognizes various programming languages
  • Handles mixed content (code + text)
  • Preserves code formatting and structure

Fallback Methods

  • Regex patterns for common code block formats
  • Markdown code block detection (code)
  • Inline code detection (code)
  • HTML code tag detection ()
  • Indented code block detection

Output Formatting

  • Clean terminal display with separators
  • Language identification when possible
  • Preserves original code formatting
  • Clear source indication (AI vs Regex)

Requirements

  • Python 3.7+
  • Gmail account with App Password enabled
  • OpenAI API key with available credits
  • Internet connection

Create a requirements.txt file:

python-dotenv
openai>=1.0.0

Security Notes

  • Never commit your .env file to version control
  • Use Gmail App Passwords, not your regular password
  • Keep your OpenAI API key secure
  • Monitor your OpenAI API usage and costs
  • Consider rate limiting for watch mode to avoid excessive API calls

Troubleshooting

Common Issues

OpenAI API Errors:

  • Verify your API key is correct and active
  • Check your OpenAI account has available credits
  • Ensure you're not hitting rate limits

Login Failed Error:

  • Ensure 2FA is enabled on your Gmail account
  • Use an App Password, not your regular password
  • Check that IMAP is enabled in Gmail settings

No Code Found:

  • Try the regex fallback if AI extraction fails
  • Verify the email actually contains code snippets
  • Check if the code is in a supported format

Rate Limiting:

  • Reduce watch frequency to avoid API limits
  • Consider batching requests for multiple emails

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/code-extraction)
  3. Commit your changes (git commit -m 'Add code extraction feature')
  4. Push to the branch (git push origin feature/code-extraction)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues, please open an issue on GitHub.

Changelog

v2.0.0 - Code Extraction Update

  • Added OpenAI API integration for intelligent code extraction
  • Implemented regex fallback for code detection
  • Added --code-only mode
  • Enhanced terminal output formatting
  • Maintained backward compatibility with forwarding features

v1.0.0 - Initial Release

  • Basic email forwarding functionality
  • Search by subject and sender
  • Watch mode for continuous monitoring

About

Python command-line tool that automatically forwards emails, intended to make family sharing of streaming accounts more accessible.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages