Add inverse validation and opt support

Support for site-specific modifiers has been added with type safety and some basic validation to help prevent regression.
Adds the 'invert' modifier to invert the CLAIMED/AVAILABLE status for a username, useful for some sites where it's easier to detect existance rather than exclude errors.
pull/2078/head
Paul Pfeister 2 months ago
parent 55c680fde1
commit 3745f836a4

@ -4,6 +4,21 @@ This module defines various objects for recording the results of queries.
"""
from enum import Enum
class QueryOptions(Enum):
"""Query option and modifier enumeration
"""
INVERT = "invert" # Invert the QueryStatus if CLAIMED or AVAILABLE (no effect on others)
def __str__(self):
"""Convert Object To String.
Keyword Arguments:
self -- This object.
Return Value:
Nicely formatted string to get information about this object.
"""
return self.value
class QueryStatus(Enum):
"""Query Status Enumeration.

@ -22,6 +22,7 @@ import requests
from requests_futures.sessions import FuturesSession
from torrequest import TorRequest
from result import QueryStatus
from result import QueryOptions
from result import QueryResult
from notify import QueryNotifyPrint
from sites import SitesInformation
@ -378,6 +379,26 @@ def sherlock(
query_status = QueryStatus.UNKNOWN
error_context = None
# Check data type of target options, and ensure it's always a [str] rather than str
# Maintains type safety with arbitrary user input
target_opts=[]
if net_info.get("options") is not None:
if isinstance(net_info.get("options"), str):
target_opts = [f"{net_info.get("options")}"]
elif isinstance(net_info.get("options"), list) and all(isinstance(item, str) for item in net_info.get("options")):
target_opts = net_info.get("options")
else:
raise ValueError(
f"Incompatible type {type(net_info.get("options"))} for " f"site '{social_network}' options"
)
# Validate option values (they must exist)
# Prevents accidental regression by changing option availability and not updating the manifest
unk_opts = [opt for opt in target_opts if opt not in QueryOptions]
if unk_opts:
raise ValueError(f"Unkown query option {unk_opts} for site {social_network}.")
del unk_opts
if error_text is not None:
error_context = error_text
@ -430,6 +451,13 @@ def sherlock(
raise ValueError(
f"Unknown Error Type '{error_type}' for " f"site '{social_network}'"
)
# Flips the claimed/available status of a username if option 'invert' is set in the manifest
if f"{QueryOptions.INVERT}" in target_opts:
if query_status is QueryStatus.CLAIMED:
query_status = QueryStatus.AVAILABLE
elif query_status is QueryStatus.AVAILABLE:
query_status = QueryStatus.CLAIMED
# Notify caller about results of query.
result = QueryResult(

Loading…
Cancel
Save