Guest User

Untitled

a guest
May 31st, 2020
2,942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import env
  2. import requests
  3. import json
  4. import boto3
  5. from datetime import datetime
  6. import time
  7.  
  8. URL = 'https://www.bestbuy.ca/ecomm-api/availability/products?accept=application%2Fvnd.bestbuy.standardproduct.v1%2Bjson&accept-language=en-CA&locations=203%7C977%7C617%7C62%7C931%7C927%7C57%7C938%7C965%7C237%7C932%7C202%7C943%7C200%7C956%7C926%7C795%7C916%7C233%7C544%7C910%7C954%7C207%7C930%7C937%7C622%7C245%7C223%7C990%7C925%7C985%7C206%7C942%7C949%7C959&postalCode=M6J&skus=13444247'
  9.  
  10. headers = {
  11.     'authority': 'www.bestbuy.ca',
  12.     'pragma': 'no-cache',
  13.     'cache-control': 'no-cache',
  14.     'user-agent': 'Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4159.2 Safari/537.36',
  15.     'accept': '*/*',
  16.     'sec-fetch-site': 'same-origin',
  17.     'sec-fetch-mode': 'cors',
  18.     'sec-fetch-dest': 'empty',
  19.     'referer': 'https://www.bestbuy.ca/en-ca/product/logitech-c920s-pro-1080p-hd-webcam/13444247',
  20.     'accept-language': 'en-US,en;q=0.9'
  21. }
  22.  
  23. def main():
  24.     quantity = 0
  25.     attempt = 0
  26.  
  27.     while (quantity < 1):
  28.         response = requests.get(URL, headers=headers)
  29.         response_formatted = json.loads(response.content.decode('utf-8-sig').encode('utf-8'))
  30.  
  31.         quantity = response_formatted['availabilities'][0]['shipping']['quantityRemaining']
  32.  
  33.         if (quantity < 1):
  34.             #Out Of stock
  35.             print('Time=' + str(datetime.now()) + "- Attempt=" + str(attempt))
  36.             attempt += 1
  37.             time.sleep(5)
  38.         else:
  39.             print('Hey its in stock! Quantity=' + str(quantity))
  40.             publish(quantity)
  41.  
  42.  
  43. def publish(quantity):
  44.     arn = 'arn:aws:sns:us-east-1:398447858632:InStockTopic'
  45.     sns_client = boto3.client(
  46.         'sns',
  47.         aws_access_key_id=env.accessKey,
  48.         aws_secret_access_key=env.secretKey,
  49.         region_name='us-east-1'
  50.     )
  51.  
  52.     response = sns_client.publish(TopicArn=arn, Message='Its in stock! Quantity=' + str(quantity))
  53.     print(response)
  54.  
  55. main()
Advertisement
Add Comment
Please, Sign In to add comment