Search code examples
pythonapigmail-api

Find a money amount specified in a email and print it next to a str


I've been trying to make an API that automates a detector for new transaction emails in my Gmail Account, and now it detects all the emails coming from my bank that are related to a transaction send to myself, but it prints the whole email (which contains the money expressed like this "$" in a certain point of the email), and I want it that the script detects the money amount and then prints a string with it like this for example

print("Transaction Recieved"," $" ,moneyAmount,"!")

The Messages Filter part of the script

#Messages Filter
        message_count = 50
        for message in messages[:message_count]:
            msg = service.users().messages().get(userId='me', id=message['id']).execute()
            email = (msg['snippet'])
            if "Datos de la transferencia que recibiste Monto $" in email:
                service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()
                print(f'{email}\n')

And I can't figure how to make it so it detects the money amount that is expressed inside the bank's email.

I tried to do an attempt of making it to detect a string pattern of a value of 1 digit that could go from 1 to 9 followed by a "." and after that a value of 3 digits that could go from 001 to 999 and then print the whole value, but it didn't work so I'm out of ideas.

Here's my attempt of making it

#Messages Filter
        message_count = 50
        for message in messages[:message_count]:
            msg = service.users().messages().get(userId='me', id=message['id']).execute()
            email = (msg['snippet'])
            if "Datos de la transferencia que recibiste Monto $" in email:
                value = re.findall(r'$\d{1}.\d{3}', email)
                service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()
                print(f'{email}\n')
                print(f'{value}\n')

The amount of money is displayed like this in the email And this is like directly from the email

If someone with an idea could help me, I would be very grateful.

P.S.:Btw if you see something that you don't understand because of the language, it's because it's in Spanish, but it's mostly in strings so it shouldn't be a problem.


Solution

  • I had to replace the string pattern that I was using for the re and it ended like this

    re.findall(r'\$\d+(?:.\d{3})+(?:,\d+)?', email)
    

    Insted of this

    re.findall(r'$\d{1}.\d{3}', email)
    

    Thanks to @Grismar for the answer