86 lines
		
	
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """
 | |
| RSS feeds for recent events and by family.
 | |
| """
 | |
| 
 | |
| import falcon
 | |
| import html
 | |
| from datetime import datetime
 | |
| from oedb.utils.logging import logger
 | |
| from oedb.utils.db import db_connect
 | |
| 
 | |
| 
 | |
| def _rss_header(title: str, link: str, desc: str) -> str:
 | |
|     return f"""<?xml version="1.0" encoding="UTF-8"?>
 | |
| <rss version="2.0">
 | |
| <channel>
 | |
|   <title>{html.escape(title)}</title>
 | |
|   <link>{html.escape(link)}</link>
 | |
|   <description>{html.escape(desc)}</description>
 | |
| """
 | |
| 
 | |
| 
 | |
| def _rss_footer() -> str:
 | |
|     return """
 | |
| </channel>
 | |
| </rss>
 | |
| """
 | |
| 
 | |
| 
 | |
| def _row_to_item(row) -> str:
 | |
|     events_id, events_tags, createdate = row
 | |
|     title = html.escape(str(events_tags.get('label') or events_tags.get('what') or f"event {events_id}"))
 | |
|     link = f"https://api.openeventdatabase.org/event/{events_id}"
 | |
|     guid = link
 | |
|     pubdate = datetime.fromisoformat(str(createdate)).strftime('%a, %d %b %Y %H:%M:%S %z') if createdate else ''
 | |
|     description = html.escape(str(events_tags))
 | |
|     return f"""
 | |
|   <item>
 | |
|     <title>{title}</title>
 | |
|     <link>{link}</link>
 | |
|     <guid>{guid}</guid>
 | |
|     <pubDate>{pubdate}</pubDate>
 | |
|     <description>{description}</description>
 | |
|   </item>
 | |
| """
 | |
| 
 | |
| 
 | |
| class RSSLatestResource:
 | |
|     def on_get(self, req, resp):
 | |
|         logger.info("Processing GET /rss")
 | |
|         resp.content_type = 'application/rss+xml; charset=utf-8'
 | |
|         db = db_connect()
 | |
|         cur = db.cursor()
 | |
|         cur.execute("""
 | |
|             SELECT events_id, events_tags, createdate
 | |
|             FROM events
 | |
|             ORDER BY createdate DESC
 | |
|             LIMIT 200
 | |
|         """)
 | |
|         items = ''.join(_row_to_item(r) for r in cur.fetchall())
 | |
|         xml = _rss_header('OEDB - Latest events', 'https://api.openeventdatabase.org/event', 'Latest 200 events') + items + _rss_footer()
 | |
|         resp.text = xml
 | |
| 
 | |
| 
 | |
| class RSSByFamilyResource:
 | |
|     def on_get(self, req, resp, family: str):
 | |
|         logger.info(f"Processing GET /rss/by/{family}")
 | |
|         resp.content_type = 'application/rss+xml; charset=utf-8'
 | |
|         db = db_connect()
 | |
|         cur = db.cursor()
 | |
|         like = family + '%'
 | |
|         cur.execute("""
 | |
|             SELECT events_id, events_tags, createdate
 | |
|             FROM events
 | |
|             WHERE events_what LIKE %s
 | |
|             ORDER BY createdate DESC
 | |
|             LIMIT 200
 | |
|         """, (like,))
 | |
|         items = ''.join(_row_to_item(r) for r in cur.fetchall())
 | |
|         xml = _rss_header(f'OEDB - {family}', f'https://api.openeventdatabase.org/event?what={family}', f'Latest 200 events in {family}') + items + _rss_footer()
 | |
|         resp.text = xml
 | |
| 
 | |
| 
 | |
| rss_latest = RSSLatestResource()
 | |
| rss_by_family = RSSByFamilyResource()
 | |
| 
 | |
| 
 | 
