Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created June 23, 2021 14:41
Show Gist options
  • Save turnipsoup/4f1433a231cbe028fff3d43b546fc071 to your computer and use it in GitHub Desktop.
Save turnipsoup/4f1433a231cbe028fff3d43b546fc071 to your computer and use it in GitHub Desktop.
Script we used to alert us if the PS5 was in stock. You need a Discord webhook.
import requests, logging
import pandas as pd
import bs4 as bs
logging.basicConfig(filename="./ps5search.log", level=logging.INFO, format="%(asctime)s|%(levelname)s|%(message)s")
# Define the base URL and the webhook URL
stock_url = "https://www.nowinstock.net/videogaming/consoles/sonyps5/"
webhook_url = "<Discord_Webhook_URL>"
# Check the datafram row to see if the item is in stock
def check_item_stock(df_row) -> bool:
# Certain cases to ignore
ignore_names = ["Ebay"]
ignore_statuses = ["Not Tracking"]
# Return False if the place being checked is in the ignore list
for word in ignore_names:
if word in df_row.Name:
return False
if df_row.Status1 in ignore_statuses:
return False
# Return True if the status is NOT out of stock.
if df_row.Status1 != "Out of Stock":
return True
# By default return False
return False
# Alert when we are in stock!
def handle_item_alert(df_row):
logging.info(f"Item may be in stock at {df_row.Name}, alerting webhook")
send_data = {
"username": "Spidey Bot",
"content": f"The PS5 may be in stock at `{df_row.Name}`, look now!"
}
try:
requests.post(webhook_url, json=send_data)
logging.info("Alerted the webhook")
except:
logging.error(f"Was unable to alert the webhook! URL was {webhook_url}")
# Our main function
if __name__ == "__main__":
# Get the HTML
try:
r = requests.get(stock_url)
logging.info(f"Fetched URL {stock_url}")
except:
logging.error(f"Could not get URL {stock_url}")
exit(1)
# Extract the table
soup = bs.BeautifulSoup(r.content, features="lxml")
stock_table = soup.find("div", {"id": "data"}).find("table")
logging.info("HTML converted to table")
# Convert the table into a dataframe for easier manipulation
df = pd.read_html(str(stock_table))
df = df[0]
# Some non-tracked rows have this stupid text, so I remove it.
df.Name = df.Name.apply(lambda x: x.replace("Item alerting temporarily suspended.", ""))
logging.info("Iterating over stock results")
for index, row in df.iterrows():
stock_status = check_item_stock(row)
if stock_status:
handle_item_alert(row)
logging.info("Done with current check")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment