-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuoteHandle.py
More file actions
76 lines (48 loc) · 1.46 KB
/
QuoteHandle.py
File metadata and controls
76 lines (48 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
from bs4 import BeautifulSoup
import random
import string
class Quotes:
def __init__(self):
self.__source = "https://www.brainyquote.com/authors/"
self.__author = "Douglas Adams"
self.__quotes = []
self.__quote = ""
self.__Update()
@property
def Author(self):
return self.__author
@Author.setter
def Author(self, value):
for p in string.punctuation:
if p in value:
value.replace(p, "")
self.__author = value
#Reload
self.__Update()
@property
def Quotes(self):
return self.__quotes
@property
def Quote(self):
return self.__quote
def __Update(self):
self.LoadQuotes()
self.RandomQuote()
def LoadQuotes(self):
url = self.__source + self.__author.replace(" ", "_").lower()
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
quotes = soup.find_all("a", title="view quote")
buffer = []
for quote in quotes:
quote = quote.text
if quote != "":
buffer.append(quote)
self.__quotes = list(set(buffer))
return self.__quotes
def RandomQuote(self):
if self.__quotes:
self.__quote = random.choice(self.__quotes) + "--" + self.__author
return self.__quote
return []