-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.py
144 lines (116 loc) · 5.38 KB
/
plugin.py
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
138
139
140
141
142
143
144
# Copyright 2016 Safa AlFulaij <safa1996alfulaij@gmail.com>
#
# This file is part of QuranFinder.
#
# QuranFinder is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# QuranFinder 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with QuranFinder. If not, see <http://www.gnu.org/licenses/>.
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('QuranFinder')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x: x
API_URL = "http://api.globalquran.com/ayah/" #verse:ayah/quranID
# Set languages and translations the bot will accept
quranID = {"ar" : "quran-simple", "en" : "en.sahih", "tr" : "tr.yazir", "fa" : "fa.fooladvand"}
TOKEN = "the token" # Token obtained from http://docs.globalquran.com
import requests
class qdata():
def __init__(self, chapter, ayah, lang):
if(lang == None):
lang = "en"
if(len(lang) == 2):
if(lang not in quranID):
raise ValueError("Only " + " ,".join(quranID) + " languages are supported using two letters code. Maybe you would like to use a translation/tafsir code instead, Check: https://git.io/vwMz4 for a list of avalible sources.")
lang = quranID[lang]
if (chapter > 114 or chapter < 1):
raise ValueError("Invalid Surah number.")
json = self.requestData(chapter, ayah, lang)
self.parseResponse(json)
if (int(self.SurahNumber) != chapter): # If the ayah number is bigger
# than the ayahs in the surah, the API jumps to another surah.
raise ValueError("Invalid Ayah number.")
def requestData(self, chapter, ayah, lang):
url = API_URL + str(chapter) + ":" + str(ayah) + "/" + lang
request = requests.get(url, params = {'key' : TOKEN})
json = request.json()
# the ID differs for each verse. So there is no static key to call in the main json.
for quran in json:
json = json[quran]
for quranVer in json: # the QuranID in the json. Here we used quranVar to avoid conflict with the quranID above.
json = json[quranVer]
for ID in json:
json = json[ID]
return json
def parseResponse(self, json):
self.SurahNumber = json["surah"]
self.ayahNumber = json["ayah"]
self.ayahText = json["verse"]
class utilities():
def arg2list(ayat): # TODO: better name for this function
MAX_DIFF = 5
try:
return [int(ayat)]
except ValueError:
t = ayat.split('-')
start, end = int(t[0]), int(t[1])
if (end - start) not in range(1, MAX_DIFF + 1):
raise ValueError("Out of range(maximum is 5) or start ayah is" +
" bigger than end ayah.") #TODO: a better message please
else:
return list(range(start, end + 1))
class QuranFinder(callbacks.Plugin):
"""This plugin gets verse and ayah number and sends you the ayah using a web API."""
def __init__(self, irc):
self.__parent = super(QuranFinder, self)
self.__parent.__init__(irc)
def quran(self, irc, msg, args, surah, ayat, lang):
"""<surah> <ayah/ayat> <lang>
returns ayah number <ayah> of surah number <surah> in <lang> language or translation or tafsir. for more information visit: https://git.io/vwMz9
"""
try:
list_of_ayat = utilities.arg2list(ayat)
except ValueError as e:
irc.error(str(e))
return
try:
ayat_list = []
for ayah in list_of_ayat:
data = qdata(surah, ayah, lang)
ayat_list.extend([str(data.SurahNumber) + ":" + str(data.ayahNumber)
+ ", " + str(data.ayahText)])
except ValueError as e:
irc.error(str(e))
return
except (KeyError, TypeError) as e: #TypeError incase requesting a audio version. The json would contain a list so qdata would raise a TypeError
irc.error("Wrong translation code or broken API.")
return
for ayah in ayat_list:
if self.registryValue('splitMessages'):
ircMsgBytes = ayah.encode('utf-8')
while len(ircMsgBytes) > 350:
splitPoint = ircMsgBytes[0:351].rfind(' '.encode('utf-8'))
irc.reply(ircMsgBytes[0:splitPoint].decode('utf-8').strip())
ircMsgBytes = ircMsgBytes[splitPoint:]
irc.reply(ircMsgBytes.decode('utf-8').strip())
else:
irc.reply(ayah)
quran = wrap(quran, ["int", "something", optional("something")])
Class = QuranFinder
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: