From 670d80eedd4a4728e3207a4024993fc0996734b0 Mon Sep 17 00:00:00 2001 From: "Avinash H. Duduskar" <2946372+Strykar@users.noreply.github.com> Date: Sun, 23 Apr 2023 15:44:26 +0530 Subject: [PATCH] bash_quote.py: add two more switches If you don't fancy this PR, I would appreciate it if you would add these features. Refactored script now also features: ``` -r MIN MAX, --range MIN MAX Filter quotes by vote range (min,max). positional arguments: quote_id Retrieve a single quote by its ID. ``` --- bash_quote/bash_quote.py | 83 ++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/bash_quote/bash_quote.py b/bash_quote/bash_quote.py index 15a3836..a9c3edb 100755 --- a/bash_quote/bash_quote.py +++ b/bash_quote/bash_quote.py @@ -4,7 +4,6 @@ Browse bash.org from the comfort of your shell.\ ''' - from __future__ import print_function, unicode_literals from bs4 import BeautifulSoup from sys import argv @@ -13,52 +12,52 @@ import requests - -def get_quotes(option='r', num_quotes=1): - '''\ - Get a list of quotes from bash.org. - option - which type of quotes you would like, (t)op, (l)atest or (r)andom - num_quotes - number of quotes that you want printed, integer\ - ''' +def get_quotes(option='r', num_quotes=1, vote_range=None, quote_id=None): base_url = 'http://bash.org/?' options_map = {'r': 'random1', 't': 'top2', 'l': 'latest'} - if not option: - option = 'r' + if quote_id: + url = base_url + quote_id + else: + url = base_url + options_map.get(option[0].lower(), 'random1') + + page = BeautifulSoup(requests.get(url).text, features="html5lib") - url = base_url + options_map.get(option[0].lower(), 'random1') - page = BeautifulSoup(requests.get(url).text) + quote_info = page.find_all('p', attrs={'class': 'quote'}) + quotes = page.find_all('p', attrs={'class': 'qt'}) - quote_info = page.find_all('p', - attrs={'class': 'quote'}) - quotes = page.find_all('p', - attrs={'class': 'qt'}) + if vote_range: + min_votes, max_votes = vote_range + filtered_quotes = [] + filtered_info = [] + for info, quote in zip(quote_info, quotes): + votes = int(info.find('font').get_text().strip('()')) + if min_votes <= votes <= max_votes: + filtered_info.append(info) + filtered_quotes.append(quote) + quote_info, quotes = filtered_info, filtered_quotes return quote_info, quotes -def print_quotes(option='r', num_quotes=1): - '''\ - Print quotes from bash.org. - option - which type of quotes you would like, (t)op, (l)atest or (r)andom - num_quotes - number of quotes that you want printed, integer\ - ''' - quote_info, quotes = get_quotes(option, num_quotes) - num_quotes_to_return = min(abs(num_quotes), 50) - for quote_num in sample(range(len(quotes)), num_quotes_to_return): - info = quote_info[quote_num] - quote = quotes[quote_num] - print('{0}\n{1}'.format(info.get_text()[:-5], quote.get_text()), - end='\n\n') +def print_quotes(option='r', num_quotes=1, vote_range=None, quote_id=None): + quote_info, quotes = get_quotes(option, num_quotes, vote_range, quote_id) + if quote_id: + if quote_info and quotes: + print('{0}\n{1}'.format(quote_info[0].get_text()[:-5], quotes[0].get_text()), end='\n\n') + else: + print("Quote not found.") + else: + num_quotes_to_return = min(abs(num_quotes), 50) + for quote_num in sample(range(len(quotes)), num_quotes_to_return): + info = quote_info[quote_num] + quote = quotes[quote_num] + print('{0}\n{1}'.format(info.get_text()[:-5], quote.get_text()), end='\n\n') def main(): - '''\ - Parse command line options and run get_quotes with options specified by the - user\ - ''' parser = ArgumentParser() n_help = 'Number of quotes you want returned' parser.add_argument('-n', @@ -77,8 +76,24 @@ def main(): dest='option', choices=('random', 'r', 'top', 't', 'latest', 'l')) + r_help = 'Filter quotes by vote range (min,max).' + parser.add_argument('-r', + '--range', + nargs=2, + help=r_help, + default=None, + dest='vote_range', + type=int, + metavar=('MIN', 'MAX')) + + parser.add_argument('quote_id', + nargs='?', + help='Retrieve a single quote by its ID.', + default=None, + type=str) + args = parser.parse_args(argv[1:]) - print_quotes(args.option, args.num_quotes) + print_quotes(args.option, args.num_quotes, args.vote_range, args.quote_id) if __name__ == '__main__':