search.py 5.7 KB

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