-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdomains.py
80 lines (62 loc) · 2.27 KB
/
domains.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from configparser import SafeConfigParser
import requests
import json
import re
from time import sleep
import logging
config = SafeConfigParser()
environment = SafeConfigParser()
config.read('config.ini')
environment.read('environment.ini')
logger = logging.getLogger(__name__)
class NetworkError(RuntimeError):
def __init__(self, arg):
self.arg = arg
def _endswithwhat(string, ends):
for e in ends:
if string.endswith(e):
return e
return False
def hacks():
logger.info('Calculating domain hacks')
words = [l.strip('\n') for l in open('words.txt').readlines()]
tlds = tuple(json.loads(config.get('tld', 'all')))
can_hack = [w for w in words if w.endswith(tlds)]
tld_len = [len(_endswithwhat(e, tlds)) for e in can_hack]
domains = [ can_hack[i][:-tld_len[i]] + '.' + can_hack[i][-tld_len[i]:]
for i in range(len(can_hack)) ]
valid = [d for d in domains if re.match(r'.{2,}\..+', d)]
logger.info('Calculated %s domain hacks', len(valid))
return valid
def bundle_domains(domains):
max_domains = int(config.get('domainr', 'max_domains'))
return [ domains[i:i+max_domains] for i in
range(0, len(domains), max_domains) ]
def status(domains):
logger.info('Requesting Domainr "status" of %s domains', len(domains))
base = "https://domainr.p.mashape.com/v2/"
payload = {
'domain': ','.join(domains)
}
headers = {
'X-Mashape-Key': environment.get('api-keys', 'domainr_mashape_api_key')
}
bad_codes = 0
while True:
r = requests.get(base + 'status', params=payload, headers=headers)
# temporarily allow 504 while Domainr fixes issues
if r.status_code == 504:
logger.warn('Domainr "status" returned 504')
sleep(30)
elif r.status_code != 200:
logger.warn('Domainr "status" returned %s', r.status_code)
bad_codes += 1
if bad_codes == 3:
logger.error('Domainr "status" returned %s', r.status_code)
raise NetworkError(
'Domainr "status" returned ' + str(r.status_code)
)
sleep(30)
else:
logger.info('Domainr "status" returned 200')
return r.json()['status']