From 17c4c016034d438191b5e695a11cc8079dbe1649 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Tue, 9 Jul 2019 16:35:34 +0530 Subject: [PATCH 1/7] Patch : Fixes issue 14 and 18. --- pyquotes/brainyquote/updated_brainyquote.py | 121 ++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 pyquotes/brainyquote/updated_brainyquote.py diff --git a/pyquotes/brainyquote/updated_brainyquote.py b/pyquotes/brainyquote/updated_brainyquote.py new file mode 100644 index 0000000..cfe4e68 --- /dev/null +++ b/pyquotes/brainyquote/updated_brainyquote.py @@ -0,0 +1,121 @@ +import requests +from bs4 import BeautifulSoup +import html5lib # Html Parser +import re +import random + +# This function returns the link to be scraped for the author. +# For example if the author is Bill Gates then url at brainyquote +# will be "https://www.brainyquote.com/authors/bill_gates" + + +def get_author_link(person): + author_name = person.lower() + author_name_split = author_name.split(' ') + author_url_link = '' + count = 0 + + for i in author_name_split: + author_url_link += i + count += 1 + if count is not len(author_name_split): + author_url_link += '_' + + author_url_link = author_url_link.replace('.', '_') + + return author_url_link + + +def get_quotes(person, category): + """ + This function returns all the quotes that matches the input. + :param person: Name of the person e.g. Albert Einstein + :param category: Category of quote e.g. Motivational + :param return: List of tuples [(quote, author_of_the_quote), ..] + """ + URL = "https://www.brainyquote.com/authors/" + get_author_link(person) + respone_author = requests.get(URL) + soup_author = BeautifulSoup(respone_author.content, 'html5lib') + categories = soup_author.find_all('div', class_='kw-box') + check = False + count = 0 + for i in categories: + a = i.text + replace = a.replace("\n", '') + r = replace.lower() + if category in r: + check = True + count += 1 + + # Getting the quote of the related author + get_quote = soup_author.find_all('a', attrs={'title': 'view quote'}) + quote_list = [] + big_list = [] + for i in range(count): + quote_list.append(get_quote[i].text) + big_list.append(quote_list) + + if len(quote_list) == 0: + return('''Oops! It seems that there are no quotes of the author of that + category. + \nYou may consider changing the category or the author ''') + quote_list.append(person) + + return(quote_list) + + +def get_quote(person, category): + """ + This function take a category and a person as a input and returns + a random quote which matches the input. + :param person: Name of the person e.g. Albert Einstein + :param category: Category of quote e.g. Motivational + :param return: A tuple (quote, author_of_the_quote) + """ + quotes = get_quotes(person, category) + length = len(quotes) + if(length == 0): + # In case no quote of the author exist for that category. + return("No quotes found of that category") + else: + random_number = random.randint(0, length - 1) + list = [] + list.append(quotes[random_number]) + list.append(person) + + return(tuple(list)) + + +def get_quote_of_the_day(): + """ + This fuction returns quote of the day. + :param return: A tuple (quote, author_of_the_quote) + """ + URL = "https://www.brainyquote.com/quote_of_the_day" + + # Sending a HTTP request to the specified URL and saving the response + # from server in a response object called response. + response = requests.get(URL) + soup = BeautifulSoup(response.content, 'html5lib') + a_tags = soup.findAll('img', alt=True) + + # Getting all the a tags of the page. + quote_of_the_day_atag = str(a_tags[0]) + + # Grabbing the first a tag of the page + matches = re.findall(r'\"(.+?)\"', quote_of_the_day_atag) + + # A regular expression which gives a list of all + # text that is in between quotes. + quote_author_split_list = str(matches[0]).split('-') + + # Get a list of quote_of_the_day and the author + quote_of_the_day = matches[0].replace(quote_author_split_list[-1], '') + quote_of_the_day = quote_of_the_day.replace('-', '') + author_name = quote_author_split_list[-1] + + # Gives the author_name + author_name = author_name.replace(' ', '') + + # Removes any extra space + return(quote_of_the_day, author_name) From 4472dd12c949e98cef2f83b1ae4541dec1c4b756 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Tue, 9 Jul 2019 16:39:15 +0530 Subject: [PATCH 2/7] Patch : Fixed issue 14 and 18. --- pyquotes/brainyquote/updated_brainyquote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyquotes/brainyquote/updated_brainyquote.py b/pyquotes/brainyquote/updated_brainyquote.py index cfe4e68..ccad2cb 100644 --- a/pyquotes/brainyquote/updated_brainyquote.py +++ b/pyquotes/brainyquote/updated_brainyquote.py @@ -5,7 +5,7 @@ import random # This function returns the link to be scraped for the author. -# For example if the author is Bill Gates then url at brainyquote +# for example if the author is Bill Gates then url at brainyquote # will be "https://www.brainyquote.com/authors/bill_gates" From 3551beeb5eaf8057a570b5ba13fecd765b46e8f0 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Sun, 14 Jul 2019 12:28:22 +0530 Subject: [PATCH 3/7] Patches Updated the function so that if the category is not given then the function returns all the quotes of the person specified, --- pyquotes/brainyquote/updated_brainyquote.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pyquotes/brainyquote/updated_brainyquote.py b/pyquotes/brainyquote/updated_brainyquote.py index ccad2cb..18753af 100644 --- a/pyquotes/brainyquote/updated_brainyquote.py +++ b/pyquotes/brainyquote/updated_brainyquote.py @@ -5,7 +5,7 @@ import random # This function returns the link to be scraped for the author. -# for example if the author is Bill Gates then url at brainyquote +# For example if the author is Bill Gates then url at brainyquote # will be "https://www.brainyquote.com/authors/bill_gates" @@ -26,9 +26,11 @@ def get_author_link(person): return author_url_link -def get_quotes(person, category): +def get_quotes(person, category=None): """ This function returns all the quotes that matches the input. + If category is not specified then it returns all the quotes + of the person. :param person: Name of the person e.g. Albert Einstein :param category: Category of quote e.g. Motivational :param return: List of tuples [(quote, author_of_the_quote), ..] @@ -36,6 +38,17 @@ def get_quotes(person, category): URL = "https://www.brainyquote.com/authors/" + get_author_link(person) respone_author = requests.get(URL) soup_author = BeautifulSoup(respone_author.content, 'html5lib') + + all_quotes_list = [] + + if category is None: + get_all_quotes = soup_author.find_all('a', + attrs={'title': 'view quote'}) + for i in range(len(get_all_quotes)): + all_quotes_list.append(get_all_quotes[i].text) + all_quotes_list.append(person) + return all_quotes_list + categories = soup_author.find_all('div', class_='kw-box') check = False count = 0 @@ -64,7 +77,7 @@ def get_quotes(person, category): return(quote_list) -def get_quote(person, category): +def get_quote(person, category=None): """ This function take a category and a person as a input and returns a random quote which matches the input. @@ -119,3 +132,5 @@ def get_quote_of_the_day(): # Removes any extra space return(quote_of_the_day, author_name) + +print(get_quote('Albert Einstein')) From 08c128919ac2f8b2d9aec1293a1b2ef87ef5f3c0 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Sun, 14 Jul 2019 12:29:26 +0530 Subject: [PATCH 4/7] Delete updated_brainyquote.py --- pyquotes/brainyquote/updated_brainyquote.py | 136 -------------------- 1 file changed, 136 deletions(-) delete mode 100644 pyquotes/brainyquote/updated_brainyquote.py diff --git a/pyquotes/brainyquote/updated_brainyquote.py b/pyquotes/brainyquote/updated_brainyquote.py deleted file mode 100644 index 18753af..0000000 --- a/pyquotes/brainyquote/updated_brainyquote.py +++ /dev/null @@ -1,136 +0,0 @@ -import requests -from bs4 import BeautifulSoup -import html5lib # Html Parser -import re -import random - -# This function returns the link to be scraped for the author. -# For example if the author is Bill Gates then url at brainyquote -# will be "https://www.brainyquote.com/authors/bill_gates" - - -def get_author_link(person): - author_name = person.lower() - author_name_split = author_name.split(' ') - author_url_link = '' - count = 0 - - for i in author_name_split: - author_url_link += i - count += 1 - if count is not len(author_name_split): - author_url_link += '_' - - author_url_link = author_url_link.replace('.', '_') - - return author_url_link - - -def get_quotes(person, category=None): - """ - This function returns all the quotes that matches the input. - If category is not specified then it returns all the quotes - of the person. - :param person: Name of the person e.g. Albert Einstein - :param category: Category of quote e.g. Motivational - :param return: List of tuples [(quote, author_of_the_quote), ..] - """ - URL = "https://www.brainyquote.com/authors/" + get_author_link(person) - respone_author = requests.get(URL) - soup_author = BeautifulSoup(respone_author.content, 'html5lib') - - all_quotes_list = [] - - if category is None: - get_all_quotes = soup_author.find_all('a', - attrs={'title': 'view quote'}) - for i in range(len(get_all_quotes)): - all_quotes_list.append(get_all_quotes[i].text) - all_quotes_list.append(person) - return all_quotes_list - - categories = soup_author.find_all('div', class_='kw-box') - check = False - count = 0 - for i in categories: - a = i.text - replace = a.replace("\n", '') - r = replace.lower() - if category in r: - check = True - count += 1 - - # Getting the quote of the related author - get_quote = soup_author.find_all('a', attrs={'title': 'view quote'}) - quote_list = [] - big_list = [] - for i in range(count): - quote_list.append(get_quote[i].text) - big_list.append(quote_list) - - if len(quote_list) == 0: - return('''Oops! It seems that there are no quotes of the author of that - category. - \nYou may consider changing the category or the author ''') - quote_list.append(person) - - return(quote_list) - - -def get_quote(person, category=None): - """ - This function take a category and a person as a input and returns - a random quote which matches the input. - :param person: Name of the person e.g. Albert Einstein - :param category: Category of quote e.g. Motivational - :param return: A tuple (quote, author_of_the_quote) - """ - quotes = get_quotes(person, category) - length = len(quotes) - if(length == 0): - # In case no quote of the author exist for that category. - return("No quotes found of that category") - else: - random_number = random.randint(0, length - 1) - list = [] - list.append(quotes[random_number]) - list.append(person) - - return(tuple(list)) - - -def get_quote_of_the_day(): - """ - This fuction returns quote of the day. - :param return: A tuple (quote, author_of_the_quote) - """ - URL = "https://www.brainyquote.com/quote_of_the_day" - - # Sending a HTTP request to the specified URL and saving the response - # from server in a response object called response. - response = requests.get(URL) - soup = BeautifulSoup(response.content, 'html5lib') - a_tags = soup.findAll('img', alt=True) - - # Getting all the a tags of the page. - quote_of_the_day_atag = str(a_tags[0]) - - # Grabbing the first a tag of the page - matches = re.findall(r'\"(.+?)\"', quote_of_the_day_atag) - - # A regular expression which gives a list of all - # text that is in between quotes. - quote_author_split_list = str(matches[0]).split('-') - - # Get a list of quote_of_the_day and the author - quote_of_the_day = matches[0].replace(quote_author_split_list[-1], '') - quote_of_the_day = quote_of_the_day.replace('-', '') - author_name = quote_author_split_list[-1] - - # Gives the author_name - author_name = author_name.replace(' ', '') - - # Removes any extra space - return(quote_of_the_day, author_name) - -print(get_quote('Albert Einstein')) From 28e1e9c6f3cbf5bc6febeb7fdb8880a8abea84a6 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Sun, 14 Jul 2019 12:29:40 +0530 Subject: [PATCH 5/7] Delete brainyquote.py --- pyquotes/brainyquote/brainyquote.py | 124 ---------------------------- 1 file changed, 124 deletions(-) delete mode 100644 pyquotes/brainyquote/brainyquote.py diff --git a/pyquotes/brainyquote/brainyquote.py b/pyquotes/brainyquote/brainyquote.py deleted file mode 100644 index 1da25e1..0000000 --- a/pyquotes/brainyquote/brainyquote.py +++ /dev/null @@ -1,124 +0,0 @@ -import requests -from bs4 import BeautifulSoup -import html5lib # Html Parser -import re -import random - -# This function returns the link to be scraped for the author. -# For example if the author is Bill Gates then url at brainyquote -# will be "https://www.brainyquote.com/authors/bill_gates" - - -def get_author_link(person): - author_name = person.lower() - author_name_split = author_name.split(' ') - author_url_link = '' - count = 0 - - for i in author_name_split: - author_url_link += i - count += 1 - if count is not len(author_name_split): - author_url_link += '_' - - author_url_link = author_url_link.replace('.', '_') - - return author_url_link - - -def get_quotes(person, category): - """ - This function returns all the quotes that matches the input. - - :param person: Name of the person e.g. Albert Einstein - :param category: Category of quote e.g. Motivational - :param return: List of tuples [(quote, author_of_the_quote), ..] - """ - URL = "https://www.brainyquote.com/authors/" + get_author_link(person) - respone_author = requests.get(URL) - soup_author = BeautifulSoup(respone_author.content, 'html5lib') - categories = soup_author.find_all('div', class_='kw-box') - check = False - count = 0 - for i in categories: - a = i.text - replace = a.replace("\n", '') - r = replace.lower() - if category in r: - check = True - count += 1 - - # Getting the quote of the related author - get_quote = soup_author.find_all('a', attrs={'title': 'view quote'}) - quote_list = [] - big_list = [] - for i in range(count): - quote_list.append(get_quote[i].text) - big_list.append(quote_list) - - if len(quote_list) == 0: - return('''Oops! It seems that there are no quotes of the author of that - category. - \nYou may consider changing the category or the author ''') - quote_list.append(person) - - return(quote_list) - - -def get_quote(person, category): - """ - This function take a category and a person as a input and returns - a random quote which matches the input. - - :param person: Name of the person e.g. Albert Einstein - :param category: Category of quote e.g. Motivational - :param return: A tuple (quote, author_of_the_quote) - """ - quotes = get_quotes(person, category) - length = len(quotes) - if(length == 0): - # In case no quote of the author exist for that category. - return("No quotes found of that category") - else: - random_number = random.randint(0, length - 1) - list = [] - list.append(quotes[random_number]) - list.append(person) - - return(tuple(list)) - - -def get_quote_of_the_day(): - """ - This fuction returns quote of the day. - - :param return: A tuple (quote, author_of_the_quote) - """ - URL = "https://www.brainyquote.com/quote_of_the_day" - - # Sending a HTTP request to the specified URL and saving the response - # from server in a response object called response. - response = requests.get(URL) - soup = BeautifulSoup(response.content, 'html5lib') - a_tags = soup.findAll('img', alt=True) - - # Getting all the a tags of the page. - quote_of_the_day_atag = str(a_tags[0]) - - # Grabbing the first a tag of the page - matches = re.findall(r'\"(.+?)\"', quote_of_the_day_atag) - - # A regular expression which gives a list of all - # text that is in between quotes. - quote_author_split_list = str(matches[0]).split('-') - - # Get a list of quote_of_the_day and the author - quote_of_the_day = matches[0].replace(quote_author_split_list[-1], '') - quote_of_the_day = quote_of_the_day.replace('-', '') - author_name = quote_author_split_list[-1] - - # Gives the author_name - author_name = author_name.replace(' ', '') - - # Removes any extra space - return (quote_of_the_day, author_name) From 6a191f6b68ac64e9dea7736ac2648cb6e0a8212d Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Sun, 14 Jul 2019 12:31:00 +0530 Subject: [PATCH 6/7] Patches Updated the function so that if the category is not specified then the function returns all the quotes of the input author. --- pyquotes/brainyquote/brainyquote.py | 135 ++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 pyquotes/brainyquote/brainyquote.py diff --git a/pyquotes/brainyquote/brainyquote.py b/pyquotes/brainyquote/brainyquote.py new file mode 100644 index 0000000..0adad2c --- /dev/null +++ b/pyquotes/brainyquote/brainyquote.py @@ -0,0 +1,135 @@ +import requests +from bs4 import BeautifulSoup +import html5lib # Html Parser +import re +import random + +# This function returns the link to be scraped for the author. +# For example if the author is Bill Gates then url at brainyquote +# will be "https://www.brainyquote.com/authors/bill_gates" + + +def get_author_link(person): + author_name = person.lower() + author_name_split = author_name.split(' ') + author_url_link = '' + count = 0 + + for i in author_name_split: + author_url_link += i + count += 1 + if count is not len(author_name_split): + author_url_link += '_' + + author_url_link = author_url_link.replace('.', '_') + + return author_url_link + + +def get_quotes(person, category = None): + """ + This function returns all the quotes that matches the input. + If category is not specified then it returns all the quotes + of the person. + :param person: Name of the person e.g. Albert Einstein + :param category: Category of quote e.g. Motivational + :param return: List of tuples [(quote, author_of_the_quote), ..] + """ + URL = "https://www.brainyquote.com/authors/" + get_author_link(person) + respone_author = requests.get(URL) + soup_author = BeautifulSoup(respone_author.content, 'html5lib') + + all_quotes_list = [] + + if category is None: + get_all_quotes = soup_author.find_all('a', attrs={'title':'view quote'}) + for i in range(len(get_all_quotes)): + all_quotes_list.append(get_all_quotes[i].text) + all_quotes_list.append(person) + return all_quotes_list + + categories = soup_author.find_all('div', class_='kw-box') + check = False + count = 0 + for i in categories: + a = i.text + replace = a.replace("\n", '') + r = replace.lower() + if category in r: + check = True + count += 1 + + # Getting the quote of the related author + get_quote = soup_author.find_all('a', attrs={'title': 'view quote'}) + quote_list = [] + big_list = [] + for i in range(count): + quote_list.append(get_quote[i].text) + big_list.append(quote_list) + + if len(quote_list) == 0: + return('''Oops! It seems that there are no quotes of the author of that + category. + \nYou may consider changing the category or the author ''') + quote_list.append(person) + + return(quote_list) + + +def get_quote(person, category=None): + """ + This function take a category and a person as a input and returns + a random quote which matches the input. + :param person: Name of the person e.g. Albert Einstein + :param category: Category of quote e.g. Motivational + :param return: A tuple (quote, author_of_the_quote) + """ + quotes = get_quotes(person, category) + length = len(quotes) + if(length == 0): + # In case no quote of the author exist for that category. + return("No quotes found of that category") + else: + random_number = random.randint(0, length - 1) + list = [] + list.append(quotes[random_number]) + list.append(person) + + return(tuple(list)) + + +def get_quote_of_the_day(): + """ + This fuction returns quote of the day. + :param return: A tuple (quote, author_of_the_quote) + """ + URL = "https://www.brainyquote.com/quote_of_the_day" + + # Sending a HTTP request to the specified URL and saving the response + # from server in a response object called response. + response = requests.get(URL) + soup = BeautifulSoup(response.content, 'html5lib') + a_tags = soup.findAll('img', alt=True) + + # Getting all the a tags of the page. + quote_of_the_day_atag = str(a_tags[0]) + + # Grabbing the first a tag of the page + matches = re.findall(r'\"(.+?)\"', quote_of_the_day_atag) + + # A regular expression which gives a list of all + # text that is in between quotes. + quote_author_split_list = str(matches[0]).split('-') + + # Get a list of quote_of_the_day and the author + quote_of_the_day = matches[0].replace(quote_author_split_list[-1], '') + quote_of_the_day = quote_of_the_day.replace('-', '') + author_name = quote_author_split_list[-1] + + # Gives the author_name + author_name = author_name.replace(' ', '') + + # Removes any extra space + return(quote_of_the_day, author_name) + +print(get_quote('Albert Einstein')) From 9728bf8a838545a14dcfb05f12253799bd8f06b3 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Sun, 14 Jul 2019 12:41:01 +0530 Subject: [PATCH 7/7] Fixing pep8 issues. --- pyquotes/brainyquote/brainyquote.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pyquotes/brainyquote/brainyquote.py b/pyquotes/brainyquote/brainyquote.py index 0adad2c..b07ffb5 100644 --- a/pyquotes/brainyquote/brainyquote.py +++ b/pyquotes/brainyquote/brainyquote.py @@ -26,7 +26,7 @@ def get_author_link(person): return author_url_link -def get_quotes(person, category = None): +def get_quotes(person, category=None): """ This function returns all the quotes that matches the input. If category is not specified then it returns all the quotes @@ -42,12 +42,13 @@ def get_quotes(person, category = None): all_quotes_list = [] if category is None: - get_all_quotes = soup_author.find_all('a', attrs={'title':'view quote'}) + get_all_quotes = soup_author.find_all('a', + attrs={'title': 'view quote'}) for i in range(len(get_all_quotes)): all_quotes_list.append(get_all_quotes[i].text) all_quotes_list.append(person) return all_quotes_list - + categories = soup_author.find_all('div', class_='kw-box') check = False count = 0 @@ -79,7 +80,7 @@ def get_quotes(person, category = None): def get_quote(person, category=None): """ This function take a category and a person as a input and returns - a random quote which matches the input. + a random quote which matches the input. :param person: Name of the person e.g. Albert Einstein :param category: Category of quote e.g. Motivational :param return: A tuple (quote, author_of_the_quote) @@ -131,5 +132,3 @@ def get_quote_of_the_day(): # Removes any extra space return(quote_of_the_day, author_name) - -print(get_quote('Albert Einstein'))