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