Skip to content

Commit

Permalink
Merge pull request sherlock-project#1218 from nblanke/color-refactor
Browse files Browse the repository at this point in the history
Refactoring of the no-color flag
  • Loading branch information
sdushantha committed Mar 10, 2022
2 parents 2ab7ca6 + aece915 commit 28b49ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 56 deletions.
88 changes: 35 additions & 53 deletions sherlock/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
results of queries.
"""
from result import QueryStatus
from colorama import Fore, Style, init
from colorama import Fore, Style


class QueryNotify():
class QueryNotify:
"""Query Notify Object.
Base class that describes methods available to notify the results of
a query.
It is intended that other classes inherit from this base class and
override the methods to implement specific functionality.
"""

def __init__(self, result=None):
"""Create Query Notify Object.
Expand Down Expand Up @@ -110,7 +111,8 @@ class QueryNotifyPrint(QueryNotify):
Query notify class that prints results.
"""
def __init__(self, result=None, verbose=False, color=True, print_all=False):

def __init__(self, result=None, verbose=False, print_all=False):
"""Create Query Notify Print Object.
Contains information about a specific method of notifying the results
Expand All @@ -122,19 +124,14 @@ def __init__(self, result=None, verbose=False, color=True, print_all=False):
results for this query.
verbose -- Boolean indicating whether to give verbose output.
print_all -- Boolean indicating whether to only print all sites, including not found.
color -- Boolean indicating whether to color terminal output
Return Value:
Nothing.
"""

# Colorama module's initialization.
init(autoreset=True)

super().__init__(result)
self.verbose = verbose
self.print_all = print_all
self.color = color

return

Expand All @@ -153,14 +150,11 @@ def start(self, message):
"""

title = "Checking username"
if self.color:
print(Style.BRIGHT + Fore.GREEN + "[" +
Fore.YELLOW + "*" +
Fore.GREEN + f"] {title}" +
Fore.WHITE + f" {message}" +
Fore.GREEN + " on:")
else:
print(f"[*] {title} {message} on:")
print(Style.BRIGHT + Fore.GREEN + "[" +
Fore.YELLOW + "*" +
Fore.GREEN + f"] {title}" +
Fore.WHITE + f" {message}" +
Fore.GREEN + " on:")

return

Expand All @@ -186,53 +180,41 @@ def update(self, result):

# Output to the terminal is desired.
if result.status == QueryStatus.CLAIMED:
if self.color:
print((Style.BRIGHT + Fore.WHITE + "[" +
Fore.GREEN + "+" +
Fore.WHITE + "]" +
response_time_text +
Fore.GREEN +
f" {self.result.site_name}: " +
Style.RESET_ALL +
f"{self.result.site_url_user}"))
else:
print(f"[+]{response_time_text} {self.result.site_name}: {self.result.site_url_user}")
print(Style.BRIGHT + Fore.WHITE + "[" +
Fore.GREEN + "+" +
Fore.WHITE + "]" +
response_time_text +
Fore.GREEN +
f" {self.result.site_name}: " +
Style.RESET_ALL +
f"{self.result.site_url_user}")

elif result.status == QueryStatus.AVAILABLE:
if self.print_all:
if self.color:
print((Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
response_time_text +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.YELLOW + " Not Found!"))
else:
print(f"[-]{response_time_text} {self.result.site_name}: Not Found!")
print(Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
response_time_text +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.YELLOW + " Not Found!")

elif result.status == QueryStatus.UNKNOWN:
if self.print_all:
if self.color:
print(Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.RED + f" {self.result.context}" +
Fore.YELLOW + f" ")
else:
print(f"[-] {self.result.site_name}: {self.result.context} ")
print(Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.RED + f" {self.result.context}" +
Fore.YELLOW + f" ")

elif result.status == QueryStatus.ILLEGAL:
if self.print_all:
msg = "Illegal Username Format For This Site!"
if self.color:
print((Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.YELLOW + f" {msg}"))
else:
print(f"[-] {self.result.site_name} {msg}")
print(Style.BRIGHT + Fore.WHITE + "[" +
Fore.RED + "-" +
Fore.WHITE + "]" +
Fore.GREEN + f" {self.result.site_name}:" +
Fore.YELLOW + f" {msg}")

else:
# It should be impossible to ever get here...
Expand Down
13 changes: 10 additions & 3 deletions sherlock/sherlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from result import QueryResult
from notify import QueryNotifyPrint
from sites import SitesInformation
from colorama import init

module_name = "Sherlock: Find Usernames Across Social Networks"
__version__ = "0.14.0"
Expand Down Expand Up @@ -591,7 +592,14 @@ def main():
if args.tor or args.unique_tor:
print("Using Tor to make requests")
print("Warning: some websites might refuse connecting over Tor, so note that using this option might increase connection errors.")


if args.no_color:
# Disable color output.
init(strip=True, convert=False)
else:
# Enable color output.
init(autoreset=True)

# Check if both output methods are entered as input.
if args.output is not None and args.folderoutput is not None:
print("You can only use one of the output methods.")
Expand Down Expand Up @@ -648,8 +656,7 @@ def main():
# Create notify object for query results.
query_notify = QueryNotifyPrint(result=None,
verbose=args.verbose,
print_all=args.print_all,
color=not args.no_color)
print_all=args.print_all)

# Run report on all specified users.
for username in args.username:
Expand Down

0 comments on commit 28b49ef

Please sign in to comment.