#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os from datetime import datetime from flask import Flask, request, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app) DATA_FILE = os.path.expanduser('~/feeding_data.json') def load_data(): if not os.path.exists(DATA_FILE): return [] with open(DATA_FILE, 'r') as f: return json.load(f) def save_data(data): with open(DATA_FILE, 'w') as f: json.dump(data, f, indent=2, ensure_ascii=False) @app.route('/api/feeding/today', methods=['GET', 'OPTIONS']) def today(): if request.method == 'OPTIONS': return '', 200 data = load_data() today_str = datetime.now().strftime('%Y-%m-%d') today_records = [r for r in data if r.get('date') == today_str] total = sum(r.get('amount', 0) for r in today_records) return jsonify({'date': today_str, 'records': today_records, 'total': total}) @app.route('/api/feeding/add', methods=['POST', 'OPTIONS']) def add(): if request.method == 'OPTIONS': return '', 200 try: body = request.get_json() if not body: return jsonify({'error': '请求体为空'}), 400 date = body.get('date') time = body.get('time') amount = body.get('amount') if not date or not time or amount is None: return jsonify({'error': '缺少必要参数 (date, time, amount)'}), 400 data = load_data() new_id = 1 if data: new_id = max(r.get('id', 0) for r in data) + 1 new_record = { 'id': new_id, 'date': date, 'time': time, 'amount': int(amount), 'created_at': datetime.now().isoformat() } data.append(new_record) save_data(data) return jsonify(new_record), 201 except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/api/feeding/delete', methods=['DELETE', 'OPTIONS']) def delete(): if request.method == 'OPTIONS': return '', 200 record_id = request.args.get('id') if not record_id: return jsonify({'error': '需要 id 参数'}), 400 try: record_id = int(record_id) except: return jsonify({'error': 'id 必须是整数'}), 400 data = load_data() new_data = [r for r in data if r.get('id') != record_id] if len(new_data) == len(data): return jsonify({'error': f'记录不存在 (id={record_id})'}), 404 save_data(new_data) return jsonify({'success': True, 'id': record_id}) @app.route('/api/feeding/update', methods=['PUT', 'OPTIONS']) def update(): if request.method == 'OPTIONS': return '', 200 body = request.json if not body: return jsonify({'error': '请求体为空'}), 400 record_id = body.get('id') if not record_id: return jsonify({'error': '需要 id 参数'}), 400 data = load_data() for i, record in enumerate(data): if record.get('id') == record_id: if 'date' in body: data[i]['date'] = body['date'] if 'time' in body: data[i]['time'] = body['time'] if 'amount' in body: data[i]['amount'] = int(body['amount']) data[i]['updated_at'] = datetime.now().isoformat() save_data(data) return jsonify(data[i]) return jsonify({'error': f'记录不存在 (id={record_id})'}), 404 @app.route('/api/feeding/day', methods=['GET', 'OPTIONS']) def day(): if request.method == 'OPTIONS': return '', 200 date = request.args.get('date') if not date: return jsonify({'error': '需要 date 参数'}), 400 data = load_data() day_records = [r for r in data if r.get('date') == date] total = sum(r.get('amount', 0) for r in day_records) return jsonify({'date': date, 'records': day_records, 'total': total}) if __name__ == '__main__': port = 5002 print(f'Feeding API started on port {port}') app.run(host='0.0.0.0', port=port, debug=False)