Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@orithena
Last active August 29, 2015 14:17
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 orithena/75f7fd1e0671c10f0d5b to your computer and use it in GitHub Desktop.
Save orithena/75f7fd1e0671c10f0d5b to your computer and use it in GitHub Desktop.
Tweet-to-Speech script, reading out your timeline. Moved to https://github.com/orithena/tweet-to-speech
#!/usr/bin/python
import alsaaudio
from subprocess import call
import twython
import guess_language
from twython import TwythonStreamer
import config
import thread
import uuid
import os
""" config is just a file named config.py next to this script, containing:
TWITTER_CONSUMER_KEY = 'your apps consumer key'
TWITTER_CONSUMER_SECRET = 'your apps consumer secret'
TWITTER_ACCESS_TOKEN = 'your apps oauth access token'
TWITTER_TOKEN_SECRET = 'your apps oauth access token secret'
PLAYVOLUME = 75
TTSDEFAULTLANG = 'de-DE'
MIXERID = 'PCM'
You can generate the keys and register your app on apps.twitter.com
"""
speechlock = thread.allocate_lock()
def say(text, lang=config.TTSDEFAULTLANG):
fname = "/tmp/picotts-%s.wav" % uuid.uuid1()
if call(["pico2wave", "-w", fname, "-l", lang, text]) == 0:
speechlock.acquire()
mixer = alsaaudio.Mixer(config.MIXERID)
oldvolume = mixer.getvolume()[0]
mixer.setvolume(config.PLAYVOLUME)
call(["aplay", fname])
mixer.setvolume(oldvolume)
mixer.close()
speechlock.release()
os.unlink(fname)
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
text = ("%s: %s" % (data['user']['screen_name'], data['text']))
l = guess_language.guessLanguage(data['text'])
lang = 'de-DE'
try:
lang = {'en': 'en-GB', 'de': 'de-DE', 'es': 'es-ES', 'fr': 'fr-FR'}[l]
except KeyError:
pass
print "%s (%s)" % (text, lang)
thread.start_new_thread(say, (text, lang))
else:
print("unknown notification received:")
print(data)
def on_error(self, status_code, data):
print status_code
twitter = twython.Twython(
app_key=config.TWITTER_CONSUMER_KEY,
app_secret=config.TWITTER_CONSUMER_SECRET,
oauth_token=config.TWITTER_ACCESS_TOKEN,
oauth_token_secret=config.TWITTER_TOKEN_SECRET)
creds = twitter.verify_credentials()
userid = creds['id_str']
stream = MyStreamer(
config.TWITTER_CONSUMER_KEY,
config.TWITTER_CONSUMER_SECRET,
config.TWITTER_ACCESS_TOKEN,
config.TWITTER_TOKEN_SECRET)
stream.twitter = twitter
stream.creds = creds
stream.user()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment