Back to Parent

The code below is used for accessing the amazon API and is written in python. In order for it to work, the user must have an amazon_associates product advertising account and the corresponding access tokens for it. Also the following package is required. Details for installation are in the link provided: - python-amazon-simple-product-api 2.2.11 (https://pypi.python.org/pypi/python-amazon-simple-product-api)
from amazon.api import AmazonAPI
import time,  threading
import requests

access_key = "YOUR AMAZON PRODUCT ADVERTISING ACCESS KEY" 
secret_key = "YOUR AMAZON PRODUCT ADVERTISING SECRET KEY" 
associate_id = "YOUR AMAZON PRODUCT ADVERTISING ASSOCIATE ID"
amazon = AmazonAPI(access_key, secret_key, associate_id, region="US")

device_id = "YOUR PARTICLE DEVICE ID"
particle_access_token = "YOUR PARTICLE ACCESS TOKEN"
check_freq = 60 # in seconds
item_id = 'AMAZON PRODUCT ID FOR DESIRED PRODUCT TO PURCHASE'
item_price = 59.99 # set this to the current market price for the item 
target_price = 47.99 # set this to desired purchase price



def servoOpenBox():
	global device_id, particle_access_token
	url = "https://api.particle.io/v1/devices/{}/{}".format(device_id, "servoOpenBox")
	res = requests.post(url, data={"arg":"", "access_token":particle_access_token})
	if res.status_code != requests.codes.ok:
		print "Bad POST request. Error {}".format(res.status_code)
	else:
		# print  "Good Request"
		print ""  


def alertPriceDrop():
	if item_price <= target_price:
		# servoWave()
		print "Price Dropped Target = {} Current = {}".format(target_price, item_price)
		servoOpenBox()

def update_price():
	global check_freq, item_price, item_id, amazon
	print time.ctime()
	item = amazon.lookup(ItemId=item_id)
	item_price = float(item.price_and_currency[0])
	alertPriceDrop()


# for simulation purposes
def simulate_update_price():
	global item_price, target_price
	# simulate_check_freq = 10 #in seconds
	high_price = 59.99
	low_price = target_price
	if item_price <= low_price:
		item_price = high_price
	else:
		item_price = low_price
	alertPriceDrop() 
	# threading.Timer(simulate_check_freq, simulate_update_price).start()


class UpdateThread(threading.Thread):
	def __init__(self, update_func, period):
		threading.Thread.__init__(self)
		self.update = update_func
		self.period = period
		self.event = threading.Event()

	def stop(self):
		self.event.set()

	def run(self):
		while not self.event.wait(self.period):
			self.update()


def setup():
	global amazon, item_id
	item = amazon.lookup(ItemId=item_id)
	# print "{}".format("abc")
	print "Name = {}, price = {}".format(item.title, item.price_and_currency)
	item_price = float(item.price_and_currency[0])
	print item_price


def run():
	setup()
	updateThread = UpdateThread(update_price, check_freq)
	updateThread.start()
	while 1:
		cmd = raw_input()
		if cmd == "stop":
			print "stopping..."
			updateThread.stop()
			break
	return




if __name__ == "__main__":
	run()
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0