-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_version_json.py
More file actions
executable file
·163 lines (135 loc) · 4.84 KB
/
Copy pathfetch_version_json.py
File metadata and controls
executable file
·163 lines (135 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
"""
Version JSON Fetcher
This script fetches and displays the content from a version.json URL.
It can handle both JSON and plain text responses with proper formatting.
Usage:
python fetch_version_json.py
python fetch_version_json.py --url https://example.com/version.json
python fetch_version_json.py --url https://cloud.stackgen.com/version.json --pretty
"""
import argparse
import json
import sys
import urllib.request
import urllib.error
import ssl
from typing import Optional
def fetch_url_content(url: str, timeout: int = 30) -> tuple[bool, str, Optional[dict]]:
"""
Fetch content from a URL.
Args:
url: URL to fetch content from
timeout: Request timeout in seconds
Returns:
Tuple of (success, content, parsed_json)
"""
try:
# Create request with proper headers
request = urllib.request.Request(
url,
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
)
# Create SSL context that doesn't verify certificates
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
# Fetch the content
with urllib.request.urlopen(request, timeout=timeout, context=ssl_context) as response:
content = response.read().decode('utf-8')
# Try to parse as JSON
try:
parsed_json = json.loads(content)
return True, content, parsed_json
except json.JSONDecodeError:
# Not valid JSON, return as plain text
return True, content, None
except urllib.error.HTTPError as e:
return False, f"HTTP Error {e.code}: {e.reason}", None
except urllib.error.URLError as e:
return False, f"URL Error: {e.reason}", None
except Exception as e:
return False, f"Error: {str(e)}", None
def print_content(content: str, parsed_json: Optional[dict] = None, pretty: bool = False) -> None:
"""
Print content to console with optional JSON formatting.
Args:
content: Raw content string
parsed_json: Parsed JSON object (if available)
pretty: Whether to use pretty formatting
"""
if parsed_json is not None:
print("JSON Content:")
print("=" * 50)
if pretty:
print(json.dumps(parsed_json, indent=2, ensure_ascii=False))
else:
print(json.dumps(parsed_json, ensure_ascii=False))
else:
print("Text Content:")
print("=" * 50)
print(content)
def main():
"""Main function to handle command-line arguments and fetch content."""
parser = argparse.ArgumentParser(
description="Fetch and display content from a version.json URL",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Fetch from default URL
python fetch_version_json.py
# Fetch from custom URL
python fetch_version_json.py --url https://example.com/version.json
# Pretty print JSON
python fetch_version_json.py --pretty
# Custom timeout
python fetch_version_json.py --timeout 60
"""
)
parser.add_argument(
"--url", "-u",
default="https://cloud.stackgen.com/version.json",
help="URL to fetch content from (default: production cloud.stackgen.com)",
)
parser.add_argument(
"--pretty", "-p",
action="store_true",
help="Pretty print JSON content with indentation"
)
parser.add_argument(
"--timeout", "-t",
type=int,
default=30,
help="Request timeout in seconds (default: 30)"
)
parser.add_argument(
"--raw",
action="store_true",
help="Always display raw content, don't try to parse as JSON"
)
args = parser.parse_args()
print(f"Fetching content from: {args.url}")
print(f"Timeout: {args.timeout} seconds")
print("-" * 60)
# Fetch content
success, content, parsed_json = fetch_url_content(args.url, args.timeout)
if not success:
print(f"❌ Failed to fetch content: {content}", file=sys.stderr)
sys.exit(1)
# Display content
if args.raw:
print_content(content, None, args.pretty)
else:
print_content(content, parsed_json, args.pretty)
# Additional info
print("\n" + "-" * 60)
print(f"Content length: {len(content)} characters")
if parsed_json is not None:
print(f"Content type: JSON")
print(f"JSON keys: {list(parsed_json.keys()) if isinstance(parsed_json, dict) else 'Not a dictionary'}")
else:
print(f"Content type: Plain text")
if __name__ == "__main__":
main()