Skip to content

Commit 3c11feb

Browse files
committed
Dynamic pause
1 parent 216db4c commit 3c11feb

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

whoare/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package_name = 'whoare'
2-
__version__ = '0.2.5'
2+
__version__ = '0.2.6'
33

44

55
def require_update_pypi():

whoare/serve.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Iterate over priority domains and send data to server
33
"""
44
import argparse
5+
from collections import deque
56
from datetime import datetime
67
import logging
78
from time import sleep
@@ -14,13 +15,14 @@
1415

1516

1617
class WhoAreShare:
17-
def __init__(self, get_domains_url, post_url, token, torify=True, pause_between_calls=41, from_path=None):
18+
def __init__(self, get_domains_url, post_url, token, torify=True, pause_between_calls=41, from_path=None, dynamic_pause=False):
1819
self.torify = torify # use local IP and also torify
1920
self.post_url = post_url # destination URL to share data (will be processed outside)
2021
self.token = token
2122
self.get_domains_url = get_domains_url # URL to get domains from
2223
self.pause_between_calls = pause_between_calls
2324
self.from_path = from_path
25+
self.dynamic_pause = dynamic_pause
2426

2527
self.total_analizados = 0
2628
self.sin_cambios = 0
@@ -30,6 +32,9 @@ def __init__(self, get_domains_url, post_url, token, torify=True, pause_between_
3032
self.otros_cambios = 0
3133
self.errores = 0
3234

35+
# Track last 20 results for dynamic pause
36+
self.last_results = deque(maxlen=20)
37+
3338
self.processed = [] # skip duplicates
3439

3540
def run(self):
@@ -181,9 +186,12 @@ def analyze_changes(self, response):
181186
if cambios == []:
182187
if response.get('created', False):
183188
self.nuevos += 1
189+
self.last_results.append(0)
184190
else:
185191
self.sin_cambios += 1
192+
self.last_results.append(1)
186193
elif 'estado' in [c['campo'] for c in cambios]:
194+
self.last_results.append(0)
187195
for cambio in cambios:
188196
if cambio['campo'] == 'estado':
189197
if cambio['anterior'] == 'disponible':
@@ -192,10 +200,39 @@ def analyze_changes(self, response):
192200
self.caidos += 1
193201
elif 'dominio_expire' in [c['campo'] for c in cambios]:
194202
self.renovados += 1
203+
self.last_results.append(0)
195204
else:
196205
self.otros_cambios += 1
206+
self.last_results.append(0)
207+
208+
self.update_dynamic_pause()
209+
no_chg_prc_all = round(self.sin_cambios / self.total_analizados, 2) * 100 if self.total_analizados > 0 else 0
210+
last_20_chg_prc = round(sum(self.last_results) / len(self.last_results), 2) * 100 if len(self.last_results) > 0 else 0
211+
logger.info(
212+
f'[{self.total_analizados}]{self.errores} RENOV {self.renovados} CAIDOS {self.caidos} '
213+
f'NOCH{self.sin_cambios} ({no_chg_prc_all}% - {last_20_chg_prc}%) NEW{self.nuevos} OTR{self.otros_cambios} '
214+
f'PAUSE {self.pause_between_calls}'
215+
)
216+
217+
def update_dynamic_pause(self):
218+
""" adjust pause based on % of sin_cambios in last 20 results """
219+
if not self.dynamic_pause:
220+
return
221+
if len(self.last_results) == 0:
222+
return
197223

198-
logger.info(f'[{self.total_analizados}]{self.errores} REN{self.renovados} DOWN{self.caidos} NOCH{self.sin_cambios} NEW{self.nuevos} OTR{self.otros_cambios}')
224+
pct = sum(self.last_results) / len(self.last_results) * 100
225+
226+
if pct > 90:
227+
self.pause_between_calls = 45
228+
elif pct > 70:
229+
self.pause_between_calls = 30
230+
elif pct > 50:
231+
self.pause_between_calls = 22
232+
elif pct > 30:
233+
self.pause_between_calls = 16
234+
else:
235+
self.pause_between_calls = 11
199236

200237

201238
def main():
@@ -211,6 +248,7 @@ def main():
211248
parser.add_argument('--token', nargs='?', help='Token to use as Header Autorization', type=str, required=True)
212249
parser.add_argument('--torify', nargs='?', type=bool, default=False, help='Use torify for WhoIs command')
213250
parser.add_argument('--pause', nargs='?', help='Pause between calls', default=41, type=int)
251+
parser.add_argument('--dynamic_pause', action='store_true', help='Enable dynamic pause based on recent changes', default=False)
214252
parser.add_argument('--from_path', nargs='?', help='If not used we will get priorities from API. This is usted for new-domain lists', type=str)
215253
parser.add_argument('--one_domain', nargs='?', help='Just update one domain', type=str)
216254
parser.add_argument('--log_level', nargs='?', default='INFO', type=str)
@@ -236,7 +274,8 @@ def main():
236274
token=args.token,
237275
torify=args.torify,
238276
pause_between_calls=args.pause,
239-
from_path=args.from_path
277+
from_path=args.from_path,
278+
dynamic_pause=args.dynamic_pause
240279
)
241280

242281
if args.one_domain is not None:

0 commit comments

Comments
 (0)