Passed
Push — master ( 9bfda5...dc2491 )
by Anas
02:12
created

modules.tts.tts()   B

Complexity

Conditions 7

Size

Total Lines 28
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nop 3
dl 0
loc 28
rs 7.808
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from modules.logging import log_command
5
from telegram import ChatAction
6
from datetime import datetime
7
from gtts import gTTS
8
import os
9
10
11
def module_init(gd):
12
    global path
13
    path = gd.config["path"]
14
    commands = gd.config["commands"]
15
    for command in commands:
16
        gd.dp.add_handler(CommandHandler(command, tts, pass_args=True))
17
18
19
def tts(bot, update, args):
20
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
21
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
22
    reply = update.message.reply_to_message
23
    if reply is None:
24
        text = "".join(args)
25
    elif reply.text is not None:
26
        text = reply.text
27
    if len(text) == 0:
0 ignored issues
show
introduced by
The variable text does not seem to be defined for all execution paths.
Loading history...
28
        update.message.reply_text("Type in some text ^^")
29
        return
30
    update.message.chat.send_action(ChatAction.RECORD_AUDIO)
31
    lang="en"
32
    tts = gTTS(text, lang)
33
    tts.save(path + filename + ".mp3")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
34
    with open(path + filename + ".mp3", "rb") as f:
35
        linelist = list(f)
36
        linecount = len(linelist)
37
    if linecount == 1:
38
        update.message.chat.send_action(ChatAction.RECORD_AUDIO)
39
        lang = "ru"
40
        tts = gTTS(text, lang)
41
        tts.save(path + filename + ".mp3")
42
    with open(path + filename + ".mp3", "rb") as speech:
43
        update.message.reply_voice(speech, quote=False)
44
    print(current_time, ">", "/say", ">", update.message.from_user.username)
45
    os.remove(path+filename+".mp3")
46
    log_command(bot, update, current_time, "say")
47