-
Notifications
You must be signed in to change notification settings - Fork 5
Added -b or --browser parameter #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
427b1b1
e6ecfe1
9ef4d42
779a169
a1ac00f
7ac2387
773510f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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="+") | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to add your |
||
| args = argparser.parse_args() | ||
| searchObj = Search() | ||
|
|
||
| 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 | ||
|
|
@@ -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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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=" | ||
|
|
@@ -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 | ||
| #end of Query | ||
There was a problem hiding this comment.
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?