aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2017-05-21 14:24:27 +0200
committerRahiel Kasim <rahielkasim@gmail.com>2017-05-21 14:24:27 +0200
commit90ef14b0932a39bdef2e81152181ddca122bedbc (patch)
tree03804b165e1ee0d9809370f3955bcd0819e83650
parent1441c92e77cc87dbe0d2980986f3049fe7eb8912 (diff)
camelCase -> snake_case, Noble Quran -> Holy Quran, ' -> "
-rw-r--r--README.md7
-rw-r--r--bismillah.py63
-rw-r--r--[-rwxr-xr-x]quran.py39
3 files changed, 54 insertions, 55 deletions
diff --git a/README.md b/README.md
index 6c2b12a..04da68a 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ
-BismillahBot is a bot on Telegram to explore the Qur'an.
+BismillahBot is a bot on Telegram to explore the Holy Qur'an.
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
**Table of Contents**
@@ -21,8 +21,9 @@ Use the bot by messaging [Bismillahbot][] on [Telegram][]. For every verse the
bot has an English translation from [Imam Ahmad Raza][], audio recitation
by [Shaykh Mahmoud Khalil al-Husary][], and exegesis (tafsir)
from [Tafsir al-Jalalayn][]. The translation and tafsir are available anywhere
-on Telegram via [inline mode][], just start a text with `@BismillahBot`. A
-conversation looks like: ![example]
+on Telegram via [inline mode][], just start a text with `@BismillahBot` (for
+example, type `@BismillahBot 1:1` in any chat). A conversation looks like:
+![example]
[BismillahBot]: https://telegram.me/BismillahBot
[Telegram]: https://telegram.org/
diff --git a/bismillah.py b/bismillah.py
index 23a81e1..4eddd14 100644
--- a/bismillah.py
+++ b/bismillah.py
@@ -1,7 +1,6 @@
-# -*- coding: utf-8 -*-
###############################################################################
-# BismillahBot -- Explore the Noble Qur'an on Telegram #
-# Copyright (C) 1436-1437 AH Rahiel Kasim #
+# BismillahBot -- Explore the Holy Qur'an on Telegram #
+# Copyright (C) 1436-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 #
@@ -17,13 +16,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
###############################################################################
import re
-from time import sleep, time
import sys
+from time import sleep, time
import telegram
+from telegram import InlineQueryResultArticle, InputTextMessageContent
from telegram.constants import MAX_MESSAGE_LENGTH
from telegram.error import NetworkError, Unauthorized, TelegramError
-from telegram import InlineQueryResultArticle, InputTextMessageContent
from redis import StrictRedis
import ujson as json
@@ -64,7 +63,7 @@ def get_audio_filename(s, a):
def get_image_filename(s, a):
- return "quran_images/" + str(s) + '_' + str(a) + ".png"
+ return "quran_images/" + str(s) + "_" + str(a) + ".png"
def send_file(bot, filename, quran_type, **kwargs):
@@ -74,9 +73,9 @@ def send_file(bot, filename, quran_type, **kwargs):
def upload(f):
if quran_type == "arabic":
- v = bot.sendPhoto(photo=f, **kwargs)["photo"][-1]["file_id"]
+ v = bot.send_photo(photo=f, **kwargs)["photo"][-1]["file_id"]
elif quran_type == "audio":
- v = bot.sendAudio(audio=f, **kwargs)["audio"]["file_id"]
+ v = bot.send_audio(audio=f, **kwargs)["audio"]["file_id"]
save_file(filename, v)
return v
@@ -105,7 +104,7 @@ def get_default_query_results(quran):
]
for s, a in ayat:
ayah = "%d:%d" % (s, a)
- english = quran.getAyah(s, a)
+ english = quran.get_ayah(s, a)
results.append(InlineQueryResultArticle(
ayah + "def", title=ayah,
description=english[:120],
@@ -119,7 +118,7 @@ def main():
bot = telegram.Bot(token=TOKEN)
try:
- update_id = bot.getUpdates()[0].update_id
+ update_id = bot.get_updates()[0].update_id
except IndexError:
update_id = None
@@ -155,19 +154,19 @@ def serve(bot, data):
def send_quran(s, a, quran_type, chat_id, reply_markup=None):
if quran_type in ("english", "tafsir"):
- text = data[quran_type].getAyah(s, a)
- bot.sendMessage(chat_id=chat_id, text=text[:MAX_MESSAGE_LENGTH],
- reply_markup=reply_markup)
+ text = data[quran_type].get_ayah(s, a)
+ bot.send_message(chat_id=chat_id, text=text[:MAX_MESSAGE_LENGTH],
+ reply_markup=reply_markup)
elif quran_type == "arabic":
- bot.sendChatAction(chat_id=chat_id,
- action=telegram.ChatAction.UPLOAD_PHOTO)
+ bot.send_chat_action(chat_id=chat_id,
+ action=telegram.ChatAction.UPLOAD_PHOTO)
image = get_image_filename(s, a)
send_file(bot, image, quran_type, chat_id=chat_id,
caption="Quran %d:%d" % (s, a),
reply_markup=reply_markup)
elif quran_type == "audio":
- bot.sendChatAction(chat_id=chat_id,
- action=telegram.ChatAction.UPLOAD_AUDIO)
+ bot.send_chat_action(chat_id=chat_id,
+ action=telegram.ChatAction.UPLOAD_AUDIO)
audio = get_audio_filename(s, a)
send_file(bot, audio, quran_type, chat_id=chat_id,
performer="Shaykh Mahmoud Khalil al-Husary",
@@ -175,7 +174,7 @@ def serve(bot, data):
reply_markup=reply_markup)
save_user(chat_id, [s, a, quran_type])
- for update in bot.getUpdates(offset=update_id, timeout=10):
+ for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
if update.inline_query:
@@ -186,8 +185,8 @@ def serve(bot, data):
s, a = parse_ayah(query)
if s is not None and Quran.exists(s, a):
ayah = "%d:%d" % (s, a)
- english = data["english"].getAyah(s, a)
- tafsir = data["tafsir"].getAyah(s, a)
+ english = data["english"].get_ayah(s, a)
+ tafsir = data["tafsir"].get_ayah(s, a)
results.append(InlineQueryResultArticle(
ayah + "english", title="English",
description=english[:120],
@@ -200,7 +199,7 @@ def serve(bot, data):
)
else:
results = data["default_query_results"]
- bot.answerInlineQuery(inline_query_id=query_id, cache_time=cache_time, results=results)
+ bot.answer_inline_query(inline_query_id=query_id, cache_time=cache_time, results=results)
continue
if not update.message: # weird Telegram update with only an update_id
@@ -214,19 +213,19 @@ def serve(bot, data):
else:
s, a, quran_type = 1, 1, "english"
- print("%d:%.3f:%s" % (chat_id, time(), message.replace('\n', ' ')))
+ print("%d:%.3f:%s" % (chat_id, time(), message.replace("\n", " ")))
if chat_id < 0:
continue # bot should not be in a group
# "special:quran_type"
- special_state = quran_type.split(':')
+ special_state = quran_type.split(":")
- if message.startswith('/'):
+ if message.startswith("/"):
command = message[1:]
if command in ("start", "help"):
text = ("Send me the numbers of a surah and ayah, for example:"
- " <b>2:255</b>. Then I respond with that ayah from the Noble "
+ " <b>2:255</b>. Then I respond with that ayah from the Holy "
"Quran. Type /index to see all Surahs or try /random. "
"I'm available in any chat on Telegram, just type: <b>@BismillahBot</b>")
elif command == "about":
@@ -251,15 +250,15 @@ def serve(bot, data):
else:
text = None # "Invalid command"
if text:
- bot.sendMessage(chat_id=chat_id, text=text, parse_mode="HTML")
+ bot.send_message(chat_id=chat_id, text=text, parse_mode="HTML")
continue
if len(special_state) > 1:
if special_state[0] == "feedback":
- with open("feedback.txt", 'a') as f:
+ with open("feedback.txt", "a") as f:
f.write("%d: %s\n" % (chat_id, message))
text = "Feedback saved 😊"
- bot.sendMessage(chat_id=chat_id, text=text)
+ bot.send_message(chat_id=chat_id, text=text)
save_user(chat_id, (s, a, special_state[1]))
continue
@@ -268,11 +267,11 @@ def serve(bot, data):
continue
elif message in ("next", "previous", "random", "/random"):
if message == "next":
- s, a = Quran.getNextAyah(s, a)
+ s, a = Quran.get_next_ayah(s, a)
elif message == "previous":
- s, a = Quran.getPreviousAyah(s, a)
+ s, a = Quran.get_previous_ayah(s, a)
elif message in ("random", "/random"):
- s, a = Quran.getRandomAyah()
+ s, a = Quran.get_random_ayah()
send_quran(s, a, quran_type, chat_id)
continue
@@ -281,7 +280,7 @@ def serve(bot, data):
if Quran.exists(s, a):
send_quran(s, a, quran_type, chat_id, reply_markup=data["interface"])
else:
- bot.sendMessage(chat_id=chat_id, text="Ayah does not exist!")
+ bot.send_message(chat_id=chat_id, text="Ayah does not exist!")
sys.stdout.flush()
diff --git a/quran.py b/quran.py
index ab1d252..71a37ce 100755..100644
--- a/quran.py
+++ b/quran.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import re
from random import randint
import xml.etree.ElementTree as ET
@@ -15,13 +14,13 @@ def parse_quran_trans():
def process_verse(verse):
"""Add verse and replace for Arabic ligatures (salawat)"""
return (verse.strip()
- .replace("– peace and blessings be upon him", 'ﷺ‎'))
+ .replace("– peace and blessings be upon him", "ﷺ‎"))
- with open("en.ahmedraza", 'r') as f:
+ with open("en.ahmedraza", "r") as f:
for line in f.readlines():
- if line == '\n': continue
+ if line == "\n": continue
if line[:3] == "#==": break
- verse = line.split('|')
+ verse = line.split("|")
assert len(verse) == 3
if int(verse[0]) == s:
surah.append(process_verse(verse[2]))
@@ -45,15 +44,15 @@ def parse_quran_tafsir():
verse = []
def add_verse(verse, surah):
- surah.append(' '.join(verse))
+ surah.append(" ".join(verse))
def add_line(line, verse):
"""Add line and replace for Arabic ligatures (salawat)"""
- verse.append(line.strip().replace("(s)", 'ﷺ‎'))
+ verse.append(line.strip().replace("(s)", "ﷺ‎"))
- with open("Al_Jalalain_Eng.txt", 'r') as f:
+ with open("Al_Jalalain_Eng.txt", "r") as f:
for line in f.readlines():
- if line == '\n': continue
+ if line == "\n": continue
if re.match("\d+\w*", line): continue
elif line.startswith("[%d:%d]" % (s, v)):
in_verse = True
@@ -98,26 +97,26 @@ class Quran(object):
elif data == "tafsir":
self.text = parse_quran_tafsir()
- def getSurah(self, surah):
+ def get_surah(self, surah):
"""Get surah by number."""
return self.text[surah - 1]
- def getAyah(self, surah, ayah):
+ def get_ayah(self, surah, ayah):
"""Get verse by surah and ayah numbers."""
return self.text[surah - 1][ayah - 1] + " (%d:%d)" % (surah, ayah)
- def getAyahs(self, surah, a, b):
+ def get_ayahs(self, surah, a, b):
"""Get range of Ayahs as tuple """
- return ' '.join(self.text[surah - 1][a - 1:b]) + " (%d:%d-%d)" % (surah, a, b)
+ return " ".join(self.text[surah - 1][a - 1:b]) + " (%d:%d-%d)" % (surah, a, b)
@staticmethod
- def getRandomAyah():
+ def get_random_ayah():
surah = randint(1, 114)
ayah = randint(1, Quran.surah_lengths[surah])
return surah, ayah
@staticmethod
- def getNextAyah(s, a):
+ def get_next_ayah(s, a):
length = Quran.surah_lengths[s]
if a == length:
s = s + 1 if s < 114 else 1
@@ -127,7 +126,7 @@ class Quran(object):
return s, a
@staticmethod
- def getPreviousAyah(s, a):
+ def get_previous_ayah(s, a):
if a == 1:
s = s - 1 if s > 1 else 114
a = Quran.surah_lengths[s]
@@ -146,9 +145,9 @@ def make_index():
chapters = [s.attrib["tname"] for s in suras]
# padding...
for i in range(9):
- chapters[i] = ' ' + chapters[i] + ' ' * (14 - len(chapters[i]))
+ chapters[i] = " " + chapters[i] + " " * (14 - len(chapters[i]))
for i in range(9, 58):
- chapters[i] += ' ' * (14 - len(chapters[i]))
+ chapters[i] += " " * (14 - len(chapters[i]))
index = []
left = range(1, 58)
@@ -156,12 +155,12 @@ def make_index():
for i, j in zip(left, right):
index.append("/{} <code>{}</code>/{} {}"
.format(i, chapters[i - 1], j, chapters[j - 1]))
- return '\n'.join(index)
+ return "\n".join(index)
def save_json(quran):
"""Save Quran to a json file."""
import json
- with open("quran.json", 'w') as f:
+ with open("quran.json", "w") as f:
json.dump(quran, f, ensure_ascii=False)