1111 Callable ,
1212)
1313
14+
1415from .version import (
1516 VERSION ,
1617)
3233 mergeExternalDictWithRegex ,
3334)
3435
35- from .doQuery import query , q2
36- from .lastWhois import get_last_raw_whois_data
36+ from .processWhoisDomainRequest import ProcessWhoisDomainRequest
37+ from .lastWhois import (
38+ get_last_raw_whois_data ,
39+ initLastWhois ,
40+ )
3741from .doWhoisCommand import setMyCache
38- from .noneStrings import NoneStrings , NoneStringsAdd
39- from .quotaStrings import QuotaStrings , QuotaStringsAdd
42+ from .domain import Domain
4043
44+ from .strings .noneStrings import NoneStrings , NoneStringsAdd
45+ from .strings .quotaStrings import QuotaStrings , QuotaStringsAdd
46+ from .tldDb .tld_regexpr import ZZ
47+ from .context .dataContext import DataContext
48+ from .context .parameterContext import ParameterContext
49+ from .cache .simpleCacheBase import SimpleCacheBase
50+ from .cache .simpleCacheWithFile import SimpleCacheWithFile
51+ from .cache .dummyCache import DummyCache
52+ from .cache .dbmCache import DBMCache
4153
42- from . doParse import (
43- cleanupWhoisResponse ,
44- )
54+ HAS_REDIS = False
55+ try :
56+ import redis
4557
46- from .tld_regexpr import ZZ
47- from .domain import Domain
48- from .parameterContext import ParameterContext
58+ HAS_REDIS = True
59+ except Exception as e :
60+ _ = e
61+
62+ if HAS_REDIS :
63+ from .cache .redisCache import RedisCache
4964
50- from .simpleCacheBase import SimpleCacheBase
51- from .simpleCacheWithFile import SimpleCacheWithFile
52- from .dummyCache import DummyCache
53- from .dbmCache import DBMCache
65+ WHOISDOMAIN : str = ""
66+ if os .getenv ("WHOISDOMAIN" ):
67+ WHOISDOMAIN = str (os .getenv ("WHOISDOMAIN" ))
5468
55- # from .redisCache import RedisCache
69+ WD = WHOISDOMAIN .upper ().split (":" )
70+
71+ SIMPLISTIC = False
72+ if "SIMPLISTIC" in WD :
73+ SIMPLISTIC = True
5674
5775TLD_LIB_PRESENT : bool = False
5876try :
7492 # from init_tld
7593 "validTlds" ,
7694 "TLD_RE" ,
77- "mergeExternalDictWithRegex" ,
7895 # from version
7996 "VERSION" ,
8097 # from parameterContext
@@ -112,6 +129,77 @@ def _inner(*args: str, **kw: Any) -> Dict[str, Any]:
112129 return _inner
113130
114131
132+ def q2 (
133+ domain : str ,
134+ pc : ParameterContext ,
135+ ) -> Optional [Domain ]:
136+
137+ initLastWhois ()
138+
139+ dc = DataContext (
140+ pc = pc ,
141+ domain = domain ,
142+ )
143+ pwdr = ProcessWhoisDomainRequest (
144+ domain = domain ,
145+ pc = pc ,
146+ dc = dc ,
147+ )
148+
149+ return pwdr .processRequest ()
150+
151+
152+ def query (
153+ domain : str ,
154+ force : bool = False ,
155+ cache_file : Optional [str ] = None ,
156+ cache_age : int = 60 * 60 * 48 ,
157+ slow_down : int = 0 ,
158+ ignore_returncode : bool = False ,
159+ server : Optional [str ] = None ,
160+ verbose : bool = False ,
161+ with_cleanup_results : bool = False ,
162+ internationalized : bool = False ,
163+ include_raw_whois_text : bool = False ,
164+ return_raw_text_for_unsupported_tld : bool = False ,
165+ timeout : Optional [float ] = None ,
166+ parse_partial_response : bool = False ,
167+ cmd : str = "whois" ,
168+ simplistic : bool = False ,
169+ withRedacted : bool = False ,
170+ pc : Optional [ParameterContext ] = None ,
171+ # if you use pc as argument all above params (except domain are ignored)
172+ ) -> Optional [Domain ]:
173+ # see documentation about paramaters in parameterContext.py
174+
175+ assert isinstance (domain , str ), Exception ("`domain` - must be <str>" )
176+
177+ if pc is None :
178+ pc = ParameterContext (
179+ force = force ,
180+ cache_file = cache_file ,
181+ cache_age = cache_age ,
182+ slow_down = slow_down ,
183+ ignore_returncode = ignore_returncode ,
184+ server = server ,
185+ verbose = verbose ,
186+ with_cleanup_results = with_cleanup_results ,
187+ internationalized = internationalized ,
188+ include_raw_whois_text = include_raw_whois_text ,
189+ return_raw_text_for_unsupported_tld = return_raw_text_for_unsupported_tld ,
190+ timeout = timeout ,
191+ parse_partial_response = parse_partial_response ,
192+ cmd = cmd ,
193+ simplistic = simplistic ,
194+ withRedacted = withRedacted ,
195+ )
196+
197+ if verbose :
198+ print (pc , file = sys .stderr )
199+
200+ return q2 (domain = domain , pc = pc )
201+
202+
115203# Add get function to support return result in dictionary form
116204get = _result2dict (query )
117205
@@ -129,3 +217,57 @@ def getTestHint(tldString: str) -> Optional[str]:
129217 return None
130218
131219 return str (ZZ [tldString ][k ])
220+
221+
222+ def cleanupWhoisResponse (
223+ whoisStr : str ,
224+ verbose : bool = False ,
225+ with_cleanup_results : bool = False ,
226+ withRedacted : bool = False ,
227+ pc : Optional [ParameterContext ] = None ,
228+ ) -> str :
229+ tmp2 : List [str ] = []
230+
231+ if pc is None :
232+ pc = ParameterContext (
233+ verbose = verbose ,
234+ withRedacted = withRedacted ,
235+ with_cleanup_results = with_cleanup_results ,
236+ )
237+
238+ skipFromHere = False
239+ tmp : List [str ] = whoisStr .split ("\n " )
240+ for line in tmp :
241+ if skipFromHere is True :
242+ continue
243+
244+ # some servers respond with: % Quota exceeded in the comment section (lines starting with %)
245+ if "quota exceeded" in line .lower ():
246+ raise WhoisQuotaExceeded (whoisStr )
247+
248+ if pc .with_cleanup_results is True and line .startswith ("%" ): # only remove if requested
249+ continue
250+
251+ if pc .withRedacted is False :
252+ if "REDACTED FOR PRIVACY" in line : # these lines contibute nothing so ignore
253+ continue
254+
255+ if (
256+ "Please query the RDDS service of the Registrar of Record" in line
257+ ): # these lines contibute nothing so ignore
258+ continue
259+
260+ # regular responses may at the end have meta info starting with a line >>> some texte <<<
261+ # similar trailing info exists with lines starting with -- but we wil handle them later
262+ # unfortunalery we have domains (google.st) that have this early at the top
263+ if 0 :
264+ if line .startswith (">>>" ):
265+ skipFromHere = True
266+ continue
267+
268+ if line .startswith ("Terms of Use:" ): # these lines contibute nothing so ignore
269+ continue
270+
271+ tmp2 .append (line .strip ("\r " ))
272+
273+ return "\n " .join (tmp2 )
0 commit comments