File: _3_adjust.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 (104 lines) | stat: -rw-r--r-- 3,525 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import re
import sys
import datetime

PYTHON_VERSION = sys.version_info[0]


class Domain:

    def __init__(self, data):
        self.name = data['domain_name'][0].strip().lower()
        self.registrar          = data['registrar'][0].strip()
        self.creation_date      = str_to_date(data['creation_date'][0])
        self.expiration_date    = str_to_date(data['expiration_date'][0])
        self.last_updated       = str_to_date(data['updated_date'][0])

        #----------------------------------
        # name_servers
        tmp = []
        for x in data['name_servers']:
            if isinstance(x, str): tmp.append(x)
            else:
                for y in x: tmp.append(y)

        self.name_servers = set()
        for x in tmp:
            x = x.strip(' .')
            if x:
                if ' ' in x:
                    x, _ = x.split(' ', 1)
                    x = x.strip(' .')

                self.name_servers.add(x.lower())


        #----------------------------------

# http://docs.python.org/library/datetime.html#strftime-strptime-behavior
DATE_FORMATS = [
    '%d-%b-%Y',                     # 02-jan-2000
    '%d.%m.%Y',                     # 02.02.2000
    '%d/%m/%Y',                     # 01/06/2011
    '%Y-%m-%d',                     # 2000-01-02
    '%Y.%m.%d',                     # 2000.01.02
    '%Y/%m/%d',                     # 2005/05/30

    '%Y.%m.%d %H:%M:%S',            # 2002.09.19 13:00:00
    '%Y%m%d %H:%M:%S',              # 20110908 14:44:51
    '%Y-%m-%d %H:%M:%S',            # 2011-09-08 14:44:51
    '%d.%m.%Y  %H:%M:%S',           # 19.09.2002 13:00:00
    '%d-%b-%Y %H:%M:%S %Z',         # 24-Jul-2009 13:20:03 UTC
    '%Y/%m/%d %H:%M:%S (%z)',       # 2011/06/01 01:05:01 (+0900)
    '%Y/%m/%d %H:%M:%S',            # 2011/06/01 01:05:01
    '%a %b %d %H:%M:%S %Z %Y',      # Tue Jun 21 23:59:59 GMT 2011
    '%a %b %d %Y',                  # Tue Dec 12 2000
    '%Y-%m-%dT%H:%M:%S',            # 2007-01-26T19:10:31
    '%Y-%m-%dT%H:%M:%SZ',           # 2007-01-26T19:10:31Z
    '%Y-%m-%dt%H:%M:%S.%fz',        # 2007-01-26t19:10:31.00z
    '%Y-%m-%dT%H:%M:%S%z',          # 2011-03-30T19:36:27+0200
    '%Y-%m-%dT%H:%M:%S.%f%z',       # 2011-09-08T14:44:51.622265+03:00
    '%Y-%m-%dt%H:%M:%S.%f',         # 2011-09-08t14:44:51.622265
    '%Y-%m-%dt%H:%M:%S',            # 2007-01-26T19:10:31
    '%Y-%m-%dt%H:%M:%SZ',           # 2007-01-26T19:10:31Z
    '%Y-%m-%dt%H:%M:%S.%fz',        # 2007-01-26t19:10:31.00z
    '%Y-%m-%dt%H:%M:%S%z',          # 2011-03-30T19:36:27+0200
    '%Y-%m-%dt%H:%M:%S.%f%z',       # 2011-09-08T14:44:51.622265+03:00
    '%Y%m%d',                       # 20110908
]


def str_to_date(s):
    s = s.strip().lower()
    if not s or s == 'not defined':
        return

    s = s.replace('(jst)', '(+0900)')
    s = re.sub('(\+[0-9]{2}):([0-9]{2})', '\\1\\2', s)
    s = re.sub('(\ #.*)', '', s)

    if PYTHON_VERSION < 3: return str_to_date_py2(s)

    for format in DATE_FORMATS:
        try:
            return datetime.datetime.strptime(s, format)
        except ValueError as e:
            pass

    raise ValueError("Unknown date format: '%s'" % s)


def str_to_date_py2(s):
    tmp = re.findall('\+([0-9]{2})00', s)
    if tmp:
        tz = int(tmp[0])
    else:
        z = 0

    for format in DATE_FORMATS:
        try:
            return datetime.datetime.strptime(s, format) + datetime.timedelta(hours=tz)
        except ValueError as e:
            pass

    raise ValueError("Unknown date format: '%s'" % s)