Files
OpenBB/gamestonk_terminal/thought_of_the_day.py
Colin Delahunty 82747072c5 Terminal Wide Rich (#1161)
* My idea for how we handle Rich moving forward

* remove independent consoles

* FIxed pylint issues

* add a few vars

* Switched print to console

* More transitions

* Changed more prints

* Replaced all prints

* Fixing tabulate

* Finished replace tabulate

* Finished removing rich from Tabulate

* add Panel around menu

* add GST watermark under feature flag

* Fixed 46 tests

* Delete test_screener[False].yaml

* Delete test_screener[True].yaml

* Fixed the rest of the tests

* add help and source color vars and use rgb

* rich on stocks/options

* update rich on disc, dps, sia

* rich in gov, ins and scr menus

* ba and ca menus with rich

* Fixed import issue

* Fixed some tests

* removed termcolor

* Removed prettytable

* add rich to remaining stocks menus

* FIxed linting issue

* Added James' changes

* Updated dependencies

* Add rich to cryptocurrency menu

* refactor economy and forex

* refactor etf with rich

* refactor mfunds

* refactor rich rest

* not specify style so default color works well on any background

* Fixing mypy issues

* Updated tests

* More test fixes

* James' test fixes

* Updating tests : stocks/screener - fix cassettes using BR

* Updating tests : crypto

* Updating tests : disable DEBUG_MODE

* Updating tests : stocks/fa/yfinance

* minor fixes that escape

* Improve the rich table function (that replaces tabulate :D )

* Fixed bad code

* delete rogue file + dcf fix + NoConsole

* sia mypy

* fuck you linter

* fuck you linter pt 2

* skip hehe

* i hate the black linter

* ubuntu mypy attempt

* Update : rich_config + gtff

* Updating tests : conftest

* Updating tests : stocks

* Update : rich_config

* Updating : rich_config

* make panel configurable for Theodore :b

* colors update

* Merged

* Updating : rich_config + feature_flags

* Updating : rich_config

* Updating tests : stocks

* Updating : feature_flags

Co-authored-by: DidierRLopes <dro.lopes@campus.fct.unl.pt>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: james <jmaslek11@gmail.com>
Co-authored-by: jose-donato <zmcdonato@gmail.com>
2022-01-15 02:06:00 +00:00

140 lines
3.8 KiB
Python

""" Thought of The Day """
__docformat__ = "numpy"
import random
import re
from typing import Dict
import requests
from bs4 import BeautifulSoup
from gamestonk_terminal.helper_funcs import get_user_agent
from gamestonk_terminal.rich_config import console
class ThoughtOfTheDay:
"""ThoughtOfTheDay class"""
def __init__(self, urls: Dict[str, str] = None):
"""Constructor"""
self.metadata = {} # type: Dict
if urls is None:
self.urls = {
"Marcus_Aurelius": "https://www.goodreads.com/author/quotes/17212.Marcus_Aurelius",
"Epictetus": "https://www.goodreads.com/author/quotes/13852.Epictetus",
"Seneca": "https://www.goodreads.com/author/quotes/4918776.Seneca",
"Marcus_Tullius_Cicero": "https://www.goodreads.com/author/quotes/13755.Marcus_Tullius_Cicero",
"Aristotle": "https://www.goodreads.com/author/quotes/2192.Aristotle",
"Plato": "https://www.goodreads.com/author/quotes/879.Plato",
"Pythagoras": "https://www.goodreads.com/author/quotes/203707.Pythagoras",
}
return
self.urls = urls
def get_urls(self) -> dict:
"""Getter method for URLs"""
return self.urls
def get_metadata(self, author: str) -> dict:
"""Loads metadata for a given author
Parameters
----------
author : str
Author key - Marcus_Aurelius, Epictetus, Seneca, Marcus_Tullius_Cicero, Aristotle, Plato, Pythagoras
Returns
-------
dict
Metadata dictionary that includes number of quotes, number of pages and first 30 quotes
"""
quotes_page = BeautifulSoup(
requests.get(
self.urls[author],
headers={"User-Agent": get_user_agent()},
).text,
"lxml",
)
find_navigation = quotes_page.find("em", {"class": "current"}).find_parent(
"div"
)
page_count = []
for a_page_ref in find_navigation.find_all("a", href=True):
page_count.append(a_page_ref.text.strip("\n"))
ret = {}
ret["pages"] = page_count[-2]
find_count = quotes_page.find(string=re.compile("Showing 1-30 of"))
quote_count = re.search(r"Showing 1-30 of (?P<number>[\d,]+)", find_count)
if quote_count:
ret["quoutes"] = quote_count.group("number")
all_quotes = quotes_page.find_all("div", {"class": "quote"})
ret["quotes"] = []
for a_quote in all_quotes:
parsed_quote = {}
parsed_quote = a_quote.find("div", {"class": "quoteText"}).text
ret["quotes"].append(parsed_quote)
return ret
def quote_to_str(self, a_quote: str) -> str:
"""Format a quote parsed from Goodreads into a string
Parameters
----------
a_quote : str
A quote formatted by Goodreads
Returns
-------
str
A string version of the quote
"""
ready = []
prev = None
for a_line in a_quote:
if not prev:
ready.append(a_line)
prev = a_line
continue
if a_line != "\n":
ready.append(a_line)
prev = a_line
continue
if prev == " \n":
ready.pop()
prev = None
return "".join(map(str, ready))
def get_thought_of_the_day():
"""Pick a thought of the day"""
totd = ThoughtOfTheDay()
quotes = []
for an_author in totd.get_urls():
metadata = totd.get_metadata(an_author)
quotes = quotes + metadata["quotes"]
console.print("Thought of the day:")
console.print(totd.quote_to_str(quotes[random.randint(0, len(quotes) - 1)]))
console.print("")