Nexus 4 on stock notification

So today the Nexus 4 launched and the F5 spam rained upon Google's servers. Due to several issues with Google's services, I wasn't able to snatch one of these great devices, so I'm stuck here wondering when they will have more available for purchase. Maybe if I had a computer.. that could somehow check for me, whenever an Nexus is available for sale.. hmm.

'''
Nexus Notification Script

This script searches for the specific button in the Google Play store that appears
when the phone is in stock and you are able to buy it. It will send you an email
as a notification.

written by: Marco A Morales
http://www.marcoamorales.com
'''

import smtplib
import requests
import datetime

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
SENDER = 'xxxxxxxxxxx@gmail.com'
PASSWORD = 'xxxxxxxxxxxxx'

test_url = 'http://www.google.com'
test_search_pattern = 'google'

# This URL is for the 16GB version.
url = 'https://play.google.com/store/devices/details?id=nexus_4_16gb'
search_pattern = 'buy-hardware-form'

def check_nexus(url, search_pattern):
    r = requests.get(url)
    if r.text.find(search_pattern) != -1:
        print 'NEXUS FOUND!'
        send_mail(SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD)
    else:
        print 'Nothing found: ' + str(datetime.datetime.now())

def send_mail(smtp_server, smtp_port, sender, password):
    subject = 'NEXUS FOUND'
    body = 'Your script has found a Nexus 4 Device in the Google Play Store.'

    # I send an email from me to me, so I use the same email for sender and recipient
    headers = ["From: " + sender,
           "Subject: " + subject,
           "To: " + sender,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
    headers = "\r\n".join(headers)
    session = smtplib.SMTP(smtp_server, smtp_port)

    session.ehlo()
    session.starttls()
    session.ehlo
    session.login(sender, password)

    session.sendmail(sender, sender, headers + "\r\n\r\n" + body)
    session.quit()

def main():
    check_nexus(url, search_pattern)

if __name__ == '__main__':
    main()

So here it is, a python script I made that will tell you whenever a phone is up for sale. It works by searching for the id of the "Add to cart" button. I'm running this script every minute with cron:

* * * * * /usr/bin/python2.7 ~/NexusChecker.py >> ~/nexus-log.log 2>&1

I hope this will help someone grab a Nexus 4, I'll report back if this script helped me in any way. Happy hunting.

comments powered by Disqus