search.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if len(message.command) < 2:
  41. await message.reply_text('"/google" Needs An Argument')
  42. return
  43. text = message.text.split(None, 1)[1]
  44. gresults = await GoogleSearch().async_search(text, 1)
  45. result = ""
  46. for i in range(4):
  47. try:
  48. title = gresults["titles"][i].replace("\n", " ")
  49. source = gresults["links"][i]
  50. description = gresults["descriptions"][i]
  51. result += f"[{title}]({source})\n"
  52. result += f"`{description}`\n\n"
  53. except IndexError:
  54. pass
  55. await message.reply_text(result, disable_web_page_preview=True)
  56. # StackOverflow [This is also a google search with some added args]
  57. @app.on_message(cust_filter.command(commands=("so")) & ~filters.edited)
  58. async def stack(_, message: Message):
  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. # Github [This is also a google search with some added args]
  77. @app.on_message(cust_filter.command(commands=("gh")) & ~filters.edited)
  78. async def github(_, message: Message):
  79. if len(message.command) < 2:
  80. await message.reply_text('"/gh" Needs An Argument')
  81. return
  82. gett = message.text.split(None, 1)[1]
  83. text = gett + ' "site:github.com"'
  84. gresults = await GoogleSearch().async_search(text, 1)
  85. result = ""
  86. for i in range(4):
  87. try:
  88. title = gresults["titles"][i].replace("\n", " ")
  89. source = gresults["links"][i]
  90. description = gresults["descriptions"][i]
  91. result += f"[{title}]({source})\n"
  92. result += f"`{description}`\n\n"
  93. except IndexError:
  94. pass
  95. await message.reply_text(result, disable_web_page_preview=True)
  96. # Wikipedia
  97. @app.on_message(cust_filter.command(commands=("wiki")) & ~filters.edited)
  98. async def wiki(_, message: Message):
  99. if len(message.command) < 2:
  100. await message.reply_text('/wiki Needs An Argument')
  101. return
  102. query = message.text.split(None, 1)[1]
  103. limit = 5
  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)
  114. # YouTube
  115. @app.on_message(cust_filter.command(commands=("yt")) & ~filters.edited)
  116. async def ytsearch(_, message: Message):
  117. if len(message.command) < 2:
  118. await message.reply_text("/yt needs an argument")
  119. return
  120. query = message.text.split(None, 1)[1]
  121. m = await message.reply_text("Searching....")
  122. results = YoutubeSearch(query, max_results=4).to_dict()
  123. i = 0
  124. text = ""
  125. while i < 4:
  126. text += f"Title - {results[i]['title']}\n"
  127. text += f"Duration - {results[i]['duration']}\n"
  128. text += f"Views - {results[i]['views']}\n"
  129. text += f"Channel - {results[i]['channel']}\n"
  130. text += f"https://youtube.com{results[i]['url_suffix']}\n\n"
  131. i += 1
  132. await m.edit(text, disable_web_page_preview=True)