diff --git a/plugins/Amazon.py b/plugins/Amazon.py index 36f466aeb..658d2016d 100644 --- a/plugins/Amazon.py +++ b/plugins/Amazon.py @@ -136,6 +136,76 @@ class Amazon(callbacks.Privmsg): pass irc.error(msg, 'No book was found with that ISBN.') + def books(self, irc, msg, args): + """[--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('-') + if option == 'url': + url = True + keyword = privmsgs.getArgs(rest) + attribs = {'ProductName' : 'title', + 'Manufacturer' : 'publisher', + 'Authors' : 'author', + 'URL' : 'url' + } + s = '"%(title)s", written by %(author)s; published by '\ + '%(publisher)s%(url)s' + try: + books = amazon.searchByKeyword(keyword) + res = self._genResults(s, attribs, books, url) + if res: + irc.reply(msg, utils.commaAndify(res)) + return + except amazon.AmazonError, e: + pass + irc.error(msg, 'No books were found with that keyword search.') + + def videos(self, irc, msg, args): + """[--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('-') + if option == 'url': + url = True + if option in products: + product = option + keyword = privmsgs.getArgs(rest) + attribs = {'ProductName' : 'title', + 'Manufacturer' : 'publisher', + 'MpaaRating' : 'mpaa', + 'Media' : 'media', + 'ReleaseDate' : 'date', + 'URL' : 'url' + } + s = '"%(title)s" (%(media)s), rated %(mpaa)s; released '\ + '%(date)s; published by %(publisher)s%(url)s' + try: + videos = amazon.searchByKeyword(keyword, product_line=product) + res = self._genResults(s, attribs, videos, url) + if res: + irc.reply(msg, utils.commaAndify(res)) + return + except amazon.AmazonError, e: + pass + irc.error(msg, 'No videos were found with that keyword search.') + def asin(self, irc, msg, args): """[--url] diff --git a/test/test_Amazon.py b/test/test_Amazon.py index 9c37931c4..d6260684d 100644 --- a/test/test_Amazon.py +++ b/test/test_Amazon.py @@ -90,5 +90,14 @@ class AmazonTestCase(PluginTestCase, PluginDocumentation): self.assertRegexp('manufacturer --software adobe', r'Photoshop') self.assertRegexp('manufacturer --photo kodak', r'Kodak') + def testBooks(self): + self.assertHelp('books') + self.assertRegexp('books linux', r'Linux Kernel Development') + + def testVideos(self): + self.assertHelp('videos') + self.assertRegexp('videos zim', r'Demystifying the Devil.*DVD') + self.assertRegexp('videos --vhs samuel jackson', r'VHS Tape') + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: