# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 # CloudWatch Custom Widget sample: simple RSS reader import base64 import os from urllib.request import urlopen import json import xml.etree.ElementTree as ET from io import StringIO DOCS = """ ## RSS Reader Reads and displays content of an RSS feed. ### Widget parameters Param | Description ---|--- **url** | The URL of the RSS feed **entryTag** | The XML tag of the element in the feed containing each "news" entry **fields** | An array of field names to extract and display from each entry ### Example parameters ``` yaml url: http://status.aws.amazon.com/rss/cloudwatch-us-east-1.rss entryTag: ./channel/item fields: [ title, pubDate, description ] ```""" CSS = """ """ def parse_xml(xml): it = ET.iterparse(StringIO(xml)) for _, el in it: prefix, has_namespace, postfix = el.tag.partition('}') if has_namespace: el.tag = postfix # strip all namespaces root = it.root return root def lambda_handler(event, context): if 'describe' in event: return DOCS url = event['url'] entryTag = event['entryTag'] fields = event['fields'] data = urlopen(url).read() xml = data.decode('utf-8') rss = parse_xml(xml) html = '' for entry in rss.findall(entryTag): html += '