Skip to content

Instantly share code, notes, and snippets.

@evildmp
Last active July 27, 2016 15:10
Show Gist options
  • Save evildmp/7f4702efac3a164629dd9c31bfff8ee4 to your computer and use it in GitHub Desktop.
Save evildmp/7f4702efac3a164629dd9c31bfff8ee4 to your computer and use it in GitHub Desktop.
Hamlet for two voices, in Python
# coding=UTF8
# Hamlet, for two voices
# after Ulises Carrión
#
# https://en.wikipedia.org/wiki/Ulises_Carrión
#
# Ulises Carrión's recording of Hamlet, for two voices:
# https://www.dropbox.com/s/vyi3psnt0da2hu0/Hamlet%20for%20two%20voices.mp3?dl=0
# Requires:
# * the OS X "say" command - there's probably an equivalent on your system
# * BeautifulSoup - pip install beautifulsoup4
# * Requests - pip install requests
import subprocess
import re
import requests
from bs4 import BeautifulSoup
text_source = "http://shakespeare.mit.edu/hamlet/full.html"
print("Getting %s" % text_source)
response = requests.get(text_source)
soup = BeautifulSoup(response.content, 'html.parser')
try:
for ind, tag in enumerate(soup.find_all('a', {'name': re.compile('speech.*')})):
# get first item with stripped tags
name = next(tag.stripped_strings)
# You will need to download the appropriate voices. The British English
# Daniel and Kate do quite well.
#
# You might think that Mexican Spanish voices Juan and Angelica would be
# better still, but they're not as good.
if ind % 2 == 0:
print(name)
subprocess.call(['say', '-v', 'daniel', '-r', '200', name])
else:
print(name)
subprocess.call(['say', '-v', 'kate', '-r', '200', name])
except KeyboardInterrupt:
exit('aborted')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment