HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/OneDrive/Desktop/tx0525.py
ˇpip2 install requests
import requests
from bs4 import BeautifulSoup
import time
import smtplib
from email.mime.text import MIMEText
from twilio.rest import Client
from plyer import notification  # 桌面通知
import re
from datetime import datetime
from datetime import datetime, timedelta

# Twilio 帳戶資訊
TWILIO_ACCOUNT_SID = "AC1171c857c61a9f2b20deb4c6e875642f"
TWILIO_AUTH_TOKEN = "561ccf35d5801b13e118de6be8f2418a"
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
TWILIO_PHONE_NUMBER = "+15074073576"
YOUR_PHONE_NUMBER = "+886906867137"


def send_sms(message):    
    client.messages.create(
        body=message,
        from_=TWILIO_PHONE_NUMBER,
        to=YOUR_PHONE_NUMBER
    )

# 🔹 拓元售票網址(請換成您的票務頁面) 星期天 19138  星期六 19137
URL = "httpstixcraft.comticketarea25_jhope19138"

# 🔹 目標座位名稱
#TARGET_SEAT = '橙1A-2區2800'  # 依照您的需求修改
TARGET_SEATS = ['橙1A-2區2800', '黃1A-1區2800','3800','橙1A-1區2800','黃1A-2區2800']

##################################################################這個

def check_tickets():
    headers = {'User-Agent':'Mozilla/5.0'}
    response = requests.get(URL, headers=headers)


    soup = BeautifulSoup(response.text, 'html.parser')

    seats = soup.find_all(['li', 'a'])
    # ✅ 網站打開通知(只寄一次)
    found = False

    for seat in seats:
        text = ''.join(seat.stripped_strings)

        for target_seat in TARGET_SEATS:
            if target_seat in text:
                found = True
                if '剩餘' in text or 'remaining' in text:
                    area = text[:10]
                    match = text[:10]
                    match = re.search(r'(\d+)s*seat\(s\) remaining', match)
                    if match:
                        remaining_seats = match.group(1)
                        result = f'🎉 星期天有票了!{area} 剩餘 {remaining_seats} 張'
                        print(result)
                        send_sms(result)
                        #notify_desktop(result)
                else:
                    
                    print(f'❌ 星期天尚無票:{text}')
                    continue

    if not found:
        print('📭 網站未開放')

def run_until(deadline_str):
    deadline = datetime.strptime(deadline_str, '%Y-%m-%d %H%M')

    last_hour_check = datetime.now()  # 用來追蹤上一個「整點提醒」
    notified_opened = False  # 可選:是否發送過「網站打開了!」

    while True:
        now = datetime.now()

        if now >= deadline:
            print('到達指定時間,停止執行')
            break

        #print(fn🔁 {now.strftime('%Y-%m-%d %H%M%S')} - 開始檢查)
        check_tickets()  # 你原本的函式

        # 若超過一小時,印出提示
        if now - last_hour_check >= timedelta(hours=1):
            t1=datetime.now()
            print(t1.strftime('%Y-%m-%d %H:%M:%S'))
            print('一小時內無票\n')
            last_hour_check = now  # 更新時間

        time.sleep(10)
run_until('2025-05-23 14:37')