Skip to content

Commit

Permalink
release: 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnathaniel committed Mar 12, 2022
1 parent f3a052c commit ad7a47e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 54 deletions.
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,57 @@

## 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
Expand All @@ -64,10 +80,8 @@ 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) Ron Nathaniel
>[![](https://github.com/itaybachar.png?size=50)](https://github.com/itaybachar) Itay Bachar

[![](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
24 changes: 13 additions & 11 deletions how/__main__.py
Original file line number Diff line number Diff line change
@@ -1,32 +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_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 = []

if not sites:
results = ask_google(query,limit=n) if g else ask_sof(query, limit=n)
display_results(results)
else:
if sites:
for site in sites:
results = ask_any(query,limit=n,site=site)
print()
display_results(results,site)
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()
47 changes: 32 additions & 15 deletions how/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

"""
Ask More Questions.
Author: Ron Nathaniel
Author: Ron Nathaniel, Itay Bachar
"""

import sys
from enum import Enum
import argparse
import colorama

Expand All @@ -18,27 +19,40 @@
'nargs': '+',
'help': 'Query to Search',
},
('-n',): {
('-n', '--num'): {
'type': int,
'metavar': 'n',
'nargs': '?',
'help': 'Query to Search',
'default': 5,
},
('-g','--google'): {
('-g', '--google'): {
'action': 'store_true',
'help': 'Search on Google',
},
('-a','--add-sites'):
{
('-s', '--sites'): {
'type': str,
'nargs': 1,
'help': 'Search different websites, this will not include stack overflow unless specified.',
'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():
Expand All @@ -47,13 +61,13 @@ def parse_args() -> dict:
args_parsed = vars(parser.parse_args())

query = args_parsed.get('query', [])
n = args_parsed.get('n', 5)
g = args_parsed.get('google',False)
sites = args_parsed.get('add_sites')
n = args_parsed.get('num', 5)
g = args_parsed.get('google', False)

#Process Sites
if sites and len(sites)>0:
sites = list(filter(lambda s: s != '',sites[0].split(',')))
sites = args_parsed.get('sites', [''])
if sites:
sites = sites[0]
sites = sites.split(',')

return {
'query': query,
Expand All @@ -63,9 +77,12 @@ def parse_args() -> dict:
}


def display_results(res: list,header:str = None) -> None:
def display_results(res: list = None, header: str = None) -> None:
if header:
print(u'\033[4m' + "Results from {0}".format(header) + u'\033[0m')
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)
30 changes: 18 additions & 12 deletions how/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
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
Expand All @@ -25,34 +22,43 @@ def ask_google(query: str, limit=20,) -> list:

return results

def ask_any(query: str, limit=20, site: str = 'stackoverflow.com') -> 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+ ' site:{0}'.format(site),
limit=limit
query=query,
limit=limit,
)

return results

def ask_sof(query: str, limit=20, ) -> list:

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
"""
return ask_any(query,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))

0 comments on commit ad7a47e

Please sign in to comment.