Update json fortune script

This commit is contained in:
Kujiu 2018-10-19 18:49:48 +02:00
parent b3e3baded5
commit a2d5a96936
No known key found for this signature in database
GPG key ID: F6A8243BF7C8404D

View file

@ -15,7 +15,7 @@ import threading
import sys
import os.path
import random
import secrets
try:
import speechd
except ImportError:
@ -40,27 +40,46 @@ class JsonFortune:
with open(srcfile) as fin:
self.quotes = json.load(fin)
by_category = getattr(options, 'by_category', False)
if not by_category:
self.uncategorized_quotes = {}
for lang in self.quotes:
self.uncategorized_quotes.setdefault(lang, [])
for category in self.quotes[lang]:
for author in self.quotes[lang][category]:
self.uncategorized_quotes[lang].extend(
[
(quote, author)
for quote
in self.quotes[lang][category][author]
]
)
def get_quote(self):
"""
Get a quote, returns (quote, lang, author)
"""
by_category = getattr(options, 'by_category', False)
quote = ""
count = 0
while not quote and count < 50:
count += 1
lang = random.choice([lang for lang in self.quotes])
category = random.choice([cat for cat in self.quotes[lang]])
lang = secrets.choice([lang for lang in self.quotes])
if by_category:
category = secrets.choice([cat for cat in self.quotes[lang]])
if not self.quotes[lang][category]:
continue
author = random.choice(
author = secrets.choice(
[author for author in self.quotes[lang][category]])
if not self.quotes[lang][category][author]:
continue
quote = random.choice(self.quotes[lang][category][author])
quote = secrets.choice(self.quotes[lang][category][author])
else:
quote, author = secrets.choice(self.uncategorized_quotes[lang])
return (quote, lang, author)
def say_quote(self):
def say_quote(self, callback=None):
"""
Say a random quote
"""
@ -88,9 +107,9 @@ class JsonFortune:
in speech_client.list_synthesis_voices()
if voice[1] == lang]
speech_client.set_language(lang)
speech_client.set_synthesis_voice(random.choice(voices))
speech_client.set_synthesis_voice(secrets.choice(voices))
logger.info("Say: %s", quote)
speech_client.say(quote)
speech_client.speak(quote, callback)
speech_client.close()
def print_quote(self):
@ -112,7 +131,7 @@ class JsonFortune:
Run
"""
if self.speak:
self.say_quote()
self.say_quote(self.run if self.cycle else None)
else:
self.print_quote()
if self.cycle:
@ -123,6 +142,9 @@ if __name__ == '__main__':
parser.add_argument(
'-s', '--source', default='~/.fortunes.json',
help='JSON file with quotes')
parser.add_argument(
'-b', '--by_category', default=False, action='store_true',
help='Each category has the same weight')
parser.add_argument(
'-v', '--verbose', default=False, action='store_true',
help='Verbose')
@ -133,7 +155,7 @@ if __name__ == '__main__':
'-m', '--module', default='default',
help='Speech Dispatcher output module')
parser.add_argument(
'-r', '--rate', default=20,
'-r', '--rate', default=15,
help='Speech Dispatcher rate', type=int)
parser.add_argument(
'-c', '--cycle', default=False, action='store_true',
@ -150,7 +172,7 @@ if __name__ == '__main__':
speak = getattr(options, 'speak', False)
module = getattr(options, 'module', None)
rate = getattr(options, 'rate', 20)
rate = getattr(options, 'rate', 15)
verbose = getattr(options, 'verbose', False)
cycle = getattr(options, 'cycle', False)