search.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from youtube_search import YoutubeSearch
  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. /yt - Search For Something On YouTube'''
  12. # ud - urbandictionary
  13. @app.on_message(cust_filter.command(commands=("ud")) & ~filters.edited)
  14. async def urbandict(_, message: Message):
  15. if len(message.command) < 2:
  16. await message.reply_text('"/ud" Needs An Argument.')
  17. return
  18. text = message.text.split(None, 1)[1]
  19. api = "http://api.urbandictionary.com/v0/define?term="
  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. # google
  34. @app.on_message(cust_filter.command(commands=("google")) & ~filters.edited)
  35. async def google(_, message: Message):
  36. try:
  37. if len(message.command) < 2:
  38. await message.reply_text('"/google" Needs An Argument')
  39. return
  40. text = message.text.split(None, 1)[1]
  41. gresults = await GoogleSearch().async_search(text, 1)
  42. result = ""
  43. for i in range(4):
  44. try:
  45. title = gresults["titles"][i].replace("\n", " ")
  46. source = gresults["links"][i]
  47. description = gresults["descriptions"][i]
  48. result += f"[{title}]({source})\n"
  49. result += f"`{description}`\n\n"
  50. except IndexError:
  51. pass
  52. await message.reply_text(result, disable_web_page_preview=True)
  53. except Exception as e:
  54. await message.reply_text(str(e))
  55. # StackOverflow [This is also a google search with some added args]
  56. @app.on_message(cust_filter.command(commands=("so")) & ~filters.edited)
  57. async def stack(_, message: Message):
  58. try:
  59. if len(message.command) < 2:
  60. await message.reply_text('"/so" Needs An Argument')
  61. return
  62. gett = message.text.split(None, 1)[1]
  63. text = gett + ' "site:stackoverflow.com"'
  64. gresults = await GoogleSearch().async_search(text, 1)
  65. result = ""
  66. for i in range(4):
  67. try:
  68. title = gresults["titles"][i].replace("\n", " ")
  69. source = gresults["links"][i]
  70. description = gresults["descriptions"][i]
  71. result += f"[{title}]({source})\n"
  72. result += f"`{description}`\n\n"
  73. except IndexError:
  74. pass
  75. await message.reply_text(result, disable_web_page_preview=True)
  76. except Exception as e:
  77. await message.reply_text(str(e))
  78. # Github [This is also a google search with some added args]
  79. @app.on_message(cust_filter.command(commands=("gh")) & ~filters.edited)
  80. async def github(_, message: Message):
  81. try:
  82. if len(message.command) < 2:
  83. await message.reply_text('"/gh" Needs An Argument')
  84. return
  85. gett = message.text.split(None, 1)[1]
  86. text = gett + ' "site:github.com"'
  87. gresults = await GoogleSearch().async_search(text, 1)
  88. result = ""
  89. for i in range(4):
  90. try:
  91. title = gresults["titles"][i].replace("\n", " ")
  92. source = gresults["links"][i]
  93. description = gresults["descriptions"][i]
  94. result += f"[{title}]({source})\n"
  95. result += f"`{description}`\n\n"
  96. except IndexError:
  97. pass
  98. await message.reply_text(result, disable_web_page_preview=True)
  99. except Exception as e:
  100. await message.reply_text(str(e))
  101. # Wikipedia
  102. @app.on_message(cust_filter.command(commands=("wiki")) & ~filters.edited)
  103. async def wiki(_, message: Message):
  104. try:
  105. if len(message.command) < 2:
  106. await message.reply_text('/wiki Needs An Argument')
  107. return
  108. query = message.text.split(None, 1)[1]
  109. limit = 5
  110. wikipedia.set_lang("en")
  111. results = wikipedia.search(query)
  112. output = "```Found These Topics```"
  113. for i, j in enumerate(results, start=1):
  114. page = wikipedia.page(j)
  115. url = page.url
  116. output += f"[{j}]({url})\n"
  117. if i == limit:
  118. break
  119. await message.reply_text(output, disable_web_page_preview=True)
  120. except Exception as e:
  121. await message.reply_text(str(e))
  122. # YouTube
  123. @app.on_message(cust_filter.command(commands=("yt")) & ~filters.edited)
  124. async def ytsearch(_, message: Message):
  125. try:
  126. if len(message.command) < 2:
  127. await message.reply_text("/yt needs an argument")
  128. return
  129. query = message.text.split(None, 1)[1]
  130. m = await message.reply_text("Searching....")
  131. results = YoutubeSearch(query, max_results=4).to_dict()
  132. i = 0
  133. text = ""
  134. while i < 4:
  135. text += f"Title - {results[i]['title']}\n"
  136. text += f"Duration - {results[i]['duration']}\n"
  137. text += f"Views - {results[i]['views']}\n"
  138. text += f"Channel - {results[i]['channel']}\n"
  139. text += f"https://youtube.com{results[i]['url_suffix']}\n\n"
  140. i += 1
  141. await m.edit(text, disable_web_page_preview=True)
  142. except Exception as e:
  143. await message.reply_text(str(e))