diff --git a/plugins/Amazon.py b/plugins/Amazon.py index 4913eee98..e78fcf073 100644 --- a/plugins/Amazon.py +++ b/plugins/Amazon.py @@ -37,16 +37,16 @@ __revision__ = "$Id$" __author__ = supybot.authors.jamessan import getopt -import supybot.plugins as plugins import amazon -import supybot.registry as registry - import supybot.conf as conf import supybot.utils as utils +from supybot.commands import * +import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.privmsgs as privmsgs +import supybot.registry as registry import supybot.callbacks as callbacks @@ -138,21 +138,17 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): self.log.warning(str(e)) return res - def isbn(self, irc, msg, args): + def isbn(self, irc, msg, args, channel, optlist, isbn): """[--url] Returns the book matching the given ISBN number. If --url is specified, a link to amazon.com's page for the book will also be returned. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False for (option, argument) in optlist: - option = option.lstrip('-') if option == 'url': url = True - isbn = privmsgs.getArgs(rest) isbn = isbn.replace('-', '').replace(' ', '') attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', @@ -162,9 +158,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s, written by %(author)s; published by ' \ '%(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - bold = self.registryValue('bold', chan) - region = self.registryValue('region', chan) + bold = self.registryValue('bold', channel) + region = self.registryValue('region', channel) try: book = amazon.searchByKeyword(isbn, locale=region) res = self._genResults(s, attribs, book, url, bold, 'title') @@ -172,24 +167,20 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No book was found with that ISBN.') + irc.error('No book was found with that ISBN.') + isbn = wrap(isbn, ['channel', getopts({'url':''}), 'text']) - def books(self, irc, msg, args): + def books(self, irc, msg, args, channel, optlist, keyword): """[--url] Returns the books matching the given search. If --url is specified, a link to amazon.com's page for the book will also be returned. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - keyword = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'Authors' : 'author', @@ -198,9 +189,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s, written by %(author)s; published by ' \ '%(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: books = amazon.searchByKeyword(keyword, locale=region) res = self._genResults(s, attribs, books, url, bold, 'title') @@ -208,28 +198,23 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No books were found with that keyword search.') + irc.error('No books were found with that keyword search.') + books = wrap(books, ['channel', getopts({'url':''}), 'text']) - def videos(self, irc, msg, args): + def videos(self, irc, msg, args, channel, optlist, keyword): """[--url] [--{dvd,vhs}] Returns the videos matching the given search. If --url is specified, a link to amazon.com's page for the video will also be returned. Search defaults to using --dvd. """ - opts = ['url'] - products = ['dvd', 'vhs'] - (optlist, rest) = getopt.getopt(args, '', opts + products) url = False product = 'dvd' - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - if option in products: + else: product = option - keyword = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'MpaaRating' : 'mpaa', @@ -240,9 +225,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s (%(media)s), rated %(mpaa)s; released ' \ '%(date)s; published by %(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: videos = amazon.searchByKeyword(keyword, product_line=product, locale=region) @@ -251,33 +235,29 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No videos were found with that keyword search.') + irc.error('No videos were found with that keyword search.') + videos = wrap(videos, ['channel', getopts({'url':'', 'dvd':'', 'vhs':''}), + 'text']) - def asin(self, irc, msg, args): + def asin(self, irc, msg, args, channel, optlist, asin): """[--url] Returns the item matching the given ASIN number. If --url is specified, a link to amazon.com's page for the item will also be returned. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - asin = privmsgs.getArgs(rest) asin = asin.replace('-', '').replace(' ', '') attribs = {'ProductName' : 'title', 'OurPrice' : 'price', 'URL' : 'url' } s = '%(title)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: item = amazon.searchByASIN(asin, locale=region) res = self._genResults(s, attribs, item, url, bold, 'title') @@ -285,10 +265,10 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No item was found with that ASIN.') + irc.error('No item was found with that ASIN.') + asin = wrap(asin, ['channel', getopts({'url':''}), 'text']) - def upc(self, irc, msg, args): + def upc(self, irc, msg, args, channel, optlist, upc): """[--url] Returns the item matching the given UPC number. If --url is @@ -297,14 +277,10 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): search: music, classical, software, dvd, video, vhs, electronics, pc-hardware, and photo. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - upc = privmsgs.getArgs(rest) upc = upc.replace('-', '').replace(' ', '') attribs = {'ProductName' : 'title', 'Manufacturer' : 'manufacturer', @@ -312,9 +288,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): 'URL' : 'url' } s = '%(title)s %(manufacturer)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: item = amazon.searchByUPC(upc, locale=region) res = self._genResults(s, attribs, item, url, bold, 'title') @@ -322,24 +297,20 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No item was found with that UPC.') + irc.error('No item was found with that UPC.') + upc = wrap(upc, ['channel', getopts({'url':''}), 'text']) - def author(self, irc, msg, args): + def author(self, irc, msg, args, channel, optlist, author): """[--url] Returns a list of books written by the given author. If --url is specified, a link to amazon.com's page for the book will also be returned. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False for (option, argument) in optlist: - option = option.lstrip('-') if option == 'url': url = True - author = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'Authors' : 'author', @@ -348,9 +319,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s, written by %(author)s; published by ' \ '%(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: books = amazon.searchByAuthor(author, locale=region) res = self._genResults(s, attribs, books, url, bold, 'title') @@ -358,8 +328,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No books were found by that author.') + irc.error('No books were found by that author.') + author = wrap(author, ['channel', getopts({'url':''}), 'text']) # FIXME: Until I get a *good* list of categories (ones that actually work), # these commands will remain unavailable @@ -379,8 +349,9 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): cats = self._textToNode.keys() cats.sort() irc.reply(utils.commaAndify(cats)) + categories = wrap(categories) - def bestsellers(self, irc, msg, args): + def bestsellers(self, irc, msg, args, channel, optlist, category): """[--url] Returns a list of best selling items in . The 'categories' @@ -388,14 +359,10 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): is specified, a link to amazon.com's page for the item will also be returned. """ - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', opts) url = False - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - category = privmsgs.getArgs(rest).lower() if category not in self._textToNode: irc.error('An invalid category was specified. The categories' ' command will return a list of valid categories') @@ -407,39 +374,33 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '"%(title)s", from %(publisher)s.%(url)s' try: - #self.log.warning(category) items = amazon.browseBestSellers(category) - #self.log.warning(items) res = self._genResults(s, attribs, items, url) if res: irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No items were found on that best seller list.') + irc.error('No items were found on that best seller list.') + bestsellers = wrap(bestsellers, + ['channel', getopts({'url':''}), rest('lowered')]) ''' - def artist(self, irc, msg, args): + def artist(self, irc, msg, args, channel, optlist, artist): """[--url] [--{music,classical}] Returns a list of items by the given artist. If --url is specified, a link to amazon.com's page for the match will also be returned. The search defaults to using --music. """ - products = ['music', 'classical'] - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', products + opts) url = False - product = '' - for (option, argument) in optlist: - option = option.lstrip('-') + product = None + for (option, _) in optlist: if option == 'url': url = True - if option in products: + else: product = option product = product or 'music' - artist = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'Artists' : 'artist', @@ -449,9 +410,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s (%(media)s), by %(artist)s; published by ' \ '%(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: items = amazon.searchByArtist(artist, product_line=product, locale=region) @@ -460,29 +420,26 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No items were found by that artist.') + irc.error('No items were found by that artist.') + artist = wrap(artist, ['channel', + getopts({'music':'', 'classical':'', 'url':''}), + 'text']) - def actor(self, irc, msg, args): + def actor(self, irc, msg, args, channel, optlist, actor): """[--url] [--{dvd,vhs,video}] Returns a list of items starring the given actor. If --url is specified, a link to amazon.com's page for the match will also be returned. The search defaults to using --dvd. """ - products = ['dvd', 'video', 'vhs'] - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', products + opts) url = False product = '' - for (option, argument) in optlist: - option = option.lstrip('-') + for (option, _) in optlist: if option == 'url': url = True - if option in products: + else: product = option product = product or 'dvd' - actor = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'MpaaRating' : 'mpaa', @@ -493,9 +450,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s (%(media)s), rated %(mpaa)s; released ' \ '%(date)s; published by %(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: items = amazon.searchByActor(actor, product_line=product, locale=region) @@ -504,29 +460,26 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No items were found starring that actor.') + irc.error('No items were found starring that actor.') + actor = wrap(actor, ['channel', + getopts({'dvd': '', 'video': '', 'vhs':'', 'url':''}), + 'text']) - def director(self, irc, msg, args): + def director(self, irc, msg, args, channel, optlist, director): """[--url] [--{dvd,vhs,video}] Returns a list of items by the given director. If --url is specified, a link to amazon.com's page for the match will also be returned. The search defaults to using --dvd. """ - products = ['dvd', 'video', 'vhs'] - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', products + opts) url = False - product = '' - for (option, argument) in optlist: - option = option.lstrip('-') + product = None + for (option, _) in optlist: if option == 'url': url = True - if option in products: + else: product = option product = product or 'dvd' - director = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'Manufacturer' : 'publisher', 'MpaaRating' : 'mpaa', @@ -537,9 +490,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): } s = '%(title)s (%(media)s), rated %(mpaa)s; released ' \ '%(date)s; published by %(publisher)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: items = amazon.searchByDirector(director, product_line=product, locale=region) @@ -548,10 +500,12 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No items were found by that director.') + irc.error('No items were found by that director.') + director = wrap(director, ['channel', getopts({'dvd': '', 'video': '', + 'vhs': '', 'url': ''}), + 'text']) - def manufacturer(self, irc, msg, args): + def manufacturer(self, irc, msg, args, channel, optlist, manufacturer): """ [--url] \ [--{pc-hardware,kitchen,electronics,videogames,software,photo}] \ @@ -560,28 +514,21 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): specified, a link to amazon.com's page for the match will also be returned. The search defaults to using --pc-hardware. """ - products = ['electronics', 'kitchen', 'videogames', 'software', - 'photo', 'pc-hardware'] - opts = ['url'] - (optlist, rest) = getopt.getopt(args, '', products + opts) url = False - product = '' - for (option, argument) in optlist: - option = option.lstrip('-') + product = None + for (option, _) in optlist: if option == 'url': url = True - if option in products: + else: product = option product = product or 'pc-hardware' - manufacturer = privmsgs.getArgs(rest) attribs = {'ProductName' : 'title', 'OurPrice' : 'price', 'URL' : 'url' } s = '%(title)s; price: %(price)s%(url)s' - chan = msg.args[0] - region = self.registryValue('region', chan) - bold = self.registryValue('bold', chan) + region = self.registryValue('region', channel) + bold = self.registryValue('bold', channel) try: items = amazon.searchByManufacturer(manufacturer, product_line=product, @@ -591,8 +538,14 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(utils.commaAndify(res)) return except amazon.AmazonError, e: - pass - irc.error('No items were found by that manufacturer.') + irc.error('No items were found by that manufacturer.') + manufacturer = wrap(manufacturer, + ['channel', getopts( + {'url':'', 'electronics':'', 'kitchen':'', + 'videogames':'', 'software':'', 'photo':'', + 'pc-hardware':'', + }), + 'text']) def amzSnarfer(self, irc, msg, match): r"http://www.amazon.com/exec/obidos/(?:tg/detail/-/|ASIN/)([^/]+)" @@ -623,8 +576,8 @@ class Amazon(callbacks.PrivmsgCommandAndRegexp): irc.reply(res, prefixName=False) return except amazon.AmazonError, e: - pass - self.log.warning('No item was found with that ASIN.') + self.log.debug('No item was found with that ASIN.') + amzSnarfer = urlSnarfer(amzSnarfer) Class = Amazon diff --git a/test/test_Amazon.py b/test/test_Amazon.py index 21ff14df1..dfcb4dec4 100644 --- a/test/test_Amazon.py +++ b/test/test_Amazon.py @@ -32,7 +32,7 @@ from testsupport import * LICENSE_KEY = 'AMAZONS_NOT_CHECKING_KEYS' if LICENSE_KEY != 'INITIAL_NON_LICENSE_KEY' and network: - class AmazonTestCase(PluginTestCase): + class AmazonTestCase(ChannelPluginTestCase): plugins = ('Amazon',) def setUp(self): PluginTestCase.setUp(self)