aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.py
blob: 10b40773d3a7d3595d943ef72adadf928e4d6f97 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# AudioQuranBot -- Listen to the Holy Qur'an on Telegram
# Copyright (C) 1438 AH  Rahiel Kasim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import asyncio
import signal

import aioredis
import uvloop
from aiotg import Bot, Chat
from bismillahbot import Quran, make_index

from secret import token


bot = Bot(api_token=token)
quran_index = make_index()
redis = None
redis_namespace = "aqb:"


def get_file(filename: str):
    return redis.get(redis_namespace + filename)


def save_file(filename: str, file_id: str):
    return redis.set(redis_namespace + filename, file_id, expire=60*60*24*30)


@bot.command(r"/(?:start|help)")
def usage(chat: Chat, match):
    text = (
        "﷽‎\n"
        "Send the number of a surah and I'll send you its audio recitation by "
        "Shaykh Mahmoud Khalil al-Husary. For example send <b>36</b> and you'll "
        "receive the recitation of surah Yasin. Send /index to see a list of available "
        "surahs."
    )
    return chat.send_text(text, parse_mode="HTML")


@bot.command(r"/about")
def about(chat: Chat, match):
    text = (
        "The recitation is by "
        "<a href=\"https://en.wikipedia.org/wiki/Mahmoud_Khalil_Al-Hussary\">Shaykh Mahmoud Khalil al-Husary</a> "
        "from <a href=\"http://torrent.mp3quran.net/details.php?id=3f2404b9cc6dfb5ccf70580a149fd8b87de0d8f1\">mp3quran.net</a>. "
        "This bot is free software, the source code is available at: https://github.com/rahiel/AudioQuranBot."
    )
    return chat.send_text(text, parse_mode="HTML")


@bot.command(r"/index")
def index(chat: Chat, match):
    return chat.send_text(quran_index, parse_mode="HTML")


@bot.command(r"/?(\d+)")
async def audio(chat: Chat, match):
    surah = int(match.group(1))
    if not (1 <= surah <= 114):
        return chat.send_text("Surah does not exist!")
    await chat.send_chat_action("upload_audio")

    directory = "Mahmoud_Khalil_Al-Hussary_(Updated2)(MP3_Quran)/"
    multiple = {                # surahs in multiple audio tracks
        2: 4,
        3: 3,
        4: 3,
        5: 2,
        6: 2,
        7: 2,
        9: 2,
        10: 2,
        11: 2,
        12: 2,
        16: 2
    }

    if surah in multiple:
        filenames = [directory + str(surah).zfill(3) + "_" + str(i) + ".mp3" for i in range(1, multiple[surah] + 1)]
        multi = True
    else:
        filenames = [directory + str(surah).zfill(3) + ".mp3"]
        multi = False
    performer = "Shaykh Mahmoud Khalil al-Husary"
    title = "Quran {} {}".format(surah, Quran.get_surah_name(surah))

    for (i, filename) in enumerate(filenames):
        if multi:
            title = "Quran {} {} (part {}/{})".format(surah, Quran.get_surah_name(surah), i+1, len(filenames))
        file_id = await get_file(filename)
        if file_id:
            try:
                response = await chat.send_audio(file_id, performer=performer, title=title)
            except Exception as e:
                if "file_id" in str(e) or "file identifier" in str(e):
                    with open(filename, "rb") as f:
                        response = await chat.send_audio(f, performer=performer, title=title)
                else:
                    raise(e)
        else:
            with open(filename, "rb") as f:
                response = await chat.send_audio(f, performer=performer, title=title)

        file_id = response["result"]["audio"]["file_id"]
        await save_file(filename, file_id)


async def main():
    global redis
    redis = await aioredis.create_redis(("localhost", 6379), encoding="utf-8")
    await bot.loop()


if __name__ == "__main__":
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    loop = asyncio.get_event_loop()

    # handle Supervisor stop signal
    loop.add_signal_handler(signal.SIGTERM, lambda: bot.stop())

    try:
        loop.run_until_complete(main())
    except KeyboardInterrupt:
        bot.stop()