Example #1
0
def checkProduct(model):

	#setup
	currentFolder = os.path.dirname(os.path.realpath(__file__)) + "/shopbot_history/"
	if not os.path.exists("shopbot_history"):
		os.makedirs(currentFolder)
	priceHistoryFile = currentFolder + "shopbot-"+model+".txt"

	#url to call
	r = requests.get("http://www.shopbot.ca/m/?m=" + model)
	html = r.text.encode('utf-8')
	soup = BeautifulSoup(html)

	#begin scrape
	product = Product(soup.h1(text=True)[0], sanitizeName(soup.findAll("li", { "class" : "image" })[0].img["alt"]), soup.findAll("li", { "class" : "image" })[0].img["src"], [])
	for x in soup.findAll("div", { "class" : "price" }, text=True):
		product.addPrice(floatFromMoney(x.span.text))

	print product.name + ":"

	if os.path.exists(priceHistoryFile):
		with open(priceHistoryFile, "r") as f:
			lines = f.readlines()
			previousPrice = float(lines[-1])
			f.close()
		
		if (previousPrice == min(product.prices)):
			print "\tNo price changes detected. Currently: " + moneyFromFloat(min(product.prices)) + "\n"

		elif (previousPrice > min(product.prices)):
			print "\tNew lower price detected! Currently: " + moneyFromFloat(min(product.prices))
			if (previousPrice - priceDropThreshold > min(product.prices)):
				subject = "Woo! " + moneyFromFloat(min(product.prices)) + " for " + product.model
				body = "\n\n" + product.name + " has dropped in price! It's now: \n\n" + moneyFromFloat(min(product.prices)) + ".\n\nLink: http://www.shopbot.ca/m/?m=" + model + "\n"
				notify(subject, body)

		elif (previousPrice < min(product.prices)):
			print "\tPrice increase. Currently: " + moneyFromFloat(min(product.prices)) + "\n"
		
		with open(priceHistoryFile, "a") as f:
			f.write(str(min(product.prices)) + "\n")
			f.close()
	else:
		with open(priceHistoryFile, "a") as f:
			f.write(str(min(product.prices)) + "\n")
			f.close()
		print "\tCreated history file. Run this script again to check for price changes.\n"