Skip to content

Instantly share code, notes, and snippets.

@kimfucious
Last active July 11, 2018 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimfucious/bd421d1a9fee6fca7b60943c35083443 to your computer and use it in GitHub Desktop.
Save kimfucious/bd421d1a9fee6fca7b60943c35083443 to your computer and use it in GitHub Desktop.
Who said it? : A quote guessing game written in Python that scrapes a quote database to a local csv file
from bs4 import BeautifulSoup
from csv import reader, writer
from halo import Halo
from os import name, remove, system
from pyfiglet import figlet_format
from random import choice
from termcolor import colored
from time import sleep
import requests
url = "http://quotes.toscrape.com"
guesses_allowed = 4
def clear_screen():
if name == "posix":
system("clear")
else:
system("cls")
def end_game():
print_ascii_art("GOODBYE", "blue")
sleep(2)
clear_screen()
quit()
def get_bio(author):
spinner = Halo(text=colored("Scraping hints...", color="white"),
spinner="dots", color="yellow")
spinner.start()
res = requests.get(url+author)
soup = BeautifulSoup(res.text, "html.parser")
birthdate = soup.find(class_="author-born-date").get_text()
location = soup.find(class_="author-born-location").get_text()
spinner.succeed()
return "This person was born on {} {}".format(birthdate, location)
def yes_or_no(answer):
if answer == "y":
play_game()
elif answer == "n":
end_game()
else:
answer = input("Please enter y or n: ")
yes_or_no(answer)
def play_game():
clear_screen()
guesses_remaining = guesses_allowed
with open("scraped_quotes.csv") as file:
quotes = list(reader(file))
current_quote = choice(quotes)
author = current_quote[0]
text = current_quote[1]
author_bio_url = current_quote[2]
print(colored("Here's a quote:", color="yellow"))
print(colored("\n{}".format(text), color="magenta"))
answer = input(colored("\nWho said it? ", color="yellow"))
for guess in range(guesses_allowed):
if answer == author:
respond_to_right_answer()
else:
guesses_remaining -= 1
answer = respond_to_wrong_answer(
author, guesses_remaining, author_bio_url)
def print_ascii_art(message, color):
ascii_art = figlet_format(message)
colored_ascii = colored(ascii_art, color=color)
print(colored_ascii)
def respond_to_right_answer():
print_ascii_art("WINNER!", "green")
play_again = input(colored(
"OMG, you're a flippin' genius! Wanna play again? (y/n): ", color="yellow"))
yes_or_no(play_again)
def respond_to_wrong_answer(author, guesses_remaining, author_bio_url):
if guesses_remaining == 3:
print(colored("That is incorrect.", color="red"))
hint = get_bio(author_bio_url)
print(colored("Here's a hint:", color="yellow"))
print(colored(hint, color="cyan"))
answer = input(colored(
"You have {} guesses left. Give it another shot: ".format(guesses_remaining), color="yellow"))
return answer
elif guesses_remaining == 2:
first_initial = author[0]
print(colored("That is incorrect.", color="red"))
print(colored("Here's another hint:", color="yellow"))
print(colored("This person's first name begins with {}".format(
first_initial), color="cyan"))
answer = input(colored(
"You have {} guesses left. There is no try, only do!: ".format(guesses_remaining), color="yellow"))
return answer
elif guesses_remaining == 1:
last_initial = author.split(" ")[-1][0]
print(colored("That is incorrect.", color="red"))
print(colored("Here's a final hint:", color="yellow"))
print(colored("This person's last name begins with {}".format(
last_initial), color="cyan"))
answer = input(colored(
"You have {} guess left. Last chance (no pressure at all!): ".format(guesses_remaining), color="red"))
return answer
else:
print(colored(
"You are all out of guesses. The correct answer was {}".format(author), color="cyan"))
play_again = input(colored(
"Wanna play again? (y/n): ", color="yellow"))
yes_or_no(play_again)
spinner = Halo(text=colored("Scraping quotes...", color="white"),
spinner="dots", color="yellow")
spinner.start()
def scrape_site(url, page):
res = requests.get(url+page)
soup = BeautifulSoup(res.text, "html.parser")
quotes = soup.find_all(class_="quote")
is_next_page = soup.find("li", class_="next")
with open("scraped_quotes.csv", "w") as file:
csv_writer = writer(file)
for quote in quotes:
text = quote.find(class_="text").get_text()
author = quote.find(class_="author").get_text()
author_url = quote.find("a")["href"]
csv_writer.writerow([author, text, author_url])
if is_next_page:
next_page_url = soup.find("li", class_="next").find("a")["href"]
scrape_site(url, next_page_url)
else:
spinner.succeed(
colored("Scraping Complete. Let's do this!", color="white"))
sleep(2)
print("\n")
clear_screen()
print_ascii_art("Who said it ?".upper(), "yellow")
remove("scraped_quotes.csv")
scrape_site(url, "")
play_game()
@kimfucious
Copy link
Author

Changing "a" to "w" prevents error when starting off fresh with no existing scraped_quotes.csv file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment