Search code examples
pythoninturllib2smtplib

First python screen scraping and smtp script won't evaluate properly


Here's my script with the personal bits scrubbed.

import urllib, urllib2, cookielib

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp3 = opener.open('https://www.mynexia.com/login')
resp4 = resp3.read().split('input name=\"authenticity_token\" type=\"hidden\" value=\"')
resp5 = resp4[1].split('\" /></div>')
login = 'website username'
password = 'website pass'
authenticity_token = resp5
login_data = urllib.urlencode({'login' : login, 'password' : password,'authenticity_token' : authenticity_token})
opener.open('https://www.mynexia.com/session', login_data)
resp = opener.open('https://www.mynexia.com/houses/ourstaticaccountpage/climate')
resp1 = resp.read().split('<div class=\"temperature\"><span>')
resp2 = resp1[1].split('</span></div>')

int(resp2[0])


if resp2[0] > 75:

    import smtplib
    import string
    SUBJECT = "Temperature is rising!"
    TO = "helpdesk@whoever.blah"
    FROM = "me@gmail.com"
    text = "Temperature is " + resp2[0]
    BODY = string.join((
        "From: %s" % FROM,
        "To: %s" % TO,
        "Subject: %s" % SUBJECT,
        "",
        text
        ), "\r\n")
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login("me@gmail.com", "gmailpass")
    server.sendmail(FROM, [TO], BODY)

elif resp2[0] <= 75:

    import smtplib
    import string
    SUBJECT = "Temperature is ok"
    TO = "helpdesk@whereever.blah"
    FROM = "me@gmail.com"
    text = "Temperature is " + resp2[0]
    BODY = string.join((
        "From: %s" % FROM,
        "To: %s" % TO,
        "Subject: %s" % SUBJECT,
        "",
        text
        ), "\r\n")
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login("me@gmail.com", "gmailpass")
    server.sendmail(FROM, [TO], BODY)

It works fine, except it always evaluates resp2[0] as > 75 no matter what its value is. The point of this script is to alert me when a room that some sensitive machines are running in gets warmer than 75 degrees. The website I'm scraping from only allows you to send alerts if it gets over 90. By then I'm at risk of machines going down, so I wanted to alert myself earlier. I'm going to run it with a cronjob every 15 minutes, and once I get the if-else statement working right, I'm just going to have <= dump to a log file instead of sending out an "Everything is a-ok alert." Any suggestions on why I fail at basic math? Is there a problem with my int(resp2[0])? Is it not base 10 by default?


Solution

  • resp2 is a list of strings. A string is greater than an integer. You need to call int on it before comparison.

    Actually, I see that you are calling int - but you're not doing anything with the result. Doing int(resp2[0]) doesn't convert the contents of resp2[0] to an integer. It simply returns the converted value. If you don't assign it to anything, it just gets thrown away. You need to assign it to a new variable, and then use that variable in your if statements.