-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.py
71 lines (58 loc) · 1.88 KB
/
cards.py
1
# Wonderfully formatted data taken from:# https://raw.githubusercontent.com/samurailink3/hangouts-against-humanity/master/source/data/cards.js# Local version ("cards.json") is that exact file with the first 14 characters chopped off to make it valid JSON.# Also changed \' to just ' and a special \" to just \".import jsonimport randomfrom itertools import chainimport systry: from itertools import izip_longest as zip_longestexcept: from itertools import zip_longestquestions = []responses = []def read_cards(filename): with open(filename) as f: cards = json.load(f) for card in cards: card_type = card['cardType'] text = card['text'] if card_type == 'A': responses.append(text) else: blanks = card['numAnswers'] questions.append((text, blanks))def get_message(): question, num_blanks = random.choice(questions) blanks = random.sample(responses, num_blanks) parts = question.split('_') # Put semicolons in strange places to make a dramatic pause for blanks. parts = [part + u':' for part in parts] message = chain.from_iterable(zip_longest(parts, blanks, fillvalue='')) # Do some unwrapping and cleanup. message = [part for part in message if part and part != '.'] return messagedef osx_speak(message): subprocess.call(['say', message])def speak(message): tts = gTTS(text=message, lang='en') tts.save("temp.mp3") mp3 = mp3play.load("temp.mp3") mp3.play() # Block until speaking has finished. while mp3.isplaying(): passif sys.platform == 'darwin': import subprocess speak = osx_speakelse: from gtts import gTTS import mp3playdef main(): read_cards("cards.json") message = ' '.join(get_message()) print(message) speak(message)if __name__ == '__main__': main()