Skip to content

Instantly share code, notes, and snippets.

@jkctech
Last active November 23, 2021 20:38
Show Gist options
  • Save jkctech/0d3049626799d9bde49fe28d1432db89 to your computer and use it in GitHub Desktop.
Save jkctech/0d3049626799d9bde49fe28d1432db89 to your computer and use it in GitHub Desktop.
Scraper for Archonia stock
import requests
# ========== SETTINGS ==========
str_available = "Beschikbaar!"
str_unavailable = "Niet beschikbaar!"
str_preorder = "Pre-order!"
str_unknown = "Onbekend"
filename = "links.txt"
pushover_enabled = True
pushover_apikey = ""
pushover_userkey = ""
# ========== /SETTINGS ==========
# Pushover util
def push(user_key, api_key, message, title=None, priority=0, url=None):
endpoint = "https://api.pushover.net/1/messages.json"
data = {
"token": api_key,
"user": user_key,
"message": message,
"priority": priority
}
if url != None:
data['url'] = url
if title != None:
data['title'] = title
req = requests.post(endpoint, params=data)
return (req.status_code == 200, req.text)
# Stores availability
available = {}
# Read all links from file
with open(filename) as file:
lines = file.readlines()
lines = [line.rstrip() for line in lines]
# Loop over lines
for line in lines:
raw = requests.get(line).text
# Ik hoop dat dit zo werkt, nog nooit zo gebruikt
if "In stock now!" in raw:
status = str_available
elif 'Pre-order <i class="fa fa-shopping-cart m-l-xs" aria-hidden="true"></i>' in raw:
status = str_preorder
elif "Out of stock":
status = str_unavailable
else:
status = str_unknown
# Get product info
productid = raw.split('Product Code')[1].split('>')[3].split('<')[0].upper().strip()
name = raw.split('"name": "')[1].split('"')[0]
# Add to list
available[productid] = (status, name, line)
# Print all items
for item in available.keys():
print("[{}] {}: {}".format(item, available[item][1], available[item][0]))
# If available, send message
if pushover_enabled and available[item][0] in [str_available, str_preorder]:
# Variables
title = "[{}] {}".format(available[item][0], available[item][1])
message = "[{}] {} in stock!".format(item, available[item][1])
# Acutally send push
p = push(pushover_userkey, pushover_apikey, message, title=title, priority=0, url=available[item][2])
# Debug
if p[0] == False:
print("Could not send push notification: " + p[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment