#!/usr/bin/python
# setup SOAP proxy
import SOAPpy
file = 'AmazonWebServices.wsdl'
amazon = SOAPpy.WSDL.Proxy(file)
# import the wxPython libraries
import wxPython.wx
from wxPython.wx import *
import urllib
# developer token for amazon api
DEV_TOKEN = 'INSERT YOUR TOKEN HERE'
# event ids
ID_SEARCH = 100
ID_SELCHG = 101
#------------------------------------------------------------------------------
# amazonWidgetFrame - the main window for our app
#------------------------------------------------------------------------------
class amazonWidgetFrame(wxFrame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(500, 475))
# add a status bar
self.CreateStatusBar()
self.SetStatusText("")
# add other widgets to frame
frame_box = wxBoxSizer(wxVERTICAL)
# query part
query_box = wxBoxSizer(wxHORIZONTAL)
# set the mode (product line to search)
mode_box =
wxStaticBoxSizer(wxStaticBox(self, -1, "Product Line"), wxVERTICAL)
self.mode_books =
wxRadioButton(self, -1, "books",
wxDefaultPosition, wxDefaultSize, wxRB_GROUP)
self.mode_music = wxRadioButton(self, -1, "music",
wxDefaultPosition, wxDefaultSize)
self.mode_dvds = wxRadioButton(self, -1, "dvd",
wxDefaultPosition, wxDefaultSize)
mode_box.Add(self.mode_books, 0, wxLEFT|wxRIGHT, 10)
mode_box.Add(self.mode_music, 0, wxLEFT|wxRIGHT, 10)
mode_box.Add(self.mode_dvds, 0, wxLEFT|wxRIGHT, 10)
query_box.Add(mode_box, 0, wxALL, 5)
# edit and button
qtext_box =
wxStaticBoxSizer(wxStaticBox(self, -1, "Enter Query"), wxHORIZONTAL)
self.search_text =
wxTextCtrl(self, -1, "python web", wxDefaultPosition, (200, -1))
qtext_box.Add(self.search_text, 1, wxALL, 5)
qtext_box.Add(wxButton(self, ID_SEARCH, "Search Amazon"), 0, wxALL, 5)
query_box.Add(qtext_box, 0, wxALL, 5)
frame_box.Add(query_box, 0, wxBOTTOM, 5)
# results part
results_box = wxBoxSizer(wxHORIZONTAL)
# details
details_box =
wxStaticBoxSizer(wxStaticBox(self, -1, "Query Results"), wxVERTICAL)
self.results_list =
wxListBox(self, ID_SELCHG, wxDefaultPosition, (300,175),
[], wxLB_ALWAYS_SB)
details_box.Add(self.results_list, 0, wxALL, 5)
hbox = wxBoxSizer(wxHORIZONTAL)
tbox = wxBoxSizer(wxVERTICAL)
tbox.Add(wxStaticText(self, -1, "Author/Artist: "), 0, wxLEFT, 5)
tbox.Add(wxStaticText(self, -1, ""), 0, wxLEFT, 5)
hbox.Add(tbox, 0)
hbox.Add(20,0,1)
self.authors = wxListBox(self, -1, wxDefaultPosition, (200,65), [])
hbox.Add(self.authors, 0, wxALIGN_RIGHT)
details_box.Add(hbox, 0, wxBOTTOM, 5)
hbox = wxBoxSizer(wxHORIZONTAL)
hbox.Add(wxStaticText(self, -1, "Release Date: "), 0, wxLEFT, 5)
self.release_date = wxStaticText(self, -1, " ",
wxDefaultPosition, wxDefaultSize)
hbox.Add(self.release_date, 0)
details_box.Add(hbox, 0)
hbox = wxBoxSizer(wxHORIZONTAL)
hbox.Add(wxStaticText(self, -1, "List Price: "), 0, wxLEFT, 5)
self.list_price = wxStaticText(self, -1, " ",
wxDefaultPosition, wxDefaultSize)
hbox.Add(self.list_price, 0)
details_box.Add(hbox, 0)
hbox = wxBoxSizer(wxHORIZONTAL)
hbox.Add(wxStaticText(self, -1, "Amazon.com Price: "), 0, wxLEFT, 5)
self.amazon_price = wxStaticText(self, -1, " ",
wxDefaultPosition, wxDefaultSize)
hbox.Add(self.amazon_price, 0)
details_box.Add(hbox, 0, wxBOTTOM, 5)
results_box.Add(details_box, 0, wxALL, 5)
# image
image_box =
wxStaticBoxSizer(wxStaticBox(self, -1, "Product Image"), wxHORIZONTAL)
# inline class for drawing our product image
class amazonImagePanel(wxPanel):
def __init__(self, parent, id, position, size):
wxPanel.__init__(self, parent, id, position, size)
self.parent = parent
self.image = None
EVT_PAINT(self, self.OnPaint)
def OnPaint(self, evt):
dc = wxPaintDC(self)
if self.image:
bitmap = wxBitmapFromImage(self.image)
dc.DrawBitmap(bitmap, 0, 0, false)
self.image_canvas = amazonImagePanel(self, -1, wxDefaultPosition, (130,150))
image_box.Add(self.image_canvas, 1, wxALL, 5)
results_box.Add(image_box, 0, wxALL, 5)
# finally...
frame_box.Add(results_box, 0, wxTOP, 5)
# add main sizer
self.SetSizer(frame_box)
# set event handlers
EVT_BUTTON(self, ID_SEARCH, self.OnSearch)
EVT_LISTBOX(self, ID_SELCHG, self.OnSelectionChange)
#---------------------------------------------------------------------
# build and call query - if returns OK, set the GUI fields
#---------------------------------------------------------------------
def OnSearch(self, event):
# fetch mode and query text
query = self.search_text.GetValue()
self.mode = 'books'
if self.mode_music.GetValue():
self.mode = 'music'
elif self.mode_dvds.GetValue():
self.mode = 'dvd'
request = { 'keyword': query, 'page': '1', 'mode': self.mode,
'tag': '', 'type': 'lite', 'devtag': DEV_TOKEN }
# do the query
try:
tmp_results = amazon.KeywordSearchRequest(request)
except SOAPpy.faultType:
self.SetStatusText('There were no exact matches for the search')
else:
self.results = tmp_results
self.SetStatusText('total results: ' + str(self.results.TotalResults) +
' (only showing first 10)')
# load up results box
items = []
images = []
for detail in self.results.Details:
items.append(detail['ProductName'])
images.append(detail['ImageUrlMedium'])
self.results_list.Set(items)
self.results_list.SetFirstItem(0)
# clear displayed image
self.authors.Set([])
self.release_date.SetLabel('')
self.list_price.SetLabel('')
self.amazon_price.SetLabel('')
self.image_canvas.image = None
self.image_canvas.Refresh()
# cache images
self.loadImages(images)
#---------------------------------------------------------------------
# whenever the product selection changes, update other fields
#---------------------------------------------------------------------
def OnSelectionChange(self, event):
# fetch selected item from results
sel = self.results_list.GetSelections()
detail = self.results.Details[sel[0]]
# set the author/artist field
items = []
if self.mode == 'books':
if 'Authors' in detail._keys():
authors = detail['Authors']
for author in authors:
items.append(author)
self.authors.Set(items)
elif self.mode == 'music':
if 'Artists' in detail._keys():
authors = detail['Artists']
for author in authors:
items.append(author)
self.authors.Set(items)
elif self.mode == 'dvd':
# only in 'heavy' search type
if 'Directors' in detail._keys():
authors = detail['Directors']
for author in authors:
items.append(author)
self.authors.Set(items)
# set release date and pricing fields
if 'ReleaseDate' in detail._keys():
self.release_date.SetLabel(detail['ReleaseDate'])
if 'ListPrice' in detail._keys():
self.list_price.SetLabel(detail['ListPrice'])
if 'OurPrice' in detail._keys():
self.amazon_price.SetLabel(detail['OurPrice'])
# fetch image
image_name = 'image_tmp_' + str(sel[0]) + '.jpg'
self.image_canvas.image = wxImage(image_name)
self.image_canvas.Refresh()
#---------------------------------------------------------------------
# retrieve from amazon.com and temporarily save images
#---------------------------------------------------------------------
def loadImages(self, images):
i = 0
for url in images:
image_name = 'image_tmp_' + str(i) + '.jpg'
pic = urllib.urlopen(url)
img = pic.read()
pic.close()
f = open(image_name,'wb')
f.write(img)
f.close()
i = i+1
#------------------------------------------------------------------------------
# simple wxApp
#------------------------------------------------------------------------------
class amazonWidgetApp(wxApp):
def __init__(self, parent):
wxApp.__init__(self, parent)
def OnInit(self):
# start GUI
frame = amazonWidgetFrame(NULL, -1, "Amazon.com SOAP Widget")
frame.Show(true)
self.SetTopWindow(frame)
return true
#------------------------------------------------------------------------------
# run this when module gets called
#------------------------------------------------------------------------------
if __name__ == '__main__':
# must call this for wxImage to load JPEGs
wx.wxInitAllImageHandlers()
app = amazonWidgetApp(0)
app.MainLoop()
|