From bf6d897438b017557760c3c544ec45427817f560 Mon Sep 17 00:00:00 2001 From: Rany Date: Sat, 9 Nov 2024 10:42:21 +0200 Subject: [PATCH] Fix Python <3.11 incompatibility bug (#306) datetime.UTC is not supported on Python <3.11. Signed-off-by: rany --- src/edge_tts/drm.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/edge_tts/drm.py b/src/edge_tts/drm.py index 16bf557..cee0f03 100644 --- a/src/edge_tts/drm.py +++ b/src/edge_tts/drm.py @@ -1,6 +1,6 @@ """This module contains functions for generating the Sec-MS-GEC and Sec-MS-GEC-Version tokens.""" -import datetime +from datetime import datetime, timezone import hashlib from .constants import CHROMIUM_FULL_VERSION, TRUSTED_CLIENT_TOKEN @@ -12,9 +12,7 @@ def generate_sec_ms_gec_token() -> str: See: https://github.com/rany2/edge-tts/issues/290#issuecomment-2464956570""" # Get the current time in Windows file time format (100ns intervals since 1601-01-01) - ticks = int( - (datetime.datetime.now(datetime.UTC).timestamp() + 11644473600) * 10000000 - ) + ticks = int((datetime.now(timezone.utc).timestamp() + 11644473600) * 10000000) # Round down to the nearest 5 minutes (3,000,000,000 * 100ns = 5 minutes) ticks -= ticks % 3_000_000_000 @@ -22,12 +20,8 @@ def generate_sec_ms_gec_token() -> str: # Create the string to hash by concatenating the ticks and the trusted client token str_to_hash = f"{ticks}{TRUSTED_CLIENT_TOKEN}" - # Compute the SHA256 hash - hash_object = hashlib.sha256(str_to_hash.encode("ascii")) - hex_dig = hash_object.hexdigest() - - # Return the hexadecimal representation of the hash - return hex_dig.upper() + # Compute the SHA256 hash and return the uppercased hex digest + return hashlib.sha256(str_to_hash.encode("ascii")).hexdigest().upper() def generate_sec_ms_gec_version() -> str: