Plaster
New
List
Login
text
apl
aspx
asterisk
brainfuck
c
c++hdr
c++src
cassandra
ceylon
clojure
clojurescript
cmake
cobol
coffeescript
common-lisp
crystal
csharp
css
cypher-query
cython
d
dart
diff
django
dockerfile
dylan
ebnf
ecl
ecmascript
edn
eiffel
ejs
elm
erb
erlang
ez80
factor
fcl
feature
forth
fortran
fragment
gfm
go
gql
groovy
gss
haml
handlebars-template
haskell
haxe
hive
html
http
httpd-php
httpd-php-open
hxml
ini
java
javascript
json
jsp
jsx
julia
kotlin
latex
less
literate-haskell
lua
mariadb
markdown
mbox
mirc
mscgen
msgenny
mssql
mumps
mysql
n-triples
nesc
nginx-conf
nsis
objectivec
octave
oz
pascal
perl
pgp
pgp-keys
pgp-signature
pgsql
php
pig
plsql
properties
protobuf
puppet
python
q
rpm-changes
rpm-spec
rsrc
ruby
rustsrc
sas
sass
scala
scheme
scss
sieve
slim
smarty
solr
soy
sparql-query
spreadsheet
sql
squirrel
stex
styl
swift
systemverilog
tcl
textile
tiddlywiki
tiki
tlv
tornado
ttcn-asn
ttcn-cfg
turtle
twig
typescript
typescript-jsx
vb
vbscript
velocity
verilog
vertex
vhdl
vue
webidl
xml
xml-dtd
xquery
xu
yaml
z80
default
Visibility:
public
unlisted
private
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"])