I want to know when the Nexus 4 is available again. So I decided to hack together a script to email me (and a friend) when it launches. I'm relying on Google removing the words "Sold out". As I said, it's a hack. Alternatively, I guess I could have stored a snapshot of the page's source code and compared for changes.
#!/usr/bin/python
#Script by Rishab Arora (spacetime)
from urllib import urlopen
import smtplib
import logging
sender = 'my.email.id@gmail.com'
recipients = ['me@gmail.com', 'friend@abc.com', 'someone@gmail.com']
subject = 'Nexus 4 back in stock!'
body = 'Check Google Play Store!'
contents = urlopen("https://play.google.com/store/devices/details?id=nexus_4_16gb").read()
if contents.find("buy-hardware-button") != -1: #Found magic button!
message = "\r\n".join(["From: " + sender,
"Subject: " + subject,
"To: " + ", ".join(recipients),
"MIME-Version: 1.0",
"Content-Type: text/html",
body])
try:
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login('my.email.id@gmail.com', 'secretpassword')
session.sendmail(sender, recipients, message)
session.quit()
except Exception:
print logging.exception('')
Added this in my crontab:
*/5 * * * * python /home/spacetime/nexus.py
Which basically runs the script every 5 minutes (Too much? ) on my US-based VPS.
Important: Do test the script at least once if you are using Gmail's SMTP server! One way you can test the script is by changing
if contents.find("buy-hardware-button") != -1:
to
if contents.find("buy-hardware-button") == -1:
And running it. An easier (and more accurate) way would be to change the URL to, say, Nexus 7 16 GB. (Remember to chmod +x) The first time you run it, you will see something resembling:
SMTPAuthenticationError: (535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 https://support.google.com/mail/bin/answe
r.py?answer=78754 abcdefgh.4')
Log into your account from a browser and there will be a notification at the top with steps on how to allow the script to work.
Edit 1: The script now looks for "buy-hardware-button" which appears when you can buy stuff. Thanks to Anarcie on reddit.
Edit 2: I can add a few names to my script, fill this Google Form
Edit 3: Yes, I realize the script spams you till I switch it off. That's a design decision. ;) I want my phone to keep ringing till I read it. You can mark the email as spam when you're done and it won't bother you. About 150 people on the alert list! =)
If you like/hate it or notice any problems, please leave a comment!!!
If you wish to see the sendmail version I'm using: click here for the github gist.
Comments !