plugin.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. '''
  2. Copyright 2017-2018 Farooq Karimi Zadeh <fkz@riseup.net>
  3. This file is part of KoranFinder.
  4. KoranFinder is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This software is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this software. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems you probably can find a version of GPLv3 in
  15. /usr/share/common-licenses/
  16. '''
  17. import supybot.utils as utils
  18. from supybot.commands import *
  19. import supybot.plugins as plugins
  20. import supybot.ircutils as ircutils
  21. import supybot.callbacks as callbacks
  22. from . import pygq
  23. try:
  24. from supybot.i18n import PluginInternationalization
  25. _ = PluginInternationalization('KoranFinder')
  26. except ImportError:
  27. # Placeholder that allows to run the plugin on a bot
  28. # without the i18n module
  29. _ = lambda x: x
  30. # Set languages and translations the bot will accept
  31. quranID = {"ar" : "quran-simple", "en" : "en.sahih", "tr" : "tr.yazir", "fa" : "fa.fooladvand"}
  32. API_TOKEN = ""
  33. def arg2list(ayat): # TODO: better name for this function
  34. MAX_AYAT = 6
  35. ayat = ayat.split(",")
  36. ayat_l = []
  37. for aya in ayat:
  38. if aya.isdigit():
  39. ayat_l += [int(aya)]
  40. else:
  41. t = aya.split('-')
  42. start, end = int(t[0]), int(t[1])
  43. ayat_l += list(range(start, end + 1))
  44. if len(ayat_l) > MAX_AYAT:
  45. raise ValueError("Sorry, you can get just %s ayat each time calling me." % str(MAX_AYAT))
  46. else:
  47. return ayat_l
  48. class KoranFinder(callbacks.Plugin):
  49. """This plugin gets verse and ayah number and sends you the ayah using a web API."""
  50. threaded = True
  51. def __init__(self, irc):
  52. self.__parent = super(KoranFinder, self)
  53. self.__parent.__init__(irc)
  54. # TODO: let user config cache_size
  55. self.PyGQ = pygq.PyGQ(token = API_TOKEN, lg_codes=quranID,cache_size=100)
  56. def koran(self, irc, msg, args, surah, ayat, lang):
  57. """<surah> <ayah/ayat> <lang>
  58. returns ayah number <ayah> of surah number <surah> in <lang> language or translation or tafsir. for more information visit: https://git.io/vwMz9
  59. """
  60. try:
  61. list_of_ayat = arg2list(ayat)
  62. except ValueError as e:
  63. irc.error(str(e))
  64. return
  65. try:
  66. ayat_list = []
  67. for ayah in list_of_ayat:
  68. verse_json = self.PyGQ.getAyah(surah, ayah, lang)
  69. ayat_list.append(str(verse_json["surah"]) + ":" +
  70. str(verse_json["ayah"]) + ", " +
  71. str(verse_json["verse"]))
  72. except ValueError as e:
  73. irc.error(str(e))
  74. return
  75. except (KeyError, TypeError) as e: #TypeError incase requesting a audio version. The json would contain a list so qdata would raise a TypeError
  76. irc.error("Wrong translation code or broken API.")
  77. return
  78. for ayah in ayat_list:
  79. if self.registryValue('splitMessages'):
  80. ircMsgBytes = ayah.encode('utf-8')
  81. while len(ircMsgBytes) > 350:
  82. splitPoint = ircMsgBytes[0:351].rfind(' '.encode('utf-8'))
  83. irc.reply(ircMsgBytes[0:splitPoint].decode('utf-8').strip())
  84. ircMsgBytes = ircMsgBytes[splitPoint:]
  85. irc.reply(ircMsgBytes.decode('utf-8').strip())
  86. else:
  87. irc.reply(ayah)
  88. koran = wrap(koran, ["int", "something", optional("something", "en")])
  89. Class = KoranFinder
  90. # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: