forked from shmilylty/OneForAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain.py
61 lines (50 loc) · 1.48 KB
/
domain.py
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
import re
from common import tldextract
from config import settings
class Domain(object):
"""
Processing domain class
:param str string: input string
"""
def __init__(self, string):
self.string = str(string)
self.regexp = r'\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b'
self.domain = None
def match(self):
"""
match domain
:return : result
"""
result = re.search(self.regexp, self.string, re.I)
if result:
return result.group()
return None
def extract(self):
"""
extract domain
>>> d = Domain('www.example.com')
<domain.Domain object>
>>> d.extract()
ExtractResult(subdomain='www', domain='example', suffix='com')
:return: extracted domain results
"""
data_storage_dir = settings.data_storage_dir
extract_cache_file = data_storage_dir.joinpath('public_suffix_list.dat')
ext = tldextract.TLDExtract(extract_cache_file)
result = self.match()
if result:
return ext(result)
return None
def registered(self):
"""
registered domain
>>> d = Domain('www.example.com')
<domain.Domain object>
>>> d.registered()
example.com
:return: registered domain result
"""
result = self.extract()
if result:
return result.registered_domain
return None