Skip to content

Instantly share code, notes, and snippets.

@korayal
Created March 29, 2020 22:20
Show Gist options
  • Save korayal/e23e87f88f166895807129a4194e343b to your computer and use it in GitHub Desktop.
Save korayal/e23e87f88f166895807129a4194e343b to your computer and use it in GitHub Desktop.
# install beautifulsoup4, requests, lxml
import re
import json
from pathlib import Path
from urllib.parse import urlencode
import requests
from bs4 import BeautifulSoup
# your user hash and token for Pushover
user = '<user>'
token = '<token>'
# temporary file to prevent multiple notifications
tmp_file = Path('/tmp/pereja')
target_uri = 'https://perejastore.com/limon-kolonyalari-72'
tmp_prods = set([])
if tmp_file.exists():
try:
with tmp_file.open('r') as f:
tmp_prods = set(json.load(f))
except:
pass
else:
with tmp_file.open('w') as f:
f.write(json.dumps([]))
def get_products(pg):
return pg.find_all('div', class_='productItem')
def is_target_product(prd):
return prd.find('a', class_='TukendiIco') is None
def get_product_name(prd):
return re.sub(r'Limon\s+Çiçeği\s+Kolonyası\s+', '', prd.find('div', class_='productName').text)
session = requests.session()
res = session.get(target_uri)
page = BeautifulSoup(res.text, 'lxml')
products_in_stock = set(map(get_product_name, filter(is_target_product, get_products(page))))
products_text = json.dumps(list(products_in_stock))
with tmp_file.open('r+') as f:
diff = list(tmp_prods.symmetric_difference(products_in_stock))
# send notification only if file contents do not match
if len(products_in_stock) > 0 and len(diff) != 0:
message = urlencode({
'user': user,
'token': token,
'title': 'PEREJA',
'priority': 1,
'message': products_text,
'url': target_uri
})
pres = session.post("https://api.pushover.net/1/messages.json", message)
if pres.status_code == 200:
f.write(products_text)
elif len(products_in_stock) == 0:
f.write(products_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment