Skip to content
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

how Generalized #1

Merged
merged 4 commits into from Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 34 additions & 13 deletions README.md
Expand Up @@ -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 rnathaniel7@gmail.com.

[`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
Expand All @@ -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.
Expand Down
23 changes: 17 additions & 6 deletions how/__main__.py
@@ -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()
55 changes: 48 additions & 7 deletions how/cli.py
@@ -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 rnathaniel7@gmail.com.' + ConsoleStyles.END)
37 changes: 26 additions & 11 deletions how/crawler.py
Expand Up @@ -4,19 +4,17 @@
Author: Ron Nathaniel
"""

from typing import Generator
from googlesearch import search

from how.util import gen_next_n


def ask_google(query: str, limit=20,) -> list:
def ask_google(query: str, limit: int = 20) -> list:
"""
Ask Google Anything.
:param query: Query to google search
:param limit: Total results to return
:return: List of result URIs
"""

results = search(
query,
num_results=limit,
Expand All @@ -25,25 +23,42 @@ def ask_google(query: str, limit=20,) -> list:
return results


def ask_sof(query: str, limit=20, ) -> list:
def ask_any(query: str, limit: int = 20, site: str = None) -> list:
"""
Ask Any Site, Anything.
:param query: Query to search
:param limit: Total results to return
:param site: Site to search
:return: List of result URIs
"""
if site:
query += ' site:' + site
results = ask_google(
query=query,
limit=limit,
)

return results


def ask_sof(query: str, limit: int = 20) -> list:
"""
Ask StackOverflow Anything.
:param query: Query to StackOverflow search
:param limit: Total results to return
:return: List of result URIs
"""
results = ask_google(
query=query + ' site:stackoverflow.com',
limit=limit
results = ask_any(
query=query,
limit=limit,
site='stackoverflow.com',
)

return results


if __name__ == '__main__':
# Example Usage
# example usage
res = ask_sof(
'exit vi'
)

print(gen_next_n(res, 5))