fix: escape control characters in JSON output to make it jq compatible
This commit is contained in:
@@ -75,7 +75,29 @@ def sanitize_json_response(text):
|
||||
except json.JSONDecodeError:
|
||||
# If parsing still fails, try one more aggressive approach
|
||||
# Remove any remaining control characters that might be causing issues
|
||||
array_content = re.sub(r"[\x00-\x08\x0b\x0c\x0e-\x1f]", "", array_content)
|
||||
# This is the key fix - we need to escape control characters rather than remove them
|
||||
def escape_control_chars(match):
|
||||
char = match.group(0)
|
||||
# Handle specific control characters that might cause issues
|
||||
if char == "\n":
|
||||
return "\\n"
|
||||
elif char == "\r":
|
||||
return "\\r"
|
||||
elif char == "\t":
|
||||
return "\\t"
|
||||
else:
|
||||
# For other control characters, use Unicode escape
|
||||
return f"\\u{ord(char):04x}"
|
||||
|
||||
# More aggressive approach: escape ALL control characters
|
||||
# This handles the case where line breaks occur within JSON string values
|
||||
# We need to escape them before they break the JSON structure
|
||||
|
||||
# First, let's try to detect if there are line breaks within quoted strings
|
||||
# and escape them properly
|
||||
|
||||
# Simple approach: escape all control characters
|
||||
array_content = re.sub(r"[\x00-\x1f]", escape_control_chars, array_content)
|
||||
|
||||
try:
|
||||
data = json.loads(array_content)
|
||||
@@ -387,7 +409,9 @@ def schedule(
|
||||
with outfile.open("w") as f:
|
||||
f.write(json.dumps(schedule))
|
||||
else:
|
||||
print(json.dumps(schedule, indent=2))
|
||||
import builtins
|
||||
|
||||
builtins.print(json.dumps(schedule, indent=2))
|
||||
|
||||
|
||||
def os_open(file: str) -> None:
|
||||
@@ -605,7 +629,13 @@ def refresh_data():
|
||||
# verify=False,
|
||||
) as r:
|
||||
r.raise_for_status()
|
||||
return json.loads(sanitize_json_response(r.text))
|
||||
# Since the API returns valid JSON, we don't need to sanitize it
|
||||
# Just parse it directly
|
||||
try:
|
||||
return r.json()
|
||||
except json.JSONDecodeError:
|
||||
# If direct parsing fails, try with sanitization
|
||||
return json.loads(sanitize_json_response(r.text))
|
||||
|
||||
|
||||
@app.command("mobile-login")
|
||||
@@ -620,7 +650,10 @@ def mobile():
|
||||
global userdata, global_config_section
|
||||
userdata = mobile_login(config_section=global_config_section)
|
||||
games = [x for x in refresh_data().get("club_games")]
|
||||
print(json.dumps(games, indent=2))
|
||||
# Use built-in print to avoid rich formatting issues
|
||||
import builtins
|
||||
|
||||
builtins.print(json.dumps(games, indent=2, ensure_ascii=False))
|
||||
|
||||
|
||||
@app.command("mobile-game")
|
||||
|
||||
Reference in New Issue
Block a user