Technology

Is the Nexus 4 in stock?

I’m currently a bit obsessed with the LG Nexus 4. The price tag, specs and design seem to be a fairly amazing combination. The trouble is, however, it’s been available in Canada for a grand total of ten minutes from what I can tell.

To combat the problem, a friend reminded me on Twitter that I am software developer, and that I can automate things. I set to work, and arrived at the following solution:

  • A command line script that checks the Nexus 4’s web page to check if its in stock.
  • If it is, this uses a Ruby script called “t” to send me a direct message
  • Direct messages are desirable for me as they reliably get to my phone

Command Line to Check Google Play to See if the Nexus 4 Is In Stock

test $(curl -s https://play.google.com/store/devices/details?id=nexus_4_16gb \
  | tr -d '\n' | grep -vi "sold out" |wc -l) -eq 1 && \
  t dm @yourtwitter "check google play"

The one thing I’m worried about; the machine I’m running this on sits in the US, and I’m concerned that there might be stock for Canadian clients but not US, and vice versa, so I’m running it on my MacBook too for good luck.

To get this to work, your crontab needs to set up a decent Ruby environment; mine looks like this (though I’m certain that this could be improved):

My Crontab

kyleh@vm:~$ crontab -l
rvm_bin_path=/home/kyleh/.rvm/bin
GEM_HOME=/home/kyleh/.rvm/gems/ruby-1.9.3-p194
IRBRC=/home/kyleh/.rvm/rubies/ruby-1.9.3-p194/.irbrc
MY_RUBY_HOME=/home/kyleh/.rvm/rubies/ruby-1.9.3-p194
PATH=/home/kyleh/.rvm/gems/ruby-1.9.3-p194/bin
PATH=$PATH:/home/kyleh/.rvm/gems/ruby-1.9.3-p194@global/bin
PATH=$PATH:/home/kyleh/.rvm/rubies/ruby-1.9.3-p194/bin:/home/kyleh/.rvm/bin
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
PATH=$PATH:/bin
RUBY_VERSION=ruby-1.9.3-p194
GEM_PATH=/home/kyleh/.rvm/gems/ruby-1.9.3-p194
GEM_PATH=$GEM_PATH:/home/kyleh/.rvm/gems/ruby-1.9.3-p194@global
* * * * * test $(curl -s https://play.google.com/store/devices/details?id=nexus_4_16gb \
 | tr -d '\n' | grep -vi "sold out" |wc -l) -eq 1 && \
 t dm @yourtwitter "check google play"

Installing t is simple, just gem install t. The first time you run t, just do t authorize and it will walk you through authorizing t to talk to Twitter for you.

Enjoy!

References

3 thoughts on “Is the Nexus 4 in stock?

    • My script seems to work fine; with the exception that my server is in the US, so I’m getting the occasional false positive, which is annoying. Yours is nice!

  1. By the way, I am now the happy owner of not one but two Nexus 4’s thanks to my misunderstanding of how the cancel order function worked. šŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s