Skip to content

Instantly share code, notes, and snippets.

@dpeters1
Created July 2, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpeters1/d3d18af519845d7c56b797e6446331ee to your computer and use it in GitHub Desktop.
Save dpeters1/d3d18af519845d7c56b797e6446331ee to your computer and use it in GitHub Desktop.
Script to notify me of stock changes in Audi's at the local junkyard, allowing me to scavenge them for parts before everyone else
import requests
import json
from pushbullet import Pushbullet
from bs4 import BeautifulSoup
pb = Pushbullet('************************')
pbPixel = pb.get_device('Google Pixel')
url = 'http://www.kennyupull.com/en/views/ajax'
headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded'}
form = {'field_car_brand_tid': 5841, 'field_car_model_tid': 'All',
'sort_by': 'field_arrival_date_value', 'view_name': 'car_parts_view',
'view_display_id': 'block', 'field_car_location_target_id[]': 82}
print('Searching for all {}s at {} Kenny\'s...'.format('Audi', 'Ottawa'))
r = requests.post(url, headers=headers, data=form)
htmlResponse = r.json()[1].get('data')
soup = BeautifulSoup(htmlResponse, 'html.parser')
htmlNames = soup.find_all('div', class_='car-parts-grid-title')
htmlDates = soup.find_all('span', class_='date-display-single')
htmlImages = soup.find_all('img')
carList = []
for i in xrange(0, len(htmlNames)):
carDict = {'Name': htmlNames[i].contents[1].string,
'Date': htmlDates[i].string,
'Image': htmlImages[i]['src']
}
carList.append(carDict)
print('{} matching cars found'.format(len(carList)))
messageBody = ''
for car in carList:
text = '\nName: {}\nDate added: {}\nImage URL: {}\n'.format(car['Name'],
car['Date'],
car['Image'])
messageBody += text
print(messageBody)
carLogFile = open('carlog.txt', 'r+')
try:
carLog = json.load(carLogFile)
except ValueError:
carLog = []
for car in carList:
if(car not in carLog):
print("New car at the junkyard!")
pbPixel.push_note("Kenny's Junkyard Report", messageBody)
break
for car in carLog:
if(car not in carList):
print("Car removed from the junkyard")
pbPixel.push_note("Kenny's Junkyard Report", messageBody)
break
carLogFile.truncate(0)
carLogFile.seek(0)
json.dump(carList, carLogFile)
carLogFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment