misc.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import re
  2. # import requests
  3. from math import ceil
  4. from pyrogram.types import InlineKeyboardButton
  5. from wbb import MOD_LOAD, MOD_NOLOAD
  6. def cleanhtml(raw_html):
  7. cleanr = re.compile("<.*?>")
  8. return re.sub(cleanr, "", raw_html)
  9. class EqInlineKeyboardButton(InlineKeyboardButton):
  10. def __eq__(self, other):
  11. return self.text == other.text
  12. def __lt__(self, other):
  13. return self.text < other.text
  14. def __gt__(self, other):
  15. return self.text > other.text
  16. def paginate_modules(page_n, module_dict, prefix, chat=None):
  17. if not chat:
  18. modules = sorted(
  19. [
  20. EqInlineKeyboardButton(
  21. x.__MODULE__,
  22. callback_data="{}_module({})".format(prefix, x.__MODULE__.lower()),
  23. )
  24. for x in module_dict.values()
  25. ]
  26. )
  27. else:
  28. modules = sorted(
  29. [
  30. EqInlineKeyboardButton(
  31. x.__MODULE__,
  32. callback_data="{}_module({},{})".format(
  33. prefix, chat, x.__MODULE__.lower()
  34. ),
  35. )
  36. for x in module_dict.values()
  37. ]
  38. )
  39. pairs = list(zip(modules[::3], modules[1::3], modules[2::3]))
  40. c = 0
  41. for x in pairs:
  42. for _ in x:
  43. c += 1
  44. if len(modules) - c == 1:
  45. pairs.append((modules[-1],))
  46. elif len(modules) - c == 2:
  47. pairs.append(
  48. (
  49. modules[-2],
  50. modules[-1],
  51. )
  52. )
  53. max_num_pages = ceil(len(pairs) / 7)
  54. modulo_page = page_n % max_num_pages
  55. # can only have a certain amount of buttons side by side
  56. if len(pairs) > 7:
  57. pairs = pairs[modulo_page * 7 : 7 * (modulo_page + 1)] + [
  58. (
  59. EqInlineKeyboardButton(
  60. "<", callback_data="{}_prev({})".format(prefix, modulo_page)
  61. ),
  62. EqInlineKeyboardButton(
  63. ">", callback_data="{}_next({})".format(prefix, modulo_page)
  64. ),
  65. )
  66. ]
  67. return pairs
  68. def is_module_loaded(name):
  69. return (not MOD_LOAD or name in MOD_LOAD) and name not in MOD_NOLOAD