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
|
class Test(object):
def retry(times=3):
"""
times == -1 forever
:return:
"""
def deco(func):
def new_handler(*args, **kwargs):
retry_time = 1
if times == -1:
while True:
try:
is_ok = func(*args, **kwargs)
if is_ok:
break
print(f'retry[{retry_time}]:{Duration.get_time(args[2]).to_str()}')
retry_time +=1
except Exception:
pass
elif times > 0:
for i in range(times):
try:
is_ok = func(*args, **kwargs)
if is_ok:
break
print(f'retry[{retry_time}]:{Duration.get_time(args[2]).to_str()}')
retry_time += 1
except Exception:
pass
return new_handler
return deco
@retry(times=-1)
def download_img(self, station_num: int, time: float, rewrite=False):
path = f'afreecatv/{station_num}/{Duration.get_time(time).to_str()}.jpg'
if not rewrite and os.path.exists(path):
return True
param = self.get_thumbnail_param(station_num, time)
is_ok = util.down_img(self.THUMBNAIL_URL, param, path)
if not is_ok:
print(path)
return is_ok
|
近期评论