-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from itaybachar/feature
how: "Generalized"
- Loading branch information
Showing
4 changed files
with
125 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,47 +10,65 @@ | |
- [Usage](#usage) | ||
- [Foss](#foss) | ||
- [Community](#community) | ||
- [Contributors](#contributors) | ||
- [Copyright](#copyright) | ||
|
||
|
||
|
||
|
||
## Installation | ||
|
||
Install as you would with pip **using pypi** under the name `askquestions` - | ||
Install **using pypi** --> | ||
|
||
$ pip3 install askquestions | ||
|
||
Or **git** - | ||
Or **git** --> | ||
|
||
$ pip3 install git+https://github.com/ronnathaniel/how | ||
|
||
Or **from source** - | ||
Or **from source** --> | ||
|
||
$ git clone https://github.com/ronnathaniel/how.git | ||
$ cd how | ||
$ pip3 install . | ||
|
||
## Usage | ||
|
||
$ how QUERY ... [-n N] | ||
$ how QUERY* | ||
|
||
Ask your terminal directly. | ||
|
||
$ how do i exit vim | ||
➜ https://stackoverflow.com/questions/11828270/how-do-i-exit-the-vim-editor | ||
➜ https://stackoverflow.com/questions/25919461/i-cannot-exit-vim-i-hit-escape-and-tried-q-x-qx | ||
➜ https://stackoverflow.com/questions/47315349/how-to-exit-the-vim-editor-when-q-or-x-doesnt-work | ||
➜ https://stackoverflow.com/questions/1879219/how-to-temporarily-exit-vim-and-go-back | ||
➜ https://stackoverflow.com/questions/31140908/how-do-i-save-and-quit-from-vim | ||
➜ ... | ||
➜ ... | ||
|
||
Arguments | ||
Optional Arguments | ||
|
||
- `-n`: (int) Amount of links to return. Defaults to 5. | ||
**shorthand**|**longhand**|**type**|**default**|**description** | ||
:-----:|:-----:|:-----:|:-----:|:-----: | ||
-n|--num|int|5|Amount of links to return | ||
-g|--google|-|False|If exists | ||
-s|--sites|comma-sep list| stackoverflow.com| sites to check for using google.com | ||
|
||
And have fun. | ||
|
||
## FOSS | ||
$ how -n 10 -s youtube.com,stackoverflow.com,example.com exit vi | ||
|
||
Results from youtube.com: | ||
➜ ... | ||
➜ ... | ||
|
||
Results from stackoverflow.com: | ||
➜ ... | ||
➜ ... | ||
|
||
Results from example.com: | ||
None found. | ||
If a discrepency is found ➜ contact the team at [email protected]. | ||
|
||
[`googlesearch-python`](https://pypi.org/project/googlesearch-python/) - A Python library for scraping the Google search engine. | ||
## FOSS | ||
|
||
[`googlesearch-python`](https://pypi.org/project/googlesearch-python/) - "A Python library for scraping the Google search engine." | ||
|
||
|
||
## Community | ||
|
@@ -61,6 +79,9 @@ After all, it is only when we ask questions that we can learn anything. | |
|
||
Contributions are more than welcome, maintainers are always invited, and if you can ask questions you're a VIP. | ||
|
||
## Contributors | ||
|
||
[![](https://github.com/ronnathaniel.png?size=50)](https://github.com/ronnathaniel) [![](https://github.com/itaybachar.png?size=50)](https://github.com/itaybachar) | ||
## Copyright | ||
|
||
MIT License. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
|
||
""" | ||
Ask More Questions. | ||
Author: Ron Nathaniel | ||
Author: Ron Nathaniel, Itay Bachar | ||
""" | ||
|
||
from how.cli import parse_args, display_results | ||
from how.util import gen_next_n | ||
from how.crawler import ask_sof | ||
from how.crawler import ask_any, ask_sof, ask_google | ||
|
||
|
||
def run(): | ||
args = parse_args() | ||
|
||
query = args.get('query', '') | ||
query = ' '.join(query) | ||
|
||
n = args.get('n') | ||
g = args.get('g') | ||
sites = args.get('sites') | ||
|
||
results = ask_sof(query, limit=n) | ||
display_results(results) | ||
if sites: | ||
for site in sites: | ||
results = ask_any(query, limit=n, site=site) | ||
display_results(results, site) | ||
elif g: | ||
results = ask_google(query, limit=n) | ||
display_results(results, 'google.com') | ||
else: | ||
results = ask_sof(query, limit=n) | ||
display_results(results, 'stackoverflow.com') | ||
|
||
|
||
if __name__ == '__main__': | ||
run() | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,88 @@ | ||
|
||
""" | ||
Ask More Questions. | ||
Author: Ron Nathaniel | ||
Author: Ron Nathaniel, Itay Bachar | ||
""" | ||
|
||
import sys | ||
from enum import Enum | ||
import argparse | ||
import colorama | ||
|
||
colorama.init(wrap=False) | ||
stream_err = colorama.AnsiToWin32(sys.stderr).stream | ||
|
||
ARGS = { | ||
'query': { | ||
('query',): { | ||
'type': str, | ||
'metavar': 'QUERY', | ||
'nargs': '+', | ||
'help': 'Query to Search', | ||
}, | ||
'-n': { | ||
('-n', '--num'): { | ||
'type': int, | ||
'metavar': 'n', | ||
'nargs': '?', | ||
'help': 'Query to Search', | ||
'default': 5, | ||
}, | ||
('-g', '--google'): { | ||
'action': 'store_true', | ||
'help': 'Search on Google', | ||
}, | ||
('-s', '--sites'): { | ||
'type': str, | ||
'nargs': 1, | ||
'help': 'Sites to search from', | ||
'metavar': 'url[,url,...]', | ||
}, | ||
} | ||
|
||
|
||
class ConsoleStyles(str, Enum): | ||
RIGHT_ARROW = u'\u279c' | ||
YELLOW = u'\033[1;33m' | ||
BLUE_LIGHT = u'\033[1;36m' | ||
TEST = u'\033[1;38m' | ||
PURPLE = u'\033[1;35m' | ||
BLUE_DARK = u'\033[1;34m' | ||
GREEN = u'\033[1;32m' | ||
RED = u'\033[1;31m' | ||
BOLD = u'\033[1m' | ||
UNDERLINE = u'\033[4m' | ||
END = u'\033[0m' | ||
|
||
|
||
def parse_args() -> dict: | ||
parser = argparse.ArgumentParser(description='how') | ||
for op, kwargs in ARGS.items(): | ||
parser.add_argument(op, **kwargs) | ||
parser.add_argument(*op, **kwargs) | ||
|
||
args_parsed = vars(parser.parse_args()) | ||
|
||
query = args_parsed.get('query', []) | ||
n = args_parsed.get('n', 5) | ||
n = args_parsed.get('num', 5) | ||
g = args_parsed.get('google', False) | ||
|
||
sites = args_parsed.get('sites', ['']) | ||
if sites: | ||
sites = sites[0] | ||
sites = sites.split(',') | ||
|
||
return { | ||
'query': query, | ||
'n': n, | ||
'g': g, | ||
'sites': sites, | ||
} | ||
|
||
|
||
def display_results(res: list) -> None: | ||
def display_results(res: list = None, header: str = None) -> None: | ||
if header: | ||
print('\n' + "Results from " + ConsoleStyles.YELLOW + header + ConsoleStyles.END + ':' + ConsoleStyles.END) | ||
for r in res: | ||
print(u'\u279c' + '\r\t', end='') | ||
print(ConsoleStyles.RIGHT_ARROW + '\r\t', end='') | ||
print(r) | ||
if not res: | ||
print('None found.') | ||
print(ConsoleStyles.RED + 'If a discrepency is found ' + ConsoleStyles.RIGHT_ARROW + ' contact the team at [email protected].' + ConsoleStyles.END) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters