#!/usr/bin/python2.7
import urllib2
import json
# Configuration variables
JSON_FEED_URL = "https://news.csun.edu/wp-json/csunfeeds/v1/news-feed/college-of-education"
NUM_ITEMS = 3 # Number of news items to display
def fetch_news():
try:
response = urllib2.urlopen(JSON_FEED_URL)
data = json.load(response)
return data
except Exception as e:
return []
def generate_html(news_items):
html_output = """
CSUN College of Education News
Latest News
"""
for item in news_items[:NUM_ITEMS]:
title = item.get("title", "No Title")
date = item.get("published_date", "").split("T")[0] # Extract just the date
formatted_date = "{}/{}/{}".format(date[5:7], date[8:10], date[2:4]) if date else "Unknown Date"
excerpt = item.get("excerpt", "No summary available.")
image_url = item.get("featured_image", "")
html_output += """
{title}
Published: {date}
{excerpt}
""".format(title=title, date=formatted_date, excerpt=excerpt, image_url=image_url)
html_output += """
"""
return html_output
def main():
news_data = fetch_news()
if not news_data:
print "Content-type: text/html\n"
print "Error fetching news.
"
return
html_content = generate_html(news_data)
print "Content-type: text/html\n"
print html_content
if __name__ == "__main__":
main()