#!/usr/bin/env python # pywoot is NOT endorsed by or affiliated with Woot # pywoot version 0.0.2 created by Alex Brown # based off of, and ideas taken from, and idea gotten from: wooty, a perl script that does the like, but without the awsome s**t I added # see (http://freshmeat.net/projects/wooty/?branch_id=65979) # 12/17/06 20:29:25 EST -0500 # Made by Alex Brown ("uh oh spaghetti-o") # Licensed under the GPL version 2.0 __build_date__ = "Sun 17 Dec 2006 08:29:25 PM EST -0500" __program__ = "PyWoot" __version__ = "0.0.2" __filecall__ = "pywoot.py" __author__ = "Alex Brown (\"uh oh spaghetti-o\") " __contributors__ = ["mikm (#ubuntu on irc.freenode.net)", "John Ehresman "] __tagline__ = " is a python module to retrieve information on the current woot at http://www.woot.com/" __tagline2__ = "A python module to retrieve information on the current woot at http://www.woot.com/" __usage__ = __filecall__ + "\n" + __program__ + __tagline__ __doc__ = __program__ + ", by " + __author__ + ", Version " + __version__ + __tagline__ __copyright__ = __program__ + " - " + __tagline2__ + "\nCopyright (C) 2006 Alex Brown" __license__ = "\t" + __program__ + ' - ' + __tagline2__ + """ Copyright (C) 2006 Alex Brown This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ def debug(*arg): """If debug is turned on, then print debug messages""" global deb; if deb == True: a = "" for i in arg: a += str(i) print a import sys, os, urllib, time, re, socket, copy socket.setdefaulttimeout(5.0) deb = False tempdir = "" def rmfolderslash(arg): """ def rmfolderslash(arg) ---------------------- arg - string to remove trailing '/' off of Used when a user is unsure if a folder name such as /tmp/ or /tmp is going to be passed, therefore if arg is /tmp/ it will take the trailing slash off. Example: directory = '/tmp/' urllib.urlretrieve("http://www.gnu.org/licenses/gpl.txt", rmfolderslash(directory) + '/gpl.txt') *me thinks this is too much explanation for a 3 line function :( """ # Check to see if the foldername ends with a '/' if so, remove it if arg[-1:] == '/': arg = arg[:-1] return arg def setupgtk(): global pygtk; import pygtk pygtk.require('2.0') global gtk; import gtk def getwoot(url = "http://www.woot.com/DefaultMicrosummary.ashx", global_bool = False, skipget=False): """ def getwoot(url = "http://www.woot.com/DefaultMicrosummary.ashx", global_bool = True) ------------------------------------------------------------------------------------- url - string containing the url for the microsummary of what's on http://www.woot.com/ global_bool - a boolean variable that tells the function whether to save the information of what's on woot to global variables. Set global_bool to 'False' if using PyWoot as a module. """ # See if the results of this function should be stored in a global variable. if global_bool == True: global price, item, status if skipget == True: (price, item, status) = ('None', 'None', 'None') return price, item, status # Try and open the Woot Microsummary for parsing for i in range(3): try: wootfile = open(urllib.urlretrieve(url)[0], 'r') except IOError: if i < 2: print "**pywoot: summary download timed-out -- retrying... ", (i + 1) continue else: sys.exit("Error: Third retry failed -- unable to retrieve information from " + url) break #except: this was declared stupid # sys.exit("Unhandled error in downloading summary!") # break else: debug("**pywoot: Summary downloaded") # Split the summary by ':', essentially, this should return [price, item, status], unless the item is in stock wootlines = wootfile.read().split(':') # If the item is in stock, the status won't show up, so we need a failsafe for this: if len(wootlines) == 2: # If we only get two returns, the price and item, set the status to In Stock print "2 items" (price, item) = wootlines status = "In Stock" elif len(wootlines) == 3: # Otherwise, we'll get the status print "3 items" (price, item, status) = wootlines elif len(wootlines) == 4: print "4 items" (poop, price, item, status) = wootlines else: # I don't freakin' know how, but just incase of something: sys.exit("Unhandled number of items in the variablewootlines.\nContact the author of this script if this always occurs.\nSee __author__\n" + str(wootlines)) # Strip the variables of trailing/leading whitespace price = price.strip(); item = item.strip(); status = status.strip() if price.endswith("%"): percent_num = int(price[:-1]) num = str(percent_num) real_item = status if percent_num >= 90: status = "In Stock (" + num + "%)" elif percent_num < 90 and percent_num >= 70: status = "Many In Stock (" + num + "%)" elif percent_num < 70 and percent_num >= 40: status = "Half In Stock (" + num + "%)" elif percent_num < 40 and percent_num >= 20: status = "Few In Stock (" + num + "%)" elif percent_num < 20 and percent_num > 1: status = "Almost out of Stock (" + num + "%)" else: status = num + "% In Stock" price = item item = real_item # Finaly print debug output of the variables, and return them. # debug("Current Woot Info: ", item, " is ", status.lower(), " and on sale for ", price, ".") Not needed, maybe I want the line again later # debug("price: ", price, "\nitem: ", item, "\nstatus: ", status) return price, item, status def get_icon(dirarg="/tmp", deletefile=False, skipget=False, url = "http://www.woot.com/App_Themes/Woot/favicon.ico"): setupgtk() global tempdir if len(tempdir) > 0: dirarg = tempdir dirarg = rmfolderslash(dirarg) iconlocation = dirarg + "/pywooticon.ico" if deletefile == True: try: os.remove(iconlocation) except: pass elif skipget == True: pass else: for i in range(3): try: urllib.urlretrieve(url, iconlocation) except IOError: if i < 2: print "**pywoot: icon download timed-out -- retrying... ", (i + 1) continue else: self.clean() sys.exit("Error: Third retry failed -- unable to retrieve Woot icon from " + url) break debug("**pywoot: Icon downloaded") return iconlocation def get_pic(dirarg="/tmp", deletefile=False, skipget=False, width=None, height=None): setupgtk() global tempdir, re if len(tempdir) > 0: dirarg = tempdir dirarg = rmfolderslash(dirarg) piclocation = dirarg + "/pywootpic.jpg" modpiclocation = dirarg + "/modwootpic.jpg" if deletefile == True: try: os.remove(piclocation) except: pass try: os.remove(modpiclocation) except: pass return elif skipget == True: pass else: try: webpage = open(urllib.urlretrieve("http://www.woot.com/Default.aspx")[0], 'r') except: #self.clean() #sys.exit('**woot-notify: Unable to retrieve homepage of www.woot.com') print '**pywoot: Unable to retrieve homepage of www.woot.com' return '' code = webpage.read() picregex = re.search(r'', code, re.I) try: urllib.urlretrieve(picregex.groups()[0], piclocation) except: #self.clean() #sys.exit("Error: Unable to retrieve picture of the current Woot") print '**pywoot: Unable to retrieve picture of the current Woot' return '' if width == None and height == None: return piclocation elif (width != None and height == None) or (width == None and height != None) or (width != None and height != None): modpic = gtk.gdk.pixbuf_new_from_file(piclocation) pic_width = modpic.get_width() pic_height = modpic.get_height() pic_ratio = pic_width/pic_height if width != None and height == None: # Change width of image new_width = width new_height = width * pic_ratio elif width == None and height != None: # Change height of image new_width = width * pic_ratio new_height = height elif width != None and height != None: # Change height and width of image new_width = width new_height = height modpic = gtk.gdk.pixbuf_new_from_file_at_size(piclocation, new_width, new_height) try: modpic.save(modpiclocation, "jpeg", {"quality":"100"}) except: print "**pywoot: Error: Unable to save modified picture of the current Woot" return '' else: return modpiclocation else: sys.exit("Error: Unhandled width/height clause\nWidth: " + width + "\nHeight: " + height) if __name__ == '__main__': (price, item, status) = getwoot() print "This program is used as a module, here is an example of what it does:" print "Current Woot Info: " + item + " is " + status.lower() + " and on sale for " + price + "."