From d92bc06f9058d03a94817aea3b75a4280d616bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 10 May 2025 21:27:22 +0200 Subject: [PATCH] [twitter:ctid] reduce chance of generating the same ID Querying the same endpoint more than once per second could result in a "404 Not Found" error if the same transaction ID is generated by 'random.randrange(256)' yielding the same result. This commit tries to significantly reduce the chance of this happening by incorporating the fractional part of the current timestamp into the random number, making it only possible to generate the same ID for one-sixteenth of a second. --- gallery_dl/transaction_id.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gallery_dl/transaction_id.py b/gallery_dl/transaction_id.py index 25f1775a..89e3d5b5 100644 --- a/gallery_dl/transaction_id.py +++ b/gallery_dl/transaction_id.py @@ -129,7 +129,9 @@ class ClientTransaction(): keyword="obfiowerehiring", rndnum=3): bytes_key = self.key_bytes - now = int(time.time()) - 1682924400 + nowf = time.time() + nowi = int(nowf) + now = nowi - 1682924400 bytes_time = ( (now ) & 0xFF, # noqa: E202 (now >> 8) & 0xFF, # noqa: E222 @@ -141,7 +143,7 @@ class ClientTransaction(): method, path, now, keyword, self.animation_key) bytes_hash = hashlib.sha256(payload.encode()).digest()[:16] - num = random.randrange(256) + num = (random.randrange(16) << 4) + int((nowf - nowi) * 16.0) result = bytes( byte ^ num for byte in itertools.chain(