Skip to content

Commit

Permalink
retry with different url params when --bypass-cdn-image-compression
Browse files Browse the repository at this point in the history
… is enabled
  • Loading branch information
yzqzss committed Apr 8, 2024
1 parent 14fa468 commit 942477f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions wikiteam3/dumpgenerator/dump/image/image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import os
import random
import re
import shutil
import sys
Expand Down Expand Up @@ -87,7 +86,7 @@ def modify_params(params: Optional[Dict] = None) -> Dict:
# bypass Cloudflare Polish (image optimization)
# <https://developers.cloudflare.com/images/polish/>
params["_wiki_t"] = int(time.time()*1000)
params[f"_wiki_{random.randint(10,99)}_"] = "random"
params["_wikiteam3_nocdn"] = "init_req" # this value will be changed on hard retry

return params

Expand Down
16 changes: 13 additions & 3 deletions wikiteam3/utils/monkey_patch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import ssl
import sys
import time
from typing import Optional
import warnings
Expand Down Expand Up @@ -89,7 +88,7 @@ class SessionMonkeyPatch:
Monkey patch `requests.Session.send`
"""
hijacked = False
def __init__(self,*, session: requests.Session, config: Config,
def __init__(self,*, session: requests.Session, config: Optional[Config]=None,
add_delay: bool=False, delay_msg: Optional[str]=None,
hard_retries: int=0,
free_timeout_connections: bool=True, vaild_lft_sec: int=60 * 3
Expand Down Expand Up @@ -127,7 +126,7 @@ def hijack(self):
# Monkey patch `requests.Session.send`
self.old_send_method = self.session.send

def new_send(request, **kwargs):
def new_send(request: requests.PreparedRequest, **kwargs):
hard_retries_left = self.hard_retries + 1
if hard_retries_left <= 0:
raise ValueError('hard_retries must be positive')
Expand All @@ -149,6 +148,17 @@ def new_send(request, **kwargs):
raise

print('Hard retry... (%d), due to: %s' % (hard_retries_left, e))

# if --bypass-cdn-image-compression is enabled, retry with different url
assert isinstance(request.url, str)
if '_wikiteam3_nocdn=' in request.url:
request.url = request.url.replace('_wikiteam3_nocdn=init_req', f'_wikiteam3_nocdn=retry_{hard_retries_left}')
request.url = request.url.replace(
f'_wikiteam3_nocdn=retry_{hard_retries_left + 1}',
f'_wikiteam3_nocdn=retry_{hard_retries_left}'
)
print('--bypass-cdn-image-compression: change url to', request.url, 'on hard retry...')

time.sleep(3)

self.session.send = new_send # type: ignore
Expand Down

0 comments on commit 942477f

Please sign in to comment.