update
This commit is contained in:
@@ -77,6 +77,72 @@ client = MongoClient(Config.MONGODB_URI)
|
||||
db = client[Config.DB_NAME]
|
||||
articles_collection = db['articles']
|
||||
subscribers_collection = db['subscribers']
|
||||
transport_alerts_collection = db['transport_alerts']
|
||||
|
||||
|
||||
def get_today_transport_disruptions():
|
||||
"""
|
||||
Get active S-Bahn disruptions for today
|
||||
Fetches from MongoDB transport_alerts collection
|
||||
|
||||
Returns:
|
||||
list: Active disruptions with details
|
||||
"""
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
# Get active disruptions
|
||||
disruptions = list(transport_alerts_collection.find(
|
||||
{'is_active': True},
|
||||
{'_id': 0}
|
||||
).sort('severity', -1).sort('updated_at', -1))
|
||||
|
||||
# Filter for disruptions happening today
|
||||
today = datetime.utcnow().date()
|
||||
today_disruptions = []
|
||||
|
||||
for d in disruptions:
|
||||
# Check if disruption is active today
|
||||
start_time = d.get('start_time')
|
||||
end_time = d.get('end_time')
|
||||
|
||||
is_today = False
|
||||
if start_time and end_time:
|
||||
start_date = start_time.date() if hasattr(start_time, 'date') else today
|
||||
end_date = end_time.date() if hasattr(end_time, 'date') else today
|
||||
is_today = start_date <= today <= end_date
|
||||
elif start_time:
|
||||
start_date = start_time.date() if hasattr(start_time, 'date') else today
|
||||
is_today = start_date <= today
|
||||
else:
|
||||
is_today = True # No time info, assume it's relevant
|
||||
|
||||
if is_today:
|
||||
# Format times for display
|
||||
if start_time:
|
||||
d['start_time_str'] = start_time.strftime('%H:%M') if hasattr(start_time, 'strftime') else str(start_time)
|
||||
if end_time:
|
||||
d['end_time_str'] = end_time.strftime('%H:%M') if hasattr(end_time, 'strftime') else str(end_time)
|
||||
|
||||
# Format lines as comma-separated string
|
||||
d['lines_str'] = ', '.join(d.get('lines', []))
|
||||
|
||||
# Get severity icon
|
||||
severity_icons = {
|
||||
'high': '🔴',
|
||||
'medium': '🟡',
|
||||
'low': '🟢'
|
||||
}
|
||||
d['severity_icon'] = severity_icons.get(d.get('severity', 'medium'), '🟡')
|
||||
|
||||
today_disruptions.append(d)
|
||||
|
||||
print(f"✓ Found {len(today_disruptions)} transport disruptions for today")
|
||||
return today_disruptions
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error fetching transport disruptions: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def get_latest_articles_by_categories(categories=None, articles_per_category=3, hours=24):
|
||||
@@ -296,6 +362,9 @@ def render_newsletter_html(articles, subscriber_categories=None, tracking_enable
|
||||
from weather_service import get_munich_weather
|
||||
weather = get_munich_weather()
|
||||
|
||||
# Get transport disruptions for today
|
||||
transport_disruptions = get_today_transport_disruptions()
|
||||
|
||||
# Prepare template data
|
||||
now = datetime.now()
|
||||
total_articles = sum(len(section['articles']) for section in category_sections)
|
||||
@@ -308,7 +377,8 @@ def render_newsletter_html(articles, subscriber_categories=None, tracking_enable
|
||||
'preferences_link': f'{Config.WEBSITE_URL}/preferences.html',
|
||||
'website_link': Config.WEBSITE_URL,
|
||||
'tracking_enabled': tracking_enabled,
|
||||
'weather': weather
|
||||
'weather': weather,
|
||||
'transport_disruptions': transport_disruptions
|
||||
}
|
||||
|
||||
# Render HTML
|
||||
|
||||
Reference in New Issue
Block a user