File: expiration_date.py

package info (click to toggle)
python-pyfunceble 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,144 kB
  • sloc: python: 27,918; sh: 142; makefile: 48
file content (337 lines) | stat: -rw-r--r-- 13,725 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
The tool to check the availability or syntax of domain, IP or URL.

::


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides our expiration date extracter.

Author:
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:
    https://pyfunceble.github.io/#/special-thanks

Contributors:
    https://pyfunceble.github.io/#/contributors

Project link:
    https://github.com/funilrys/PyFunceble

Project documentation:
    https://docs.pyfunceble.com

Project homepage:
    https://pyfunceble.github.io/

License:
::


    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

from typing import List, Optional

from PyFunceble.helpers.regex import RegexHelper
from PyFunceble.query.whois.converter.base import ConverterBase
from PyFunceble.query.whois.converter.digit2digits import Digit2Digits
from PyFunceble.query.whois.converter.month2unified import Month2Unified


class ExpirationDateExtractor(ConverterBase):
    """
    Provides an interface for the extraction of the expiration date.
    """

    PATTERNS: List[str] = [
        r"\[expires\s+on\](.*)",
        r"data\s+de\s+expiração\s+\/\s+Expiration\s+Date\s+\(dd\/mm\/yyyy\):(.*)",
        r"date\s+d'expiration:(.*)",
        r"domain_datebilleduntil:(.*)",
        r"Exp\s+date:(.*)",
        r"expiration:\.+(.*)",
        r"expiration\s+date:(.*)",
        r"expiration\s+date\s+:(.*)",
        r"expiration\s+time:(.*)",
        r"expire-date:(.*)",
        r"expire:(.*)",
        r"expire\s+date:(.*)",
        r"expire\s+on:(.*)",
        r"expired:(.*)",
        r"expires:(.*)",
        r"expires\.+:(.*)",
        r"expires\s+at:(.*)",
        r"expires\s+on:(.*)",
        r"expires\s+on\.+:(.*)",
        r"expiry\s+:(.*)",
        r"expiry\s+date:(.*)",
        r"fecha\s+de\s+expiración\s+\(expiration\s+date\):(.*)",
        r"fecha\s+de\s+vencimiento:(.*)",
        r"free-date(.*)",
        r"record\s+expires\s+on(.*)(\(YYYY-MM-DD\))",
        r"registry\s+expiry\s+date:(.*)",
        r"renewal:(.*)",
        r"renewal\s+date:(.*)",
        r"status:\s+OK-UNTIL(.*)",
        r"valid-date(.*)",
        r"validity:(.*)",
    ]
    """
    Provides all our known patterns.
    """

    REGEX_DIGITS: str = r"[0-9]"
    """
    Provides the regex to match in order to extract the digits.
    """

    MARKER2DATE_REGEX: dict = {
        # Date in format: 02-jan-2017
        "1": r"([0-9]{2})-([a-z]{3})-([0-9]{4})",
        # Date in format: 02.01.2017 // Month: jan
        "2": r"([0-9]{2})\.([0-9]{2})\.([0-9]{4})$",
        # Date in format: 02/01/2017 // Month: jan
        "3": r"([0-3][0-9])\/(0[1-9]|1[012])\/([0-9]{4})",
        # Date in format: 2017-01-02 // Month: jan
        "4": r"([0-9]{4})-([0-9]{2})-([0-9]{2})$",
        # Date in format: 2017.01.02 // Month: jan
        "5": r"([0-9]{4})\.([0-9]{2})\.([0-9]{2})$",
        # Date in format: 2017/01/02 // Month: jan
        "6": r"([0-9]{4})\/([0-9]{2})\/([0-9]{2})$",
        # Date in format: 2017.01.02 15:00:00
        "7": r"([0-9]{4})\.([0-9]{2})\.([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}",
        # Date in format: 20170102 15:00:00 // Month: jan
        "8": r"([0-9]{4})([0-9]{2})([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}",
        # Date in format: 2017-01-02 15:00:00 // Month: jan
        "9": r"([0-9]{4})-([0-9]{2})-([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}",
        # Date in format: 02.01.2017 15:00:00 // Month: jan
        "10": r"([0-9]{2})\.([0-9]{2})\.([0-9]{4})\s[0-9]{2}:[0-9]{2}:[0-9]{2}",
        # Date in format: 02-Jan-2017 15:00:00 UTC
        "11": r"([0-9]{2})-([A-Z]{1}[a-z]{2})-([0-9]{4})\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{1}.*",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: 2017/01/02 01:00:00 (+0900) // Month: jan
        "12": r"([0-9]{4})\/([0-9]{2})\/([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s\(.*\)",
        # Date in format: 2017/01/02 01:00:00 // Month: jan
        "13": r"([0-9]{4})\/([0-9]{2})\/([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}$",
        # Date in format: Mon Jan 02 15:00:00 GMT 2017
        "14": r"[a-zA-Z]{3}\s([a-zA-Z]{3})\s([0-9]{2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{3}\s([0-9]{4})",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: Mon Jan 02 2017
        "15": r"[a-zA-Z]{3}\s([a-zA-Z]{3})\s([0-9]{2})\s([0-9]{4})",
        # Date in format: 2017-01-02T15:00:00 // Month: jan
        "16": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}$",
        # Date in format: 2017-01-02T15:00:00Z // Month: jan${'7}
        "17": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}[A-Z].*",
        # Date in format: 2017-01-02T15:00:00+0200 // Month: jan
        "18": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{4}",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: 2017-01-02T15:00:00+0200.622265+03:00 //
        # Month: jan
        "19": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9].*[+-][0-9]{2}:[0-9]{2}",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: 2017-01-02T15:00:00+0200.622265 // Month: jan
        "20": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}$",
        # Date in format: 2017-01-02T23:59:59.0Z // Month: jan
        "21": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9].*[A-Z]",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: 02-01-2017 // Month: jan
        "22": r"([0-9]{2})-([0-9]{2})-([0-9]{4})",
        # Date in format: 2017. 01. 02. // Month: jan
        "23": r"([0-9]{4})\.\s([0-9]{2})\.\s([0-9]{2})\.",
        # Date in format: 2017-01-02T00:00:00+13:00 // Month: jan
        "24": r"([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{2}:[0-9]{2}",  # pylint: disable=line-too-long  # noqa: E501
        # Date in format: 20170102 // Month: jan
        "25": r"(?=[0-9]{8})(?=([0-9]{4})([0-9]{2})([0-9]{2}))",
        # Date in format: 02-Jan-2017
        "26": r"([0-9]{2})-([A-Z]{1}[a-z]{2})-([0-9]{4})$",
        # Date in format: 02.1.2017 // Month: jan
        "27": r"([0-9]{2})\.([0-9]{1})\.([0-9]{4})",
        # Date in format: 02 Jan 2017
        "28": r"([0-9]{1,2})\s([A-Z]{1}[a-z]{2})\s([0-9]{4})",
        # Date in format: 02-January-2017
        "29": r"([0-9]{2})-([A-Z]{1}[a-z]*)-([0-9]{4})",
        # Date in format: 2017-Jan-02.
        "30": r"([0-9]{4})-([A-Z]{1}[a-z]{2})-([0-9]{2})\.",
        # Date in format: Mon Jan 02 15:00:00 2017
        "31": r"[a-zA-Z]{3}\s([a-zA-Z]{3})\s([0-9]{1,2})\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s([0-9]{4})",  # pylint: disable=line-too-long   # noqa: E501
        # Date in format: Mon Jan 2017 15:00:00
        "32": r"()[a-zA-Z]{3}\s([a-zA-Z]{3})\s([0-9]{4})\s[0-9]{2}:[0-9]{2}:[0-9]{2}",
        # Date in format: January 02 2017-Jan-02
        "33": r"([A-Z]{1}[a-z]*)\s([0-9]{1,2})\s([0-9]{4})",
        # Date in format: 2.1.2017 // Month: jan
        "34": r"([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})",
        # Date in format: 20170102000000 // Month: jan
        "35": r"([0-9]{4})([0-9]{2})([0-9]{2})[0-9]+",
        # Date in format: 01/02/2017 // Month: jan
        "36": r"(0[1-9]|1[012])\/([0-3][0-9])\/([0-9]{4})",
        # Date in format: January  2 2017
        "37": r"([A-Z]{1}[a-z].*)\s\s([0-9]{1,2})\s([0-9]{4})",
        # Date in format: 2nd January 2017
        "38": r"([0-9]{1,})[a-z]{1,}\s([A-Z].*)\s(2[0-9]{3})",
    }
    """
    Provides all the known regex to extract the date.

    .. note::
        The index (or key) will be used to map or group some of them later.
    """

    REGEX_PARSE_MAP: List[dict] = [
        {
            "regex_keys": [1, 2, 3, 10, 11, 22, 26, 27, 28, 29, 32, 34, 38],
            "positions": {"day": 0, "month": 1, "year": 2},
        },
        {
            "regex_keys": [14, 15, 31, 33, 36, 37],
            "positions": {"day": 1, "month": 0, "year": 2},
        },
        {
            "regex_keys": [
                4,
                5,
                6,
                7,
                8,
                9,
                12,
                13,
                16,
                17,
                18,
                19,
                20,
                21,
                23,
                24,
                25,
                30,
                35,
            ],
            "positions": {"day": 2, "month": 1, "year": 0},
        },
    ]
    """
    Our parsing map. Indeed, we have a list of regex, but no way to know
    how to parse them. Especially when the order (month, day, year) are
    different from a format to another.

    This variable solve that problem by interpreting all regex we previously
    created.
    """

    _regex_helper: Optional[RegexHelper] = None

    @property
    def regex_helper(self) -> "RegexHelper":
        """
        Provides the regex helper to use.
        """

        if self._regex_helper is None:
            self._regex_helper = RegexHelper()

        return self._regex_helper

    @ConverterBase.data_to_convert.setter
    def data_to_convert(self, value: str) -> None:
        """
        Sets the data to convert or work with.

        :param value:
            The record to work with.

        :raise TypeError:
            When the given :code:`value` is not a :py:class:`str`.
        :raise ValueError:
            When the given :code:`value` is empty.
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} is given.")

        if not value:
            raise ValueError("<value> should not be empty.")

        # pylint: disable=no-member
        super(ExpirationDateExtractor, self.__class__).data_to_convert.fset(self, value)

    def __get_line(self) -> Optional[str]:
        """
        Tries to get the expiration date line from the given record.
        """

        for regex in self.PATTERNS:
            expiration_date_line = self.regex_helper.set_regex(r"(?i)" + regex).match(
                self.data_to_convert, return_match=True, rematch=True, group=0
            )

            if not expiration_date_line:
                continue

            return expiration_date_line
        return None

    def __get_actual_expiration_date(self, extracted: str) -> Optional[str]:
        """
        Tries to extract the actual expiration date.
        """

        for index, date_regex in self.MARKER2DATE_REGEX.items():
            matched = self.regex_helper.set_regex(date_regex).match(
                extracted, return_match=True, rematch=True
            )
            date_parts = tuple()

            if not matched:
                continue

            for parse_case in self.REGEX_PARSE_MAP:
                if int(index) not in parse_case["regex_keys"]:
                    continue

                date_parts = (
                    Digit2Digits(
                        matched[parse_case["positions"]["day"]],
                    ).get_converted(),
                    Month2Unified(
                        matched[parse_case["positions"]["month"]]
                    ).get_converted(),
                    str(matched[parse_case["positions"]["year"]]),
                )

            if date_parts:
                return "-".join(date_parts)
        return None

    @ConverterBase.ensure_data_to_convert_is_given
    def get_converted(self) -> Optional[str]:
        """
        Provides the expiration date of the record (if found).
        """

        expiration_date_line = self.__get_line()

        if expiration_date_line:
            expiration_date = expiration_date_line[0].strip()

            if self.regex_helper.set_regex(self.REGEX_DIGITS).match(
                expiration_date, return_match=False
            ):
                return self.__get_actual_expiration_date(expiration_date)

        return None