-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainyquote.py
70 lines (60 loc) · 1.81 KB
/
brainyquote.py
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
#!/usr/bin/env python3
import feedparser
import random
#
# Sample structure of an 'entries' object:
#
# {'summary_detail':
# {'type': 'text/html',
# 'language': None,
# 'value': '"It is an ironic habit of human beings to run faster when we have lost our way."',
# 'base': 'https://www.brainyquote.com/link/quotebr.rss'
# },
# 'title_detail':
# {'type': 'text/plain',
# 'language': None,
# 'value': 'Rollo May',
# 'base': 'https://www.brainyquote.com/link/quotebr.rss'
# },
# 'title': 'Rollo May',
# 'summary': '"It is an ironic habit of human beings to run faster when we have lost our way."',
# 'links':
# [
# {'type': 'text/html',
# 'href': 'https://www.brainyquote.com/authors/rollo_may',
# 'rel': 'alternate'
# }
# ],
# 'link': 'https://www.brainyquote.com/authors/rollo_may'
# }
class BrainyQuote:
_FEED_URL = 'https://www.brainyquote.com/link/quotebr.rss'
def __init__( self ):
self.quotes = [ ]
return
def haveQuotes( self ):
return len( self.quotes ) > 0
def getQuote( self ):
self.fetchQuotes( )
if len( self.quotes ) == 0:
author, quote = "", ""
else:
i = random.randrange( len( self.quotes ) )
author, quote = self.quotes.pop( i )
return author, quote
def fetchQuotes( self ):
if len( self.quotes ) == 0:
data = feedparser.parse( self._FEED_URL )
for item in data[ 'entries' ]:
self.quotes.append( ( item[ 'title' ], item[ 'summary' ] ) )
return
def main( ):
bq = BrainyQuote( )
author, quote = bq.getQuote( )
print( author, ":", quote )
while bq.haveQuotes( ):
author, quote = bq.getQuote( )
print( author, ":", quote )
return
if __name__ == "__main__":
main( )