Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put this under a sub-bullet of -b?


## Built With
Python 3
Expand Down
14 changes: 10 additions & 4 deletions pySearch.py
Original file line number Diff line number Diff line change
@@ -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="+")

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to add your -b argument here.

args = argparser.parse_args()
searchObj = Search()
Expand Down
43 changes: 40 additions & 3 deletions search.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm debating if we should open the search in the default browser if the selected browser is invalid. This could be resolved in another issue if necessary but this is something that's worth thinking about.

#end of get browser

def buildLink(self):
if self.engine == "amazon":
self.searchString = "/s/keywords="
Expand All @@ -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):
Expand All @@ -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
#end of Query
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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