Instantly share code, notes, and snippets.

Embed
What would you like to do?
import json
import requests
def make_request(path, method, payload=None):
# Use API Key and API Token that you've copied from
# your settings page.
headers = {
'apikey': 'internal-test-samet-20171227-1@prisync.com',
'apitoken': 'c19287f99149b8c2dca88bcfa135f540',
}
# This is the root URL. All API calls are relative to this URL.
base_url = "https://prisync.com/api/v2"
# We append the path into base_url
url = "".join([base_url, path])
# HTTP GET for getting data
if method == 'get':
result = requests.get(url, headers=headers)
# HTTP POST for usually creating new data and editing data.
elif method == 'post':
result = requests.post(url, data=payload, headers=headers)
else:
pass
return result
# Get all products, paginated by 100 items.
response = make_request('/list/product', 'get')
# Load all products into a Python List
products = json.loads(response.text)
# Iterate over all products one by one
for product in products['results']:
# For each product, get product details
r = make_request('/get/product/id/{}'.format(product['id']), 'get')
# Load product details into a Python object
product_details = json.loads(r.text)
# Get all URL IDs belonging to this product
urls = product_details['urls']
# Iterate over all URLs
for url in urls:
# For every URL, get URL details
url_response = make_request('/get/url/id/{}'.format(url), 'get')
# Load URL info to a Python object
url_info = json.loads(url_response.text)
# Get daily price change from URL info.
daily_change = float(url_info['change_day'] or 0)
# Get current price of this URL
price = float(url_info['price'])
# Let's do the calculation if only the price is available.
if price > 0:
# If the price is discounted more than 5%, report it.
if (daily_change / price * 100) > 5:
print("Discount alert at {}".format(url_info['url']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment