search.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import wikipedia
  2. from search_engine_parser import GoogleSearch
  3. from pyrogram.types import Message
  4. from pyrogram import filters
  5. from requests import get
  6. from wbb import app
  7. from wbb.utils import cust_filter
  8. __MODULE__ = "Search"
  9. __HELP__ = '''/ud - Search For Something In Urban Dictionary
  10. /google - Search For Something On google
  11. /so - Search For Something On StackOverflow
  12. /gh - Search For Something On Github
  13. /wiki - Search For Something On Wikipedia'''
  14. # ud - urbandictionary
  15. @app.on_message(cust_filter.command(commands=("ud")) & ~filters.edited)
  16. async def urbandict(_, message: Message):
  17. text = message.text.replace("/ud ", '')
  18. api = "http://api.urbandictionary.com/v0/define?term="
  19. if text != '':
  20. try:
  21. results = get(f"{api}{text}").json()
  22. reply_text = f'Definition: {results["list"][0]["definition"]}'
  23. reply_text += f'\n\nExample: {results["list"][0]["example"]}'
  24. except IndexError:
  25. reply_text = ("Sorry could not find any matching results!")
  26. ignore_chars = "[]"
  27. reply = reply_text
  28. for chars in ignore_chars:
  29. reply = reply.replace(chars, "")
  30. if len(reply) >= 4096:
  31. reply = reply[:4096]
  32. await message.reply_text(reply)
  33. else:
  34. await message.reply_text('"/ud" Needs An Argument.')
  35. # google
  36. @app.on_message(cust_filter.command(commands=("google")) & ~filters.edited)
  37. async def google(_, message: Message):
  38. text = message.text.replace("/google ", '')
  39. if text != '':
  40. gresults = await GoogleSearch().async_search(text, 1)
  41. result = ""
  42. for i in range(4):
  43. try:
  44. title = gresults["titles"][i].replace("\n", " ")
  45. source = gresults["links"][i]
  46. description = gresults["descriptions"][i]
  47. result += f"[{title}]({source})\n"
  48. result += f"`{description}`\n\n"
  49. except IndexError:
  50. pass
  51. await message.reply_text(result, disable_web_page_preview=True)
  52. else:
  53. await message.reply_text('"/google" Needs An Argument')
  54. # StackOverflow [This is also a google search with some added args]
  55. @app.on_message(cust_filter.command(commands=("so")) & ~filters.edited)
  56. async def stack(_, message: Message):
  57. gett = message.text.replace("/so ", '')
  58. text = gett + ' "site:stackoverflow.com"'
  59. if gett != '':
  60. gresults = await GoogleSearch().async_search(text, 1)
  61. result = ""
  62. for i in range(4):
  63. try:
  64. title = gresults["titles"][i].replace("\n", " ")
  65. source = gresults["links"][i]
  66. description = gresults["descriptions"][i]
  67. result += f"[{title}]({source})\n"
  68. result += f"`{description}`\n\n"
  69. except IndexError:
  70. pass
  71. await message.reply_text(result, disable_web_page_preview=True)
  72. else:
  73. await message.reply_text('"/so" Needs An Argument')
  74. # Github [This is also a google search with some added args]
  75. @app.on_message(cust_filter.command(commands=("gh")) & ~filters.edited)
  76. async def github(_, message: Message):
  77. gett = message.text.replace("/gh ", '')
  78. text = gett + ' "site:github.com"'
  79. if gett != '':
  80. gresults = await GoogleSearch().async_search(text, 1)
  81. result = ""
  82. for i in range(4):
  83. try:
  84. title = gresults["titles"][i].replace("\n", " ")
  85. source = gresults["links"][i]
  86. description = gresults["descriptions"][i]
  87. result += f"[{title}]({source})\n"
  88. result += f"`{description}`\n\n"
  89. except IndexError:
  90. pass
  91. await message.reply_text(result, disable_web_page_preview=True)
  92. else:
  93. await message.reply_text('"/gh" Needs An Argument')
  94. # Wikipedia
  95. @app.on_message(cust_filter.command(commands=("wiki")) & ~filters.edited)
  96. async def wiki(_, message: Message):
  97. query = message.text.replace("/wiki", '')
  98. limit = 5
  99. if message.reply_to_message:
  100. query = message.reply_to_message.text
  101. if not query:
  102. await message.reply_text('Reply To A Message Or Give An Argument')
  103. return
  104. wikipedia.set_lang("en")
  105. results = wikipedia.search(query)
  106. output = "```Found These Topics```"
  107. for i, j in enumerate(results, start=1):
  108. page = wikipedia.page(j)
  109. url = page.url
  110. output += f"[{j}]({url})\n"
  111. if i == limit:
  112. break
  113. await message.reply_text(output, disable_web_page_preview=True)