diff --git a/README.md b/README.md index 58b961c..f085bd4 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ For Windows: Command Line Prompt - `-e` changes name or alias of search engine and sets it to as search engine for session - `-d` changes to domain extension - `-h` provides a description of each command line argument +- `-b` changes the browser to search in. If an invalid browser is selected, a list of valid ones will be displayed. + +Note for Windows users: browsers that are not added to the system environment variables will not be usable, or recognized as an available browser. ## Built With Python 3 diff --git a/pySearch.py b/pySearch.py index 208cccb..6c700bd 100644 --- a/pySearch.py +++ b/pySearch.py @@ -1,12 +1,18 @@ -#jrkong's command line searcher +# jrkong's command line searcher import argparse from search import Search def main(): argparser = argparse.ArgumentParser() - argparser.add_argument("-s", action="append", help="Takes a query to search for and searches it.", nargs="*", required=True) - argparser.add_argument("-e", "--engine", help="Changes the name or alias of a search engine and sets it as the search engine for the session", nargs="+") - argparser.add_argument("-d", "--domain", help="Changes the domain extention", nargs="+") + argparser.add_argument("-s", action="append", + help="Takes a query to search for and searches it.", + nargs="*", required=True) + argparser.add_argument("-e", "--engine", help=("Changes the name or alias of " + + "a search engine and sets it as the search engine for the session"), + nargs="+") + argparser.add_argument("-d", "--domain", + help="Changes the domain extention", + nargs="+") args = argparser.parse_args() searchObj = Search() diff --git a/search.py b/search.py index 74a08f9..d53cedc 100644 --- a/search.py +++ b/search.py @@ -1,12 +1,22 @@ import urllib import webbrowser +import re + +#print available broswers +def printAvailableBrowsers(invalid): + print("You have selected an invalid or unreigstered browser: " + invalid + ".\nHere is a list of available browsers") + for browser in webbrowser._browsers: + print("\t" + browser) +#end of printAvailableBrowsers + class Search: - def __init__(self, searchIn = None, engineIn = "google", domainIn = "ca"): + def __init__(self, searchIn = None, engineIn = "google", domainIn = "ca", browser=None): self.searchRaw = searchIn self.searchQuery = "" self.engine = engineIn self.domain = domainIn + self.browser = browser self.url = "" self.searchString = "/search?q=" #end of constructor @@ -26,6 +36,27 @@ def setQuery(self, searchIn): self.searchRaw = searchIn #end of setQuery + #set browser + def setBrowser(self, browser): + regex = {'chrome': '(google|chrome|google chrome|google-chrome)', + 'firefox': '(firefox|mozilla|mozilla firefox|mozilla-firefox)', + 'iexplore': '(ie|internet explorer|internet-explorer|iexplorer|iexplore)', + 'safari': 'safari'} + for i in regex: + match = re.search(regex[i], browser, re.IGNORECASE) + if match: + browser = i + self.browser = browser + #end of setBrowser + + #get browser + def getBrowser(self): + try: + webbrowser.get(self.browser).open_new_tab(self.url) + except(webbrowser.Error): + printAvailableBrowsers(self.browser) + #end of get browser + def buildLink(self): if self.engine == "amazon": self.searchString = "/s/keywords=" @@ -40,7 +71,10 @@ def buildLink(self): #end of link building def openBrowser(self): - webbrowser.open_new_tab(self.url) + if self.browser is None: + webbrowser.open_new_tab(self.url) + else: + self.getBrowser() #end of openBrowser() def handleArgs(self, args): @@ -50,10 +84,13 @@ def handleArgs(self, args): if args.domain is not None: self.setDomain(args.domain[0]) + if args.browser is not None: + self.setBrowser(args.browser[0]) + for search in args.s: self.setQuery(search) self.buildLink() self.openBrowser() #end of handleArgs -#end of Query \ No newline at end of file +#end of Query diff --git a/setup.cfg b/setup.cfg index d708177..4e7b761 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,7 +11,8 @@ ignore = E501, # line too long (105 > 79 characters) F401, # '...' imported but unused W292, # no newline at end of file - W293 # blank line contains whitespace + W293, # blank line contains whitespace + W503 # line break before binary operator exclude = __init__.py, setup.cfg