giffgaffbot    Posted:


Last time I wanted to buy new Samsung Galaxy S 5 I liked blue one but there was no preorder option on giffgaff website (it was cheapest there) so to notify me I wrote a bot in pycurl and placed job in cron to send me an email when ot will be in stock.

Cut of sourcefragment from giffgaff webpage:

<form id="frmBuyPhone" action="/samsung/samsung-galaxy-s5/phone-plans" method="post" data-color-memory-mapping='
{"black":{"16":{"price":"517","sku":"GG1SAS5BBN","stock":1,"isLoanAvailable":true,"minMonthlyPayment":2139,"minAPR":970}},
 "white":{"16":{"price":"517","sku":"GG1SAS5WWN","stock":1,"isLoanAvailable":true,"minMonthlyPayment":2139,"minAPR":970}},
 "blue":{"16":{"price":"517","sku":"GG1SAS5BLN","stock":1,"isLoanAvailable":true,"minMonthlyPayment":2139,"minAPR":970}},
 "gold":{"16":{"price":"517","sku":"GG1SAGS5GN","stock":1,"isLoanAvailable":true,"minMonthlyPayment":2139,"minAPR":970}}}'
data-loan-mapping='{"GG1SAS5BBN":{"5000":{"24":{"deal_id":"7049","apr":970,"loan_amount":46700,"monthly_payment":2139,"total_payment":56336},"18":{"deal_id":"7053","apr":1140,"loan_amount":46700,"monthly_payment":2821,"total_payment":55778},"12":{"deal_id":"7057","apr":1470,"loan_amount":46700,"monthly_payment":4187,"total_payment":55244},"6":{"deal_id":"7061","apr":2580,"loan_amount":46700,"monthly_payment":8317,"total_payment":54902}}

The bot looks like this :

 1  #!/usr/bin/env python
 2  import pycurl
 3  import sys
 4  import cStringIO
 5  import re
 6  import os
 7  buf = cStringIO.StringIO()
 8  ch = pycurl.Curl()
 9  ch.setopt(pycurl.VERBOSE, 0)
10  ch.setopt(pycurl.WRITEFUNCTION, buf.write)
11  ch.setopt(pycurl.COOKIEJAR, 'Ocookie.txt')
12  ch.setopt(pycurl.USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; BTRS122288; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.3)")
13  def get_products(url):
14      ch.setopt(pycurl.URL, url)
15      ch.perform()
16      buf.reset()
17      newdata = buf.getvalue()
18      newdata = re.search("data-color-memory-mapping.*", newdata).group()
19      newdata = re.search("blue.*", newdata).group()
20      newdata = re.findall("stock.:([0-9]{0,2})", newdata)
21      newdata = newdata[0]
22      if int(newdata) == 0:
23          print "not in stock"
24      else:
25          os.system("echo 'in stock  %s' | /usr/sbin/sendmail mymail@gmail.com" % newdata)
26          print "in stock"
27 
28  get_products('http://giffgaff.com/samsung/samsung-galaxy-s5/phone-details')

and added job to crontab to check every 5 minutes

*/5 * * * * /workspace/projects/checkbot/readweb.py

Comments