Simple Search Bot in Python

Greetings!!!

In this blog we will see how to make a Bot in python which will search everything you copy to clipboard. Source code at the end of blog.

Past Covid-19 all examinations are conducted online and we all know why study if we can search everything on the internet. Particularly my university asked us to attempt exam through google form. So we had to follow a sequence of steps to search each question on the internet and get the solution.

  • Select the question and Hit Ctrl + C on the keyboard
  • Open browser
  • Visit each MCQ website where solutions can be available
  • Search for solutions on all sites

This steps had to be repeated for each and every question. Which felt hectic after few tests. So i wrote this bot which allowed me to perform just first step and rest were taken care by the bot.

Enough ChitChat! Let's start with the actual code.

First part will be importing the packages we will be needing to make this work.

from pynput import keyboard
import time
import clipboard
import webbrowser
import pip

Now you will be thinking...

We imported everything but didn't install any package in our environment. Since this blog is all about automating steps we will be automating this part too. How? just copy paste this next block of code and run

def install(package):
    if hasattr(pip, 'main'):
        pip.main(['install', package])
    else:
        pip._internal.main(['install', package])


if __name__ == '__main__':
    install('pynput')
    install('clipboard')
    install('webbrowser')

Now that we have all setup we can go dive into the main logic. We will create a text file and list down all the sites on which we want to search for the solutions and name the file "sites.txt". Here is my list for reference.

sanfoundry.com
examradar.com
examveda.com
coursehero.com
careerride.com
tuteehub.com
quizelet.com
brainscape.com
compsciedu.com
ciscopress.com
gkseries.com
engineeringinterviewquestions.com
mcqslearns.com
texalab.com
brainly.in
educationforever.in
lecturenotes.in
pinoybix.org
calculator.in

Now we will ask for consent on which search engine to choose

The one which keeps privacy safe - DuckDuckGo

Or the one with plays with our privacy who's name shall not be taken (Google) 

print("WHICH SEARCH ENGINE DO YOU USE?")
print("ENTER '0' FOR DUCK DUCK GO ")
print("OR ANY OTHER NUMBER FOR GOOGLE")
bchoice = int(input("BE QUICK \n"))

if bchoice == 0:
    print("NOICE!!! SMORT!!! TOIT!!! KEEP USING DUCKDUCKGO")
else:
    print("LEVEL UP DUDE AND START USING DUCK DUCK GO")
    time.sleep(3)
    print("")
    print("")
    print("ANYWAYS")

print("")
print("")
print("PORTAL STARTED")
print("now select any text and press control plus C ")
print("")

Let's create a funtion to use this all info and do actual work. we will see each task in function one by one. Start by saving clipboard string into a variable

text = clipboard.paste()

A query needs to be created so that we can search for it on any search engine. And that will be done by combining our text to be searched and the sites list. One more thing we cannot enter empty spaces in URL so we will replace them all with "+" symbol.

query = text + sites
query = query.replace(" ", "+")

Compose final URL to be searched for based on the search engine choice and save it in a variable.

if bchoice == 0:
    openbrow = "https://duckduckgo.com/?q=" + query
else:
    openbrow = "https://www.google.com/search?q=" + query

Now we can open web browser and enter the URL using "open" method on "webbrowser" but new window for every question will be mess so we keep the new attribute as "new=2" and we also want to keep that tab in focus so "autoraise=True"

webbrowser.open(openbrow, new=2, autoraise=True)

The final funtion will look as following

def function_1():
    text = clipboard.paste()
    print(text)

    query = text + sites
    query = query.replace(" ", "+")

    if bchoice == 0:
        openbrow = "https://duckduckgo.com/?q=" + query
    else:
        openbrow = "https://www.google.com/search?q=" + query

    webbrowser.open(openbrow, new=2, autoraise=True)

Only thing left is to trigger this funtion when someone presses Ctrl key and C key at once. That's when a text is coppied into the clipboard. 

with keyboard.GlobalHotKeys({
    '<ctrl>+c': function_1}) as h:
    h.join()

DONE!!! 

Now just run the file and you will be good to go.

Source code of this entire bot can be found at https://github.com/vinaynpp/searchbot