0

I'm using this code to login to my gmail account and retrieve text from an email from steampowered.

else:
      g = gmail.login(emailAddress, emailPassword)
      # Check for emails until we get Steam Guard code
      for i in range(0, 3):
        mails = g.inbox().mail(sender="noreply@steampowered.com", unread=True)
        if mails:
          mail = mails[-1]
          mail.fetch()
          #print re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)
          guardCode = re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)[0]
          mail.read()
          mail.delete()
          g.logout()
          return guardCode
        else:
          time.sleep(3)

I get a

IndexError: List Index out of Range

At this line:

guardCode = re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)[0]

This is because

re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)

Returns

[]

So the list is empty. Why is it empty?

This is the format of the email:

Are you logging into Steam using a new browser or Steam app? Here’s the Steam Guard code you’ll need to complete the process: XXXXX <---- I need this

EDIT: Gmail is a custom python module from here. EDIT #2: Here's a better representation of the email:

Dear XXX,

Are you logging into Steam using a new browser or Steam app? Here's the Steam Guard code you'll need to complete the process:

XXXXX

If you haven’t recently tried to login to Steam from the device located at XXXX (US), someone else may be trying to access your account. You can view more info about this login attempt online.

If you suspect someone else may be attempting to access your account, please either:

Vishwa Iyer
  • 781
  • 5
  • 13
  • 32

2 Answers2

2

Problem is because of in you’ll. It isn't a normal single quote '.

>>> s = "Are you logging into Steam using a new browser or Steam app? Here’s the Steam Guard code you’ll need to complete the process: XXXXX"
>>> re.findall(r"you’ll need to complete the process: ([A-Z0-9]{5})", s)
['XXXXX']
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
0

I solved my problem. I just ended up removing all line breaks and making the email body text as one entire string. Thanks to everyone who helped!

str = re.sub('\s+',' ', mail.body)
guardCode = re.findall(r'to complete the process: ([A-Z0-9]{5})', str)[0]
Vishwa Iyer
  • 781
  • 5
  • 13
  • 32