import requests

url = 'https://data.tycg.gov.tw/api/v1/rest/datastore/a1b4714b-3b75-4ff8-a8f2-cc377e4eaa0f?format=json'

resp = requests.get(url)

if resp.status_code != 200:
    print('無法取得資料')
    exit()

stations = resp.json().get('result', {}).get('records', [])

while True:
    user = input('\n請輸入要搜尋的Youbike站台(離開=>quit): ')

    if user == 'quit':
        break

    matching_stations = [station for station in stations if user in station['sna']]

    if not matching_stations:
        print('查無該站台:')
        continue

    print(f'共找到{len(matching_stations)}站 =>\n')
    for station in matching_stations:
        print(f'站台: {station["sna"]}, 地址: {station["ar"]}')
        print(f'  -可借: {station["sbi"]}')
        print(f'  -可還: {station["bemp"]}')
        print(f'  -總數: {station["tot"]}\n')

    break