daily pastebin goal
15%
SHARE
TWEET

Gearbest.com price tracking script

Masoko Feb 4th, 2016 (edited) 2,628 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import web, json, urllib, urllib2
  3. from bs4 import BeautifulSoup
  4. import os
  5. import os.path
  6. from termcolor import colored
  7. from random import randint
  8. from time import sleep
  9.  
  10. dir = "/home/pi/scripts/pythontest/db-files/" #folder where the DB files will be saved
  11.  
  12. # configure smtp server for sending mail
  13. web.config.smtp_server = 'smtp.gmail.com'
  14. web.config.smtp_port = 587
  15. web.config.smtp_username = 'username@gmail.com'
  16. web.config.smtp_password = 'password'
  17. web.config.smtp_starttls = True
  18.  
  19. headers = { 'User-Agent' : 'Mozilla/5.0' } # headers for the url download request
  20.  
  21. url_list = []
  22. url_list.append("http://www.gearbest.com/batteries-chargers/pp_140785.html")  #usb led light
  23. url_list.append("http://www.gearbest.com/electronics-gadgets/pp_280338.html") # enhanced xiaomi led light
  24. url_list.append("http://www.gearbest.com/other-classic-toys/pp_271768.html") # Mitko magnetic cubs
  25. url_list.append("http://www.gearbest.com/tv-box-mini-pc/pp_284266.html") # Intel WiDi Display Dongle
  26. url_list.append("http://www.gearbest.com/mobile-power-bank/pp_187685.html") # Mi 10k Ah Battery
  27. url_list.append("http://www.gearbest.com/tv-box-mini-pc/pp_238050.html") # pipo x8 64 gb
  28. url_list.append("http://www.gearbest.com/speakers/pp_228241.html") # MPow Bluetooth Speaker
  29.  
  30. def price_check(check_url):
  31. #build the request for the page that will be checked
  32.     req = urllib2.Request(check_url, None, headers)
  33. #reads the html
  34.     html = urllib2.urlopen(req).read()
  35.  
  36.     soup = BeautifulSoup(html) #it'll make this an obj
  37.     soup.get_text() #this will print all the text no html code
  38.  
  39.     rawprice = soup.findAll(attrs={"name":"og:price"}) #get the price
  40.     price = rawprice[0]['content'].encode('utf-8')
  41.    
  42.     image = soup.findAll(attrs={"name":"og:image"}) #get the image
  43.     image = image[0]['content'].encode('utf-8')
  44.     image = "<img src='" + image + "'>"
  45.  
  46.     product_name = soup.findAll(attrs={"name":"og:description"}) #get product name
  47.     product_name = product_name[0]['content'].encode('utf-8')
  48.  
  49.     print colored(product_name, 'yellow')
  50.  
  51.     print "New price:", price,
  52.  
  53. # build the path and open the DB file where last product price is stored
  54.     db_filename = dir + product_name.replace(" ", "") + ".txt"
  55.  
  56.     if os.path.isfile(db_filename):  # check if file exists
  57.         fo = open(db_filename, "r+")
  58.         oldprice = fo.readline() # if there is a file read the previous value
  59.     else:
  60.         fo = open(db_filename, "w+") # create the missing file
  61.         oldprice = "$0" # set previous price to 0
  62.                          
  63.     print " / Old price: ", oldprice,
  64.  
  65.     position = fo.seek(0, 0);
  66.  
  67.     fo.write(price+"\n")        # write the new value
  68.     fo.close()                  # close the file
  69.  
  70.     price = price.replace("$", "")
  71.     old_price = oldprice.replace("$", "")
  72.  
  73.     change = None
  74.    
  75.     if float(price) > float(old_price):
  76.         message = 'New price: ${} <b>The price have raised with ${}</b>'.format(price, float(price) - float(old_price))
  77.         print message + "\n"
  78.         change = True
  79.     elif float(price) < float(old_price): # check if there is a price drop
  80.         message = "New price: ${} <b>The price have droped with ${}</b>".format(price, float(old_price) - float(price))
  81.         print message  + "\n"
  82.         change = True
  83.     elif float(price) == float(old_price):
  84.         message = "No change"
  85.         print "No price change \n"
  86.  
  87.     message = "<h2>" + product_name + "</h2>" + message + "<br/><a href='" + check_url +"'>"+ image +"</a>"
  88.  
  89.     if (change and old_price != "0"):
  90.         web.sendmail('sender@gmail.com', 'receiver@gmail.com',"Price Change " + product_name, message, headers={'Content-Type':'text/html;charset=utf-8'})
  91.     return (old_price,price,change)
  92.    
  93. i = 0
  94. while i < len(url_list):
  95.     price_check(url_list[i])
  96.     sleep(randint(10,30))
  97.     i = i + 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top