Skip to content

Instantly share code, notes, and snippets.

@invisiblek
Created March 19, 2021 00:36
Show Gist options
  • Save invisiblek/320e8034d58551d017939b3fba987d70 to your computer and use it in GitHub Desktop.
Save invisiblek/320e8034d58551d017939b3fba987d70 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import os
import smtplib
import subprocess
import sys
import time
import xmpp
from bitlyshortener import Shortener
from datetime import datetime
from dateutil.relativedelta import relativedelta
from random import randint
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
opts = []
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "options.json")) as f:
try:
opts = json.load(f)
except:
print("invalid options.json")
sys.exit()
if "bitly_api_key" in opts:
shortener = Shortener(tokens=[opts['bitly_api_key']], max_cache_size=8192)
if "xmpp_to" in opts and "xmpp_from" in opts and "xmpp_pass" in opts:
jid = xmpp.JID(opts['xmpp_from'])
connection = xmpp.Client(jid.getDomain(), debug=None)
connection.connect()
connection.auth(user=jid.getNode(), password=opts['xmpp_pass'])
j = []
t = []
with open(sys.argv[1]) as f:
try:
j = json.load(f)
except:
print("cannot open or load json")
sys.exit()
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
def foundone(d):
try:
if shortener:
shorturl = shortener.shorten_urls_to_dict([d['url']])[d['url']]
except Exception as e:
shorturl = d['url']
pass
return "New item: " + d['name'] + " - " + shorturl
try:
driver.get('https://www.treatland.tv/new-puch-peugeot-motobecane-vespa-moped-parts-s/88.htm')
elements = driver.find_elements_by_class_name("productnamecolor")
for e in elements:
d = {}
d['name'] = e.text
d['url'] = e.get_attribute("href")
if opts['debug']:
print(d['name'])
found = False
for i in j:
if not found and i['name'] == d['name']:
found = True
if datetime.strptime(i['last_seen'], "%Y-%m-%d %H:%M:%S.%f") + relativedelta(months=1) < datetime.now():
msg = foundone(d)
print(msg)
if connection:
connection.send(xmpp.protocol.Message(to=opts['xmpp_to'], body=msg))
if not found:
msg = foundone(d)
print(msg)
if connection:
connection.send(xmpp.protocol.Message(to=opts['xmpp_to'], body=msg))
d['last_seen'] = datetime.now().__str__()
t.append(d)
if t:
with open(sys.argv[1], "w") as f:
json.dump(t, f, indent=4)
except Exception as e:
print(e)
if connection:
connection.send(xmpp.protocol.Message(to=opts['xmpp_to'], body=e))
pass
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment