write_rss_feed_7.py 1015 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. def write_index(bucket, feed_data):
  2. try:
  3. index = s3.get_object(
  4. Bucket=bucket,
  5. Key='feeds.json',)
  6. feed_index = json.load(index['Body'])
  7. except ClientError as e:
  8. error_code = e.response['Error']['Code']
  9. if error_code == 'NoSuchKey':
  10. feed_index = {}
  11. else:
  12. raise e
  13. feed_path = feed_data['encoded_path']
  14. feed_index[feed_path] = feed_data
  15. s3.put_object(
  16. Bucket=bucket,
  17. Key='feeds.json',
  18. Body=json.dumps(feed_index, indent=4),
  19. ContentType='application/json'
  20. )
  21. index_template = """
  22. <html>
  23. <body>
  24. {}
  25. </body>
  26. </html>
  27. """
  28. feed_links = [
  29. '<li><a href="{0[url]}">{0[title]}</a></li>'.format(feed)
  30. for feed in feed_index.values()
  31. ]
  32. html = index_template.format('<br>\n'.join(feed_links))
  33. s3.put_object(
  34. Bucket=bucket,
  35. Key='index.html',
  36. Body=html,
  37. ContentType='text/html'
  38. )