-
-
Save michalvalasek/b0854a35483f61a3a0997efa78fffc0f to your computer and use it in GitHub Desktop.
A script that watches prime now for stock, sending a webhook to Zapier which you can configure to send you an SMS, Slack, whatever...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import bs4 | |
import requests | |
import logging | |
import sys | |
logger = logging.getLogger('switchlogger') | |
logger.setLevel(logging.DEBUG) | |
ch = logging.StreamHandler(sys.stdout) | |
ch.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - %(message)s') | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
ZIP_CODE = 12345 # get you a zip code | |
ZAPIER_WEBHOOK_URL = 'https://hooks.zapier.com/hooks/catch/N/ABCDEFG/' # get you a webhook -> sms zapier.com | |
SEARCH_STRING = 'Nintendo Switch' | |
PRODUCT_IDS = {'B01MUAGZ49', 'B01LTHP2ZK'} | |
POLL_INTERVAL = 5 | |
ALERT_EVERY_SECONDS = 60 | |
LAST_ALERT_UNIX = time.time() - ALERT_EVERY_SECONDS | |
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' | |
MAIN_URL = 'https://primenow.amazon.com' | |
SEARCH_URL = '{}/search'.format(MAIN_URL) | |
if __name__ == '__main__': | |
session = requests.Session() | |
session.headers['User-Agent'] = USER_AGENT | |
session.post(MAIN_URL, data={'newPostalCode': ZIP_CODE}) # login | |
logger.debug('logging in for {}'.format(ZIP_CODE)) | |
while True: | |
response = session.get(SEARCH_URL, params={'k': SEARCH_STRING}) | |
soup = bs4.BeautifulSoup(response.text, 'html.parser') | |
logger.debug('searching for {}'.format(SEARCH_STRING)) | |
for product_id in PRODUCT_IDS: | |
filtered = soup.select('div#house-search-result div#asin-card-{}'.format(product_id)) | |
if filtered: | |
if LAST_ALERT_UNIX < time.time(): | |
logger.debug('!!! found for {} !!!'.format(product_id)) | |
LAST_ALERT_UNIX = time.time() + ALERT_EVERY_SECONDS | |
requests.post(ZAPIER_WEBHOOK_URL, json={ | |
'product_id': product_id, | |
'zip_code': ZIP_CODE | |
}) | |
else: | |
logger.debug('found but silent for last alert'.format(product_id)) | |
time.sleep(POLL_INTERVAL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment