Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import web, json, urllib, urllib2
- from bs4 import BeautifulSoup
- import os
- import os.path
- from termcolor import colored
- from random import randint
- from time import sleep
- dir = "/home/pi/scripts/pythontest/db-files/" #folder where the DB files will be saved
- # configure smtp server for sending mail
- web.config.smtp_server = 'smtp.gmail.com'
- web.config.smtp_port = 587
- web.config.smtp_username = 'username@gmail.com'
- web.config.smtp_password = 'password'
- web.config.smtp_starttls = True
- headers = { 'User-Agent' : 'Mozilla/5.0' } # headers for the url download request
- url_list = []
- url_list.append("http://www.gearbest.com/batteries-chargers/pp_140785.html") #usb led light
- url_list.append("http://www.gearbest.com/electronics-gadgets/pp_280338.html") # enhanced xiaomi led light
- url_list.append("http://www.gearbest.com/other-classic-toys/pp_271768.html") # Mitko magnetic cubs
- url_list.append("http://www.gearbest.com/tv-box-mini-pc/pp_284266.html") # Intel WiDi Display Dongle
- url_list.append("http://www.gearbest.com/mobile-power-bank/pp_187685.html") # Mi 10k Ah Battery
- url_list.append("http://www.gearbest.com/tv-box-mini-pc/pp_238050.html") # pipo x8 64 gb
- url_list.append("http://www.gearbest.com/speakers/pp_228241.html") # MPow Bluetooth Speaker
- def price_check(check_url):
- #build the request for the page that will be checked
- req = urllib2.Request(check_url, None, headers)
- #reads the html
- html = urllib2.urlopen(req).read()
- soup = BeautifulSoup(html) #it'll make this an obj
- soup.get_text() #this will print all the text no html code
- rawprice = soup.findAll(attrs={"name":"og:price"}) #get the price
- price = rawprice[0]['content'].encode('utf-8')
- image = soup.findAll(attrs={"name":"og:image"}) #get the image
- image = image[0]['content'].encode('utf-8')
- image = "<img src='" + image + "'>"
- product_name = soup.findAll(attrs={"name":"og:description"}) #get product name
- product_name = product_name[0]['content'].encode('utf-8')
- print colored(product_name, 'yellow')
- print "New price:", price,
- # build the path and open the DB file where last product price is stored
- db_filename = dir + product_name.replace(" ", "") + ".txt"
- if os.path.isfile(db_filename): # check if file exists
- fo = open(db_filename, "r+")
- oldprice = fo.readline() # if there is a file read the previous value
- else:
- fo = open(db_filename, "w+") # create the missing file
- oldprice = "$0" # set previous price to 0
- print " / Old price: ", oldprice,
- position = fo.seek(0, 0);
- fo.write(price+"\n") # write the new value
- fo.close() # close the file
- price = price.replace("$", "")
- old_price = oldprice.replace("$", "")
- change = None
- if float(price) > float(old_price):
- message = 'New price: ${} <b>The price have raised with ${}</b>'.format(price, float(price) - float(old_price))
- print message + "\n"
- change = True
- elif float(price) < float(old_price): # check if there is a price drop
- message = "New price: ${} <b>The price have droped with ${}</b>".format(price, float(old_price) - float(price))
- print message + "\n"
- change = True
- elif float(price) == float(old_price):
- message = "No change"
- print "No price change \n"
- message = "<h2>" + product_name + "</h2>" + message + "<br/><a href='" + check_url +"'>"+ image +"</a>"
- if (change and old_price != "0"):
- web.sendmail('sender@gmail.com', 'receiver@gmail.com',"Price Change " + product_name, message, headers={'Content-Type':'text/html;charset=utf-8'})
- return (old_price,price,change)
- i = 0
- while i < len(url_list):
- price_check(url_list[i])
- sleep(randint(10,30))
- i = i + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement