Skip to content

Instantly share code, notes, and snippets.

@wragge
Last active August 31, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wragge/a7c817fa8ef68d6e6764930a872e2a2f to your computer and use it in GitHub Desktop.
Save wragge/a7c817fa8ef68d6e6764930a872e2a2f to your computer and use it in GitHub Desktop.
Python script for MacOS that speaks interjections from Historic Hansard. The interjections, voice, speaking rate, volume, and delay are all set randomly.
import subprocess
from pymongo import MongoClient
import time
import random
import argparse
from credentials import MONGO_URL
HOUSES = {
'hofreps': 'House of Representatives',
'senate': 'Senate'
}
def get_interjections():
dbclient = MongoClient(MONGO_URL)
db = dbclient.get_default_database()
match = {'length': {'$lte': args.max_length}}
if args.query:
match['$text'] = {'$search': args.query}
interjections = list(db.interjections.aggregate(
[
{'$match': match},
{'$sample': {'size': int(args.number)}},
]))
else:
interjections = list(db.interjections.aggregate(
[
{'$sample': {'size': int(args.number)}},
{'$match': match}
]))
return interjections
def get_voices():
# This is MacOS specific stuff.
voices = []
# Get details of all the currently installed system voices
system_voices = subprocess.check_output(['say', '-v', '?'])
for voice in system_voices.split('\n'):
parts = voice.split(None, 2)
try:
# Add all English speaking voices to the list of voices
if parts[1][:2] == 'en':
voices.append(parts[0])
except IndexError:
pass
return voices
def speak():
# Again very MacOS specific
interjections = get_interjections()
for interjection in interjections:
text = interjection['text'].encode('utf-8')
# Choose a random voice
voice = random.choice(voices)
print '\n\n{} (as voiced by {}), {}, {}:\n'.format(interjection['speaker']['name'].upper(), voice, HOUSES[interjection['house']], interjection['date'].strftime('%-d %B %Y'))
print " '{}'".format(text)
# Set the volume somewhere between 50% & 100%
subprocess.call(['osascript', '-e', 'set volume output volume {}'.format(random.randrange(50, 100, 10))])
# Speak! Note randomly selected speaking rate between 150 and 250 words per minute
subprocess.call(['say', '-v', voice, text.replace('"', '').replace("'", ""), '-r', str(random.randrange(150, 250, 25))])
# Random delay between interjections from 0 to 5 seconds
time.sleep(random.randrange(0, 5, 1))
# To run: python speak.py -n [number of tweets] -q [search word or phrase] -l [maximum length of interjection]
# For example: python speak.py -n 20 -q "\"White Australia\"" -l 200
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--number', type=int, default=20, help='Number of interjections')
parser.add_argument('-q', '--query', help='Filter results by word or phrase')
parser.add_argument('-l', '--max_length', type=int, default=200, help='Maximum length of interjection')
args = parser.parse_args()
voices = get_voices()
speak()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment