def main(num_items, heading_level, args): """Main routine""" # Retrieve the contents of the API key file apikey = get_api_config('.amznrc') # Create AmazonScraper object using API key amznscpr = AmazonScraper(*apikey) # Check keyword list entered on the command line if len(args) < 1: print('Missing search terms. For usage help: python amznsrch.py -h') sys.exit(1) # Loop through quoted lists of search terms from command line arguments for arg in args: # Print search terms as a markdown heading srch_terms = str(arg) if heading_level > 0 and heading_level < 7: print '\n' + '#' * heading_level + ' ' + srch_terms + '\n' # Fetch and return results for item in itertools.islice( amznscpr.search(Keywords=srch_terms, SearchIndex='Books'), num_items): # Skip if no title, else encode, remove parenthetical text, & quote if not item.title: continue else: bktitle = item.title.encode('utf8') bktitle = re.sub('\s*[(\[].*[)\]]', '', bktitle) bktitlesrch = urllib.quote_plus('"' + bktitle + '"') # Encode author, if present, and format for printing if not item.author: bkauthor = '' else: bkauthor = 'by ' + item.author.encode('utf8') # Add associate tag to item URL bkurl = str(item.url) + '/?tag=' + apikey[2] # Construct links as desired amzn = '[AMZN](' + bkurl + ')' goog = ('[GOOG]' + '(https://www.google.com/' + 'search?tbo=p&tbm=bks&q=intitle:' + bktitlesrch + '&num=10&gws_rd=ssl)') spl = ('[SPL](https://seattle.bibliocommons.com/search?' + 't=title&search_category=title&q=' + bktitlesrch + '&commit=Search)') uwl = ('[UW](http://alliance-primo.hosted.exlibrisgroup.com/' + 'primo_library/libweb/action/search.do?fn=search&' + 'ct=search&vid=UW&vl%28753972432UI0%29=title&' + 'vl%281UIStartWith0%29=starts+with&vl%28freeText0%29=' + bktitlesrch + '&Submit=Search)') # Searching UW Libraries through WorldCat to be deprecated 2015-09 #uwl = ('[UW](http://uwashington.worldcat.org' + # '/search?q=ti%3A' + bktitlesrch + '&qt=advanced)') # Print markdown for title, author, and links as bulleted list item print('- _' + bktitle + '_ ' + bkauthor + ' ( ' + goog + ' | ' + amzn + ' | ' + spl + ' | ' + uwl + ' )')
def main(num_items, heading_level, args): """Main routine""" # Retrieve the contents of the API key file apikey = get_api_config(".amznrc") # Create AmazonScraper object using API key amznscpr = AmazonScraper(*apikey) # Check keyword list entered on the command line if len(args) < 1: print ("Missing search terms. For usage help: python amznsrch.py -h") sys.exit(1) # Loop through quoted lists of search terms from command line arguments for arg in args: # Print search terms as a markdown heading srch_terms = str(arg) if heading_level > 0 and heading_level < 7: print "\n" + "#" * heading_level + " " + srch_terms + "\n" # Fetch and return results for item in itertools.islice(amznscpr.search(Keywords=srch_terms, SearchIndex="Books"), num_items): # Skip if no title, else encode, remove parenthetical text, & quote if not item.title: continue else: bktitle = item.title.encode("utf8") bktitle = re.sub("\s*[(\[].*[)\]]", "", bktitle) bktitlesrch = urllib.quote_plus('"' + bktitle + '"') # Encode author, if present, and format for printing if not item.author: bkauthor = "" else: bkauthor = "by " + item.author.encode("utf8") # Add associate tag to item URL bkurl = str(item.url) + "/?tag=" + apikey[2] # Construct links as desired amzn = "[AMZN](" + bkurl + ")" goog = ( "[GOOG]" + "(https://www.google.com/" + "search?tbo=p&tbm=bks&q=intitle:" + bktitlesrch + "&num=10&gws_rd=ssl)" ) spl = ( "[SPL](https://seattle.bibliocommons.com/search?" + "t=title&search_category=title&q=" + bktitlesrch + "&commit=Search)" ) uwl = ( "[UW](http://alliance-primo.hosted.exlibrisgroup.com/" + "primo_library/libweb/action/search.do?fn=search&" + "ct=search&vid=UW&vl%28753972432UI0%29=title&" + "vl%281UIStartWith0%29=starts+with&vl%28freeText0%29=" + bktitlesrch + "&Submit=Search)" ) # Searching UW Libraries through WorldCat to be deprecated 2015-09 # uwl = ('[UW](http://uwashington.worldcat.org' + # '/search?q=ti%3A' + bktitlesrch + '&qt=advanced)') # Print markdown for title, author, and links as bulleted list item print ("- _" + bktitle + "_ " + bkauthor + " ( " + goog + " | " + amzn + " | " + spl + " | " + uwl + " )")