#!/usr/bin/env python3
import subprocess
import json
from datetime import datetime, timedelta

start_date = datetime(2026, 3, 15)
end_date = datetime(2026, 3, 31)

results = []

for i in range((end_date - start_date).days + 1):
    date = start_date + timedelta(days=i)
    date_str = date.strftime('%Y-%m-%d')
    
    cmd = f'python3 flyclaw.py search --from 北京 --to AKL --date {date_str} --stops any --sort cheapest -l 1 2>/dev/null'
    
    try:
        output = subprocess.check_output(cmd, shell=True, text=True, timeout=30)
        lines = output.strip().split('\n')
        for line in lines:
            if 'Price:' in line:
                parts = line.split()
                flight = ''
                price = 0
                for idx, p in enumerate(parts):
                    if p.endswith(')') and not flight:
                        flight = parts[idx-1] if idx > 0 else ''
                    if p == 'Price:':
                        if idx + 1 < len(parts):
                            try:
                                price_str = parts[idx+1].replace('$', '').replace(',', '')
                                price = float(price_str)
                            except:
                                pass
                if flight and price > 0:
                    results.append({
                        'date': date_str,
                        'flight': flight,
                        'price': price
                    })
                    break
    except Exception as e:
        print(f"Error on {date_str}: {e}", file=__import__('sys').stderr)

# 按价格排序
results.sort(key=lambda x: x['price'])

print(f"共找到 {len(results)} 个日期的最便宜航班")
print()
print("日期|航班|价格(USD)|约合人民币")
print("---|---|---|---")
for r in results[:30]:
    cny = round(r['price'] * 7.2)
    print(f"{r['date']}|{r['flight']}|${r['price']}|¥{cny}")
