Spaces:
Running
Running
File size: 2,065 Bytes
30ddf21 | 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 | import requests
import json
import urllib3
# Ignore SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def fetch_and_save_notices():
print("Fetching live data from the Alert API...")
url = "https://alert.ktunotes.live/api/notifications"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Accept": "application/json"
}
try:
# 1. Download the clean JSON data
response = requests.get(url, headers=headers, verify=False, timeout=15)
if response.status_code == 200:
api_data = response.json()
if api_data.get("success"):
raw_notifications = api_data.get("notifications", [])
formatted_notices = []
# 2. Translate their data into YOUR website's format
for notice in raw_notifications:
formatted_notices.append({
"subject": notice.get("title", "KTU Update"),
"announcementDate": notice.get("date", "Recent"),
"urlHref": notice.get("link", "https://ktu.edu.in/Menu/announcements"),
"attachmentList": [{"title": "Read More"}] # This triggers the button on your site
})
# 3. Save the formatted data to your notices.json file
with open("notices.json", "w", encoding="utf-8") as f:
json.dump({"content": formatted_notices}, f, indent=4)
print(f"VICTORY! Successfully translated and saved {len(formatted_notices)} notices.")
else:
print("Connected to API, but it returned success: false")
else:
print(f"Failed. Server returned HTTP {response.status_code}")
except Exception as e:
print(f"Fatal error fetching data: {e}")
if __name__ == "__main__":
fetch_and_save_notices()
|