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/PycharmProjects/JupyterProject/tf25.ipynb
{
 "cells": [
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "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",
    "# Twilio 帳戶資訊\n",
    "TWILIO_ACCOUNT_SID = \"AC1171c857c61a9f2b20deb4c6e875642f\"\n",
    "TWILIO_AUTH_TOKEN = \"561ccf35d5801b13e118de6be8f2418a\"\n",
    "client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)\n",
    "TWILIO_PHONE_NUMBER = \"+15074073576\"\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 = \"httpstixcraft.comticketarea25_jhope19138\"\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(fn🔁 {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(t1.strftime('%Y-%m-%d %H:%M:%S'))\n",
    "            print('一小時內無票\\n')\n",
    "            last_hour_check = now  # 更新時間\n",
    "\n",
    "        time.sleep(10)\n",
    "run_until('2025-05-23 14:37')\n"
   ],
   "id": "da2defd3bd53c0c9",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}