From 53a20f853b11ceb61c96c4fcf1c130516cb536de Mon Sep 17 00:00:00 2001 From: girst Date: Thu, 20 May 2021 17:30:09 +0200 Subject: [PATCH] cache POST requests, explicity not cache some POSTs --- app/common/anticaptcha.py | 12 ++++++++---- app/common/common.py | 2 +- app/common/utils.py | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/common/anticaptcha.py b/app/common/anticaptcha.py index 52cbf8c..813279c 100644 --- a/app/common/anticaptcha.py +++ b/app/common/anticaptcha.py @@ -87,7 +87,9 @@ def submit_captcha(r): """, (nonce, r.url, captcha.action)) conn.commit() - r2 = requests.post(f"{api_host}/createTask", json={ + import requests_cache + with requests_cache.disabled(): + r2 = requests.post(f"{api_host}/createTask", json={ "clientKey": api_key, "task": { "type": "NoCaptchaTaskProxyless", @@ -96,7 +98,7 @@ def submit_captcha(r): "recaptchaDataSValue": captcha.svalue, }, "callbackUrl": f"{public_uri}/captcha_response/v1/{nonce}", - }) + }) task_id = r2.json().get("taskId") flask_logger(f"submitted captcha task with id {task_id}") @@ -117,11 +119,13 @@ def solve_captcha(nonce, json_obj): solution = json_obj.get("solution", {}) - r = requests.post( + import requests_cache + with requests_cache.disabled(): + r = requests.post( urljoin(url, action), data={"g-recaptcha-response": solution.get("gRecaptchaResponse")}, allow_redirects=False - ) + ) cookie_name = "goojf" cookie_value = r.cookies.get(cookie_name) diff --git a/app/common/common.py b/app/common/common.py index 05a9a2e..74ea4db 100644 --- a/app/common/common.py +++ b/app/common/common.py @@ -19,7 +19,7 @@ if not 'global' in cf: # todo: full config check raise Exception("Configuration file not found or empty") # Note: currently expiring after 10 minutes. googlevideo-urls are valid for 5h59m, but this makes reddit very stale and premiere videos won't start. TODO: exipre when video is livestream/premiere/etc -requests_cache.install_cache(backend='memory', expire_after=10*60, allowable_codes=(200,)) +requests_cache.install_cache(backend='memory', expire_after=10*60, allowable_codes=(200,), allowable_methods=('GET', 'HEAD', 'POST')) # Note: requests-cache doesn't use redis expiry, so we need this in all backends: # https://github.com/reclosedev/requests-cache/issues/58#issuecomment-164537971 diff --git a/app/common/utils.py b/app/common/utils.py index d5454d0..e86e070 100755 --- a/app/common/utils.py +++ b/app/common/utils.py @@ -117,7 +117,9 @@ def update_feed(feed_id, feed_type, verbose): version, timestamp = "v1", int(time.time()) nonce = secrets.token_urlsafe(16) sig = websub_url_hmac(hmackey, feed_id, timestamp, nonce) - r = requests.post("https://pubsubhubbub.appspot.com/subscribe", { + import requests_cache + with requests_cache.disabled(): + r = requests.post("https://pubsubhubbub.appspot.com/subscribe", { "hub.callback": f"{webhook}/websub/{version}/{timestamp}/" + \ f"{nonce}/{feed_id}/{sig}", "hub.topic": f"https://www.youtube.com/xml/feeds/videos.xml" + \ @@ -126,7 +128,7 @@ def update_feed(feed_id, feed_type, verbose): "hub.mode": "subscribe", "hub.lease_seconds": lease, "hub.secret": hmackey, - }) + }) if not r.ok: if verbose: sys.stderr.write(f'FAILED {feed_id}: {r.text}\n') -- 2.39.3