Skip to content

Instantly share code, notes, and snippets.

@Moving-Electrons
Created August 31, 2014 01:49
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 Moving-Electrons/e7b03cc489de359890e4 to your computer and use it in GitHub Desktop.
Save Moving-Electrons/e7b03cc489de359890e4 to your computer and use it in GitHub Desktop.
This python script parses all woot.com feeds and searches for predefined words in each item title. If it finds a match, it sends an instant message through Pushover (pushover.net). It also logs all the posted items in text files per category. More information in www.movingelectrons.net
import feedparser #need manual installation. Doesn't come with Python.
import os
import httplib #used by pushover
import urllib #used by pushover
# Linux:
folderPath = '/home/YOUR_USERNAME/Scripts/Support_Files/Woot/'
# Just woot.com items:
#url = "https://api.woot.com/1/sales/current.rss/www.woot.com"
# All woot feeds:
url = "http://api.woot.com/1/sales/current.rss"
alertsFile = folderPath+'alert_list.txt'
dealsFile = folderPath+'dealsgroup' #omit extension as it will be provided by the script.
def pushover(msg):
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "YOUR-PUSHOVER-API-TOKEN",
"user": "YOUR-PUSHOVER-API-USER",
"message": msg,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
def inItemsList(wootTitle, fileIndex):
''' Checks if the item currently in woot.com was previously entered in the item_list.txt file.
If it was not, it enters it and returns the inList flag as False (i.e. it wasn't previously in the
list of items). Otherwise, it returns the flag as True (i.e. it is already in the list)'''
inList=True
fileStr = str(fileIndex)
with open(dealsFile+'-'+fileStr+'.txt', 'a+') as itemList: #it opens or creates a file in the form of dealsgroup-1.txt
try:
lastLine = itemList.readlines()[-1].strip('\n')
except IndexError:
lastLine = ''
''' If the file is empty or does not exist, there will be an IndexError.
Then, an arbitraty value is assigned to lastLine so that the script continues
and the file is created'''
if wootTitle==lastLine:
inList=True
else:
itemList.write(wootTitle+'\n')
inList=False
return inList
# Main Script
def main():
parsedFeed = feedparser.parse(url)
entryIndex = 0
for entry in parsedFeed['entries']:
entryIndex += 1
try:
# The feed only has one entry. Therefore, the data from index [0] is assigned.
title = entry['title']
soPercentage = entry['woot_soldoutpercentage']
price = entry['woot_price']
wootoff = entry['woot_wootoff']
except IOError:
print ('Variables could not be assigned based on parsed data')
if not inItemsList(title, entryIndex):
with open(alertsFile, 'r') as alertList:
for alertName in alertList:
alertName = alertName.strip('\n').strip('\r')
# '\r' should be used to remove carriage return on *nix systems.
print alertName
print title
if alertName in title:
print ('Item found in Alert List')
soPerInt = int(soPercentage)*100
body = "Title: %s \nPrice: %s \nSold Out Percentage: %s%% \nWoot Off? %s\n" % (title, price, str(soPerInt), wootoff)
print ('Sending message to pushover')
pushover(body)
else:
print ('Item not found in Alert List')
else:
print ('Item already in Item List')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment