File: _2_parse.py

package info (click to toggle)
python-whois 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 76 kB
  • sloc: python: 453; makefile: 5
file content (63 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from . import tld_regexpr
import re

TLD_RE = {}


def get_tld_re(tld):
    if tld in TLD_RE: return TLD_RE[tld]
    v = getattr(tld_regexpr, tld)
    extend = v.get('extend')
    if extend:
        e = get_tld_re(extend)
        tmp = e.copy()
        tmp.update(v)

    else:
        tmp = v

    if 'extend' in tmp: del tmp['extend']
    TLD_RE[tld] = dict((k, re.compile(v, re.IGNORECASE) if isinstance(v, str) else v) for k, v in tmp.items())
    return TLD_RE[tld]


[get_tld_re(tld) for tld in dir(tld_regexpr) if tld[0] != '_']


def do_parse(whois_str, tld):
    r = {}

    if whois_str.count('\n') < 5:
        s = whois_str.strip().lower()
    if 'extend' in tmp:
        del tmp['extend']
    TLD_RE[tld] = dict((k, re.compile(v, re.IGNORECASE) if isinstance(v, str) else v) for k, v in tmp.items())
    return TLD_RE[tld]


[get_tld_re(tld) for tld in dir(tld_regexpr) if tld[0] != '_']


def do_parse(whois_str, tld):
    r = {}

    if whois_str.count('\n') < 5:
        s = whois_str.strip().lower()
        if s == 'not found':
            return
        if s.count('error'):
            return
        raise Exception(whois_str)

    sn = re.findall(r'Server Name:\s?(.+)', whois_str, re.IGNORECASE)
    if sn:
        whois_str = whois_str[whois_str.find('Domain Name:'):]

    for k, v in TLD_RE.get(tld, TLD_RE['com']).items():
        if v is None:
            r[k] = ['']

        else:
            r[k] = v.findall(whois_str) or ['']

    return r