summaryrefslogtreecommitdiff
path: root/callers/read_numbers_aloud.py
blob: 7578ae200d10ef62e4c488d238497b243bbf5b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3

import asyncio
import sys

from espeak import espeak


class Speaker(object):
    """Speak aloud something instead of calling numbers."""

    def __init__(self, voice='europe/it'):
        espeak.set_voice(voice)

    def _output(self, text):
        print(text)
        espeak.synth(text)

    def speak(self, number):
        try:
            getattr(self, 'call_'+number, None)()
        except TypeError:
            self._output("Sto chiamando il numero %s." % number)

    def call_1234(self):
        self._output(u"Questa è la combinazione della mia valigia.")

    def call_1234567890(self):
        self._output(u"Stiamo testando tutti i numeri, vero?")

    def call_666(self):
        self._output(u"Risponde la segreteria telefonica di Satana.")

    def call_42(self):
        self._output(u"E' la risposta alla domanda fondamentale sulla vita, l'universo e tuttoquanto. Giusto?")


class Reader(object):
    """Read digits from stdin and turn them into phone numbers."""

    def __init__(self):
        self.number = ""
        self.handle = None
        self.speaker = Speaker()

    def read_number(self):
        text = sys.stdin.readline()
        if len(text) == 2 and text[0] in "0123456789":
            self.number += text[0]
            if self.handle:
                self.handle.cancel()
            self.handle = self.loop.call_later(3, self.call_number)
        # TODO: do we want a warning in case we receive something wrong?

    def call_number(self):
        self.speaker.speak(self.number)
        self.number = ""

    def run(self):
        self.loop = asyncio.get_event_loop()
        self.loop.add_reader(sys.stdin, self.read_number)
        try:
            self.loop.run_forever()
        finally:
            self.loop.close()


def main():
    reader = Reader()
    reader.run()

if __name__ == '__main__':
    main()