pygq.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. Copyright (C) 2018 Farooq Karimi Zadeh <fkz@riseup.net>
  3. Permission to use, copy, modify, and distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. """
  14. import requests
  15. class PyGQ:
  16. SURAH_LAST = 114
  17. SURAH_FIRST = 1
  18. def __init__(self, url="http://api.globalquran.com/ayah/", token="",
  19. lg_codes={}, cache_size=0):
  20. """url and token are API url and token. lg_codes is a dict which maps
  21. 2 letter language codes to their fullname. like:
  22. {"en": "en.sahih", "ar": "quran-simple"}
  23. """
  24. self.url = url
  25. self.token = token
  26. self.lg_codes = lg_codes
  27. self.cache_size = cache_size
  28. self.cache = list()
  29. def __update_cache(self):
  30. if not self.cache_size: return
  31. if len(self.cache) > self.cache_size:
  32. while len(self.cache) > self.cache_size:
  33. self.cache.pop()
  34. def getAyah(self, surah, ayah, lang):
  35. """Returns a dict containing the verse itself, ayah and surah number
  36. and id. example:
  37. {
  38. "surah": 1,
  39. "ayah": 1,
  40. "id": 1,
  41. "verse": "In the name of Allah, the Entirely Merciful, the Especially Merciful."
  42. }
  43. """
  44. if len(lang) == 2:
  45. if lang in self.lg_codes:
  46. lang = self.lg_codes[lang]
  47. else:
  48. raise ValueError(lang+ " is not supported using 2letter codes")
  49. if (not self.SURAH_FIRST <= surah <= self.SURAH_LAST):
  50. raise ValueError("surah(chapter) must be between "
  51. + str(self.SURAH_FIRST) + " and " +
  52. str(self.SURAH_LAST))
  53. # TODO: check if it's in cache, if so use cache, if not get and add to
  54. # cache
  55. if self.cache_size:
  56. for key, json_ in self.cache: # FIXME: better name than json_
  57. if (surah,ayah,lang) == key:
  58. self.cache.remove((key,json_))
  59. self.cache.insert(0, (key,json_))
  60. return json_
  61. req_url = self.url + str(surah) + ":" + str(ayah) + "/" + lang
  62. ayah_json = requests.get(req_url, params = {'key': self.token}).json()
  63. while len(ayah_json) == 1:
  64. ayah_json = ayah_json[next(iter(ayah_json))]
  65. if ayah_json["surah"] != surah:
  66. raise ValueError("Invalid ayah number for this surah")
  67. self.cache.insert(0, ((surah, ayah, lang), ayah_json))
  68. self.__update_cache()
  69. return ayah_json
  70. if __name__ == "__main__":
  71. import sys
  72. if len(sys.argv) < 2:
  73. print("Usage:", sys.argv[0], "surah:ayah [lang]")
  74. sys.exit(0)
  75. surah, ayah = sys.argv[1].split(":")
  76. surah = int(surah)
  77. ayah = int(ayah)
  78. lang = "en.sahih" if len(sys.argv) < 3 else sys.argv[2]
  79. Q = PyGQ()
  80. print(Q.getAyah(surah, ayah, lang)["verse"])