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/.ipynb_checkpoints/tx0525-checkpoint.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "84820888-e94b-47c7-bbfa-1c8d4290f24e",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 95\u001b[0m\n\u001b[0;32m     92\u001b[0m             last_hour_check \u001b[38;5;241m=\u001b[39m now  \u001b[38;5;66;03m# 更新時間\u001b[39;00m\n\u001b[0;32m     94\u001b[0m         time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m10\u001b[39m)\n\u001b[1;32m---> 95\u001b[0m run_until(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2025-05-23 17:28\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[9], line 94\u001b[0m, in \u001b[0;36mrun_until\u001b[1;34m(deadline_str)\u001b[0m\n\u001b[0;32m     91\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m ⏰ \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mt1\u001b[38;5;241m.\u001b[39mstrftime(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mY-\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mm-\u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mH:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mM:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mS\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m 一小時內無票\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     92\u001b[0m     last_hour_check \u001b[38;5;241m=\u001b[39m now  \u001b[38;5;66;03m# 更新時間\u001b[39;00m\n\u001b[1;32m---> 94\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m10\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import time\n",
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from twilio.rest import Client\n",
    "#from plyer import notification  # 桌面通知\n",
    "import re\n",
    "from datetime import datetime\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "\n",
    "# Twilio 帳戶資訊\n",
    "TWILIO_ACCOUNT_SID = \"ACa2a780795d52435623071de0c5b6626\"\n",
    "TWILIO_AUTH_TOKEN = \"561ccf35d5801b13e118de6be8f2418a\"\n",
    "client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)\n",
    "TWILIO_PHONE_NUMBER = \"+12403267359\"\n",
    "YOUR_PHONE_NUMBER = \"+886906867137\"\n",
    "\n",
    "\n",
    "def send_sms(message):    \n",
    "    client.messages.create(\n",
    "        body=message,\n",
    "        from_=TWILIO_PHONE_NUMBER,\n",
    "        to=YOUR_PHONE_NUMBER\n",
    "    )\n",
    "\n",
    "# 🔹 拓元售票網址(請換成您的票務頁面) 星期天 19138  星期六 19137\n",
    "URL = \"https://tixcraft.com/ticket/area/25_jhope/19138\"\n",
    "\n",
    "# 🔹 目標座位名稱\n",
    "#TARGET_SEAT = '橙1A-2區2800'  # 依照您的需求修改\n",
    "TARGET_SEATS = ['橙1A-2區2800', \"黃1A-1區2800\",'3800','橙1A-1區2800','黃1A-2區2800']\n",
    "\n",
    "##################################################################這個\n",
    "\n",
    "def check_tickets():\n",
    "    headers = {\"User-Agent\": \"Mozilla/5.0\"}\n",
    "    response = requests.get(URL, headers=headers)\n",
    "\n",
    "\n",
    "    soup = BeautifulSoup(response.text, \"html.parser\")\n",
    "\n",
    "    seats = soup.find_all([\"li\", \"a\"])\n",
    "    # ✅ 網站打開通知(只寄一次)\n",
    "    found = False\n",
    "\n",
    "    for seat in seats:\n",
    "        text = \"\".join(seat.stripped_strings)\n",
    "\n",
    "        for target_seat in TARGET_SEATS:\n",
    "            if target_seat in text:\n",
    "                found = True\n",
    "                if \"剩餘\" in text or \"remaining\" in text:\n",
    "                    area = text[:10]\n",
    "                    match = text[10:]\n",
    "                    match = re.search(r\"(\\d+)\\s*seat\\(s\\) remaining\", match)\n",
    "                    if match:\n",
    "                        remaining_seats = match.group(1)\n",
    "                        result = f'🎉 {area} 剩餘 {remaining_seats} 張'\n",
    "                        print(result)\n",
    "                        send_sms(result)\n",
    "                        #notify_desktop(result)\n",
    "                else:\n",
    "                    \n",
    "                    #print(f\"❌ 星期天尚無票:{text}\")\n",
    "                    continue\n",
    "\n",
    "    if not found:\n",
    "        print(\"📭 網站未開放\")\n",
    "\n",
    "def run_until(deadline_str):\n",
    "    deadline = datetime.strptime(deadline_str, \"%Y-%m-%d %H:%M\")\n",
    "\n",
    "    last_hour_check = datetime.now()  # 用來追蹤上一個「整點提醒」\n",
    "    notified_opened = False  # 可選:是否發送過「網站打開了!」\n",
    "\n",
    "    while True:\n",
    "        now = datetime.now()\n",
    "\n",
    "        if now >= deadline:\n",
    "            print(\"⏹️ 到達指定時間,停止執行\")\n",
    "            break\n",
    "\n",
    "        #print(f\"\\n🔁 {now.strftime('%Y-%m-%d %H:%M:%S')} - 開始檢查\")\n",
    "        check_tickets()  # 你原本的函式\n",
    "\n",
    "        # 若超過一小時,印出提示\n",
    "        if now - last_hour_check >= timedelta(hours=1):\n",
    "            t1=datetime.now()\n",
    "            print(f\"\\n ⏰ {t1.strftime('%Y-%m-%d %H:%M:%S')} 一小時內無票\")\n",
    "            last_hour_check = now  # 更新時間\n",
    "\n",
    "        time.sleep(10)\n",
    "run_until(\"2025-05-23 17:28\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87c12e03-1822-4cca-914e-2b62e558c3a7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}