search.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. text = message.text.replace("/ud ", '')
  20. api = "http://api.urbandictionary.com/v0/define?term="
  21. if text != '':
  22. try:
  23. results = get(f"{api}{text}").json()
  24. reply_text = f'Definition: {results["list"][0]["definition"]}'
  25. reply_text += f'\n\nExample: {results["list"][0]["example"]}'
  26. except IndexError:
  27. reply_text = ("Sorry could not find any matching results!")
  28. ignore_chars = "[]"
  29. reply = reply_text
  30. for chars in ignore_chars:
  31. reply = reply.replace(chars, "")
  32. if len(reply) >= 4096:
  33. reply = reply[:4096]
  34. await message.reply_text(reply)
  35. else:
  36. await message.reply_text('"/ud" Needs An Argument.')
  37. # google
  38. @app.on_message(cust_filter.command(commands=("google")) & ~filters.edited)
  39. async def google(_, message: Message):
  40. text = message.text.replace("/google ", '')
  41. if text != '':
  42. gresults = await GoogleSearch().async_search(text, 1)
  43. result = ""
  44. for i in range(4):
  45. try:
  46. title = gresults["titles"][i].replace("\n", " ")
  47. source = gresults["links"][i]
  48. description = gresults["descriptions"][i]
  49. result += f"[{title}]({source})\n"
  50. result += f"`{description}`\n\n"
  51. except IndexError:
  52. pass
  53. await message.reply_text(result, disable_web_page_preview=True)
  54. else:
  55. await message.reply_text('"/google" Needs An Argument')
  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. gett = message.text.replace("/so ", '')
  60. text = gett + ' "site:stackoverflow.com"'
  61. if gett != '':
  62. gresults = await GoogleSearch().async_search(text, 1)
  63. result = ""
  64. for i in range(4):
  65. try:
  66. title = gresults["titles"][i].replace("\n", " ")
  67. source = gresults["links"][i]
  68. description = gresults["descriptions"][i]
  69. result += f"[{title}]({source})\n"
  70. result += f"`{description}`\n\n"
  71. except IndexError:
  72. pass
  73. await message.reply_text(result, disable_web_page_preview=True)
  74. else:
  75. await message.reply_text('"/so" Needs An Argument')
  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. gett = message.text.replace("/gh ", '')
  80. text = gett + ' "site:github.com"'
  81. if gett != '':
  82. gresults = await GoogleSearch().async_search(text, 1)
  83. result = ""
  84. for i in range(4):
  85. try:
  86. title = gresults["titles"][i].replace("\n", " ")
  87. source = gresults["links"][i]
  88. description = gresults["descriptions"][i]
  89. result += f"[{title}]({source})\n"
  90. result += f"`{description}`\n\n"
  91. except IndexError:
  92. pass
  93. await message.reply_text(result, disable_web_page_preview=True)
  94. else:
  95. await message.reply_text('"/gh" Needs An Argument')
  96. # Wikipedia
  97. @app.on_message(cust_filter.command(commands=("wiki")) & ~filters.edited)
  98. async def wiki(_, message: Message):
  99. query = message.text.replace("/wiki", '')
  100. limit = 5
  101. if message.reply_to_message:
  102. query = message.reply_to_message.text
  103. if not query:
  104. await message.reply_text('Reply To A Message Or Give An Argument')
  105. return
  106. wikipedia.set_lang("en")
  107. results = wikipedia.search(query)
  108. output = "```Found These Topics```"
  109. for i, j in enumerate(results, start=1):
  110. page = wikipedia.page(j)
  111. url = page.url
  112. output += f"[{j}]({url})\n"
  113. if i == limit:
  114. break
  115. await message.reply_text(output, disable_web_page_preview=True)
  116. # YouTube
  117. @app.on_message(cust_filter.command(commands=("yt")) & ~filters.edited)
  118. async def ytsearch(_, message: Message):
  119. m = await message.reply_text("Searching....")
  120. query = message.text.replace("/yt", '')
  121. results = YoutubeSearch(query, max_results=4).to_dict()
  122. i = 0
  123. text = ""
  124. while i < 4:
  125. text += f"Title - {results[i]['title']}\n"
  126. text += f"Duration - {results[i]['duration']}\n"
  127. text += f"Views - {results[i]['views']}\n"
  128. text += f"Channel - {results[i]['channel']}\n"
  129. text += f"https://youtube.com{results[i]['url_suffix']}\n\n"
  130. i += 1
  131. await m.edit(text, disable_web_page_preview=True)