File: C:/Users/fred/.virtual_documents/Desktop/tx0525.ipynb
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 = "+19788003538"
YOUR_PHONE_NUMBER = "+886906867137"
def send_sms(message):
client.messages.create(
body=message,
from_=TWILIO_PHONE_NUMBER,
to=YOUR_PHONE_NUMBER
)
# 🔹 拓元售票網址(請換成您的票務頁面) 星期天 19138 星期六 19137
URL = "https://tixcraft.com/ticket/area/25_jhope/19138"
# 🔹 目標座位名稱
#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(f"\n🔁 {now.strftime('%Y-%m-%d %H:%M:%S')} - 開始檢查")
check_tickets() # 你原本的函式
# 若超過一小時,印出提示
if now - last_hour_check >= timedelta(hours=1):
t1=datetime.now()
print(f"\n ⏰ {t1.strftime('%Y-%m-%d %H:%M:%S')} 一小時內無票")
last_hour_check = now # 更新時間
time.sleep(3)
run_until("2025-05-25 16:00")
# Twilio 帳戶資訊
TWILIO_ACCOUNT_SID = "AC8a2a780795d52434523071de0c5b6626"
TWILIO_AUTH_TOKEN = "b4a95a298841e743b921f4f7ce6d01c5"
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
TWILIO_PHONE_NUMBER = "+12403267359"
YOUR_PHONE_NUMBER = "+886906867137"
send_sms('test')