65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Transport Crawler API - Flask service for triggering crawls
|
|
"""
|
|
from flask import Flask, jsonify
|
|
from crawler_service import run_crawler
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/health', methods=['GET'])
|
|
def health():
|
|
"""Health check endpoint"""
|
|
return jsonify({'status': 'ok'}), 200
|
|
|
|
|
|
@app.route('/api/transport/crawl', methods=['POST'])
|
|
def trigger_crawl():
|
|
"""Trigger a transport disruption crawl"""
|
|
try:
|
|
result = run_crawler()
|
|
return jsonify(result), 200
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
@app.route('/api/transport/disruptions', methods=['GET'])
|
|
def get_disruptions():
|
|
"""Get current active disruptions from MongoDB"""
|
|
try:
|
|
from pymongo import MongoClient
|
|
|
|
mongo_uri = os.getenv('MONGODB_URI', 'mongodb://admin:changeme@mongodb:27017/')
|
|
client = MongoClient(mongo_uri)
|
|
db = client['munich_news']
|
|
collection = db['transport_alerts']
|
|
|
|
# Get active disruptions
|
|
disruptions = list(collection.find(
|
|
{'is_active': True},
|
|
{'_id': 0}
|
|
))
|
|
|
|
# Convert datetime to ISO format
|
|
for d in disruptions:
|
|
if d.get('start_time'):
|
|
d['start_time'] = d['start_time'].isoformat()
|
|
if d.get('end_time'):
|
|
d['end_time'] = d['end_time'].isoformat()
|
|
if d.get('updated_at'):
|
|
d['updated_at'] = d['updated_at'].isoformat()
|
|
|
|
return jsonify({
|
|
'total': len(disruptions),
|
|
'disruptions': disruptions
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
if __name__ == '__main__':
|
|
port = int(os.getenv('FLASK_PORT', 5002))
|
|
app.run(host='0.0.0.0', port=port, debug=False)
|