Skip to content

Instantly share code, notes, and snippets.

@benelog
Last active Jan 19, 2017
Embed
What would you like to do?
SMS to TTS

활용요소

  • IFTTT
  • Dropbox
  • Google TTS API
  • mplayer (Linux mp3 player)
  • Python watchdog 라이브러리

사용방법

  1. IFTTT가입
  2. 레서피 복사하여 활성화
  3. IFTTT 앱을 휴대폰에 설치
  4. 스피커 장비에 Dropbox 설치
  5. mplayer 설치 sudo apt-get install mplayer
  6. Listener daemon 설치
  7. Listener 실행
    • Dropbox로 동기화되는 폴더를 파라미터로 지정 python tts_listener.py ~/Dropbox/IFTTT/SMS
import sys
import os
import requests
def tts(msg):
url = 'http://translate.google.com/translate_tts?ie=UTF-8&tl=ko&q='+ msg
file = 'test.mp3'
headers = {
'User-Agent': 'Mozilla'
}
mp3 = open(file,'wb')
mp3.write(requests.get(url,headers=headers).content)
mp3.close()
os.system("mplayer test.mp3")
msg = sys.stdin.read()
print msg
tts(msg)
import time
import sys
import os
import requests
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.txt"]
def on_created(self, event):
msg_file = open(event.src_path, 'r')
msg = msg_file.read()
msg_file.close()
print(msg)
self.tts(msg)
def tts(self, msg):
url = 'http://translate.google.com/translate_tts?ie=UTF-8&tl=ko&q='+ msg
file = 'test.mp3'
headers = {
'User-Agent': 'Mozilla'
}
mp3 = open(file,'wb')
mp3.write(requests.get(url,headers=headers).content)
mp3.close()
os.system("mplayer test.mp3")
if __name__ == '__main__':
args = sys.argv[1:]
observer = Observer()
observer.schedule(MyHandler(), path=args[0] if args else '.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment