This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygq.py
96 lines (80 loc) · 3.47 KB
/
pygq.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
"""
Copyright (C) 2018 Farooq Karimi Zadeh <farooghkarimizadeh at gmail dot com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
import requests
class PyGQ:
SURAH_LAST = 114
SURAH_FIRST = 1
def __init__(self, url="http://api.globalquran.com/ayah/", token="",
lg_codes={}, cache_size=0):
"""url and token are API url and token. lg_codes is a dict which maps
2 letter language codes to their fullname. like:
{"en": "en.sahih", "ar": "quran-simple"}
"""
self.url = url
self.token = token
self.lg_codes = lg_codes
self.cache_size = cache_size
self.cache = list()
def __update_cache(self):
if not self.cache_size: return
if len(self.cache) > self.cache_size:
while len(self.cache) > self.cache_size:
self.cache.pop()
def getAyah(self, surah, ayah, lang):
"""Returns a dict containing the verse itself, ayah and surah number
and id. example:
{
"surah": 1,
"ayah": 1,
"id": 1,
"verse": "In the name of Allah, the Entirely Merciful, the Especially Merciful."
}
"""
if len(lang) == 2:
if lang in self.lg_codes:
lang = self.lg_codes[lang]
else:
raise ValueError(lang+ " is not supported using 2letter codes")
if (not self.SURAH_FIRST <= surah <= self.SURAH_LAST):
raise ValueError("surah(chapter) must be between "
+ str(self.SURAH_FIRST) + " and " +
str(self.SURAH_LAST))
# TODO: check if it's in cache, if so use cache, if not get and add to
# cache
if self.cache_size:
for key, json_ in self.cache: # FIXME: better name than json_
if (surah,ayah,lang) == key:
self.cache.remove((key,json_))
self.cache.insert(0, (key,json_))
return json_
req_url = self.url + str(surah) + ":" + str(ayah) + "/" + lang
ayah_json = requests.get(req_url, params = {'key': self.token}).json()
while len(ayah_json) == 1:
ayah_json = ayah_json[next(iter(ayah_json))]
if ayah_json["surah"] != surah:
raise ValueError("Invalid ayah number for this surah")
self.cache.insert(0, ((surah, ayah, lang), ayah_json))
self.__update_cache()
return ayah_json
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage:", sys.argv[0], "surah:ayah [lang]")
sys.exit(0)
surah, ayah = sys.argv[1].split(":")
surah = int(surah)
ayah = int(ayah)
lang = "en.sahih" if len(sys.argv) < 3 else sys.argv[2]
Q = PyGQ()
print(Q.getAyah(surah, ayah, lang)["verse"])