whois/parser.py
changeset 39 68375a768598
parent 37 8ad334b5363b
child 40 9573d5bc9ad5
equal deleted inserted replaced
38:da8f2956db7e 39:68375a768598
     5 #
     5 #
     6 # This module is part of pywhois and is released under
     6 # This module is part of pywhois and is released under
     7 # the MIT license: http://www.opensource.org/licenses/mit-license.php
     7 # the MIT license: http://www.opensource.org/licenses/mit-license.php
     8 
     8 
     9 import re
     9 import re
    10 from datetime import datetime
    10 try:
    11    
    11     import dateutil.parser as dp
       
    12     from whois.time_zones import tz_data
       
    13     DATEUTIL = True
       
    14 except ImportError:
       
    15     from datetime import datetime
       
    16     DATEUTIL = False
       
    17 
       
    18 KNOWN_FORMATS = [
       
    19     '%d-%b-%Y', 				# 02-jan-2000
       
    20     '%Y-%m-%d', 				# 2000-01-02
       
    21     '%d.%m.%Y', 				# 2.1.2000
       
    22     '%Y.%m.%d',                 # 2000.01.02
       
    23     '%Y/%m/%d',                 # 2000/01/02
       
    24     '%d/%m/%Y',                 # 02/01/2013
       
    25     '%Y. %m. %d.',              # 2000. 01. 02.
       
    26     '%Y.%m.%d %H:%M:%S',        # 2014.03.08 10:28:24
       
    27     '%d-%b-%Y %H:%M:%S %Z',		# 24-Jul-2009 13:20:03 UTC
       
    28     '%a %b %d %H:%M:%S %Z %Y',  # Tue Jun 21 23:59:59 GMT 2011
       
    29     '%Y-%m-%dT%H:%M:%SZ',       # 2007-01-26T19:10:31Z
       
    30     '%Y-%m-%dT%H:%M:%S%z',      # 2013-12-06T08:17:22-0800
       
    31     '%Y-%m-%d %H:%M:%SZ',       # 2000-08-22 18:55:20Z
       
    32     '%Y-%m-%d %H:%M:%S',        # 2000-08-22 18:55:20
       
    33     '%d %b %Y %H:%M:%S',        # 08 Apr 2013 05:44:00
       
    34     '%d/%m/%Y %H:%M:%S',     # 23/04/2015 12:00:07 EEST
       
    35     '%d/%m/%Y %H:%M:%S %Z',     # 23/04/2015 12:00:07 EEST
       
    36     '%d/%m/%Y %H:%M:%S.%f %Z',  # 23/04/2015 12:00:07.619546 EEST
       
    37 ]
       
    38 
    12 
    39 
    13 class PywhoisError(Exception):
    40 class PywhoisError(Exception):
    14     pass
    41     pass
    15 
    42 
    16 
    43 
    17 def cast_date(s):
    44 def datetime_parse(s):
    18     """Convert any date string found in WHOIS to a datetime object.
    45     for known_format in KNOWN_FORMATS:
    19     """
       
    20     known_formats = [
       
    21         '%d-%b-%Y', 				# 02-jan-2000
       
    22         '%Y-%m-%d', 				# 2000-01-02
       
    23         '%d.%m.%Y', 				# 2.1.2000
       
    24         '%Y.%m.%d',                 # 2000.01.02
       
    25         '%Y/%m/%d',                 # 2000/01/02
       
    26         '%d/%m/%Y',                 # 02/01/2013
       
    27         '%Y. %m. %d.',              # 2000. 01. 02.
       
    28         '%Y.%m.%d %H:%M:%S',        # 2014.03.08 10:28:24
       
    29         '%d-%b-%Y %H:%M:%S %Z',		# 24-Jul-2009 13:20:03 UTC
       
    30         '%a %b %d %H:%M:%S %Z %Y',  # Tue Jun 21 23:59:59 GMT 2011
       
    31         '%Y-%m-%dT%H:%M:%SZ',       # 2007-01-26T19:10:31Z
       
    32         '%Y-%m-%dT%H:%M:%S%z',      # 2013-12-06T08:17:22-0800
       
    33         '%Y-%m-%d %H:%M:%SZ',       # 2000-08-22 18:55:20Z
       
    34         '%Y-%m-%d %H:%M:%S',        # 2000-08-22 18:55:20
       
    35         '%d %b %Y %H:%M:%S',        # 08 Apr 2013 05:44:00
       
    36         '%d/%m/%Y %H:%M:%S %Z',     # 23/04/2015 12:00:07 EEST
       
    37         '%d/%m/%Y %H:%M:%S.%f %Z',  # 23/04/2015 12:00:07.619546 EEST
       
    38     ]
       
    39 
       
    40     for known_format in known_formats:
       
    41         try:
    46         try:
    42             s = datetime.strptime(s.strip(), known_format)
    47             s = datetime.strptime(s.strip(), known_format)
    43             break
    48             break
    44         except ValueError as e:
    49         except ValueError as e:
    45             pass # Wrong format, keep trying
    50             pass  # Wrong format, keep trying
    46     return s
    51     return s
       
    52 
       
    53 
       
    54 def cast_date(s):
       
    55     """Convert any date string found in WHOIS to a datetime object.
       
    56     """
       
    57     if DATEUTIL:
       
    58         try:
       
    59             return dp.parse(s.strip(), tzinfos=tz_data)
       
    60         except Exception:
       
    61             return datetime_parse(s)
       
    62     else:
       
    63         return datetime_parse(s)
    47 
    64 
    48 
    65 
    49 class WhoisEntry(object):
    66 class WhoisEntry(object):
    50     """Base class for parsing a Whois entries.
    67     """Base class for parsing a Whois entries.
    51     """
    68     """