上傳檔案到「/」
-- 指定專案資料夾 cd /employee_votes -- gitea 初始化 git init -- 建立註解 git add . git commit -m "Initial commit" -- 切換到 main 分支 git branch -M main -- 上傳檔案到 gitea git remote add origin https://github.com/91771/<REPO>.git git push -u origin main
This commit is contained in:
300
temp_app.py
Normal file
300
temp_app.py
Normal file
@@ -0,0 +1,300 @@
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
import mysql.connector
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
from datetime import datetime
|
||||
import re
|
||||
import urllib3
|
||||
|
||||
# 禁用SSL警<4C>?
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Database configuration from DB_connection.txt
|
||||
DB_CONFIG = {
|
||||
'host': 'mysql.theaken.com',
|
||||
'port': 33306,
|
||||
'database': 'db_A019',
|
||||
'user': 'A019',
|
||||
'password': '9wvKEkxBzVca'
|
||||
}
|
||||
|
||||
def get_db_connection():
|
||||
"""Establish database connection"""
|
||||
try:
|
||||
conn = mysql.connector.connect(**DB_CONFIG)
|
||||
return conn
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Database connection error: {e}")
|
||||
return None
|
||||
|
||||
def create_menu_table():
|
||||
"""Create menu_items table if not exists"""
|
||||
conn = get_db_connection()
|
||||
if conn:
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS menu_items (
|
||||
Id INTEGER AUTO_INCREMENT PRIMARY KEY,
|
||||
main_course VARCHAR(50) NOT NULL,
|
||||
side_dish VARCHAR(50),
|
||||
addon VARCHAR(50),
|
||||
is_active BOOLEAN NOT NULL
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
print("Menu table created or already exists")
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error creating table: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def scrape_menu_data():
|
||||
"""Scrape menu data from the target URL"""
|
||||
url = "https://club.panjit.com.tw/back/menu/menu.php?1672970133&indexselectid=1"
|
||||
|
||||
try:
|
||||
# Bypass SSL verification for the target website
|
||||
response = requests.get(url, timeout=10, verify=False)
|
||||
response.raise_for_status()
|
||||
|
||||
soup = BeautifulSoup(response.content, 'html.parser')
|
||||
|
||||
# ?<3F><>?實<>?網<>?結<>?<3F><>???<3F>單資<E596AE>?
|
||||
menu_items = []
|
||||
|
||||
# ?<3F>找?<3F>單表格?<3F><>?<3F><>? menu_tables = soup.find_all('table')
|
||||
menu_lists = soup.find_all(['ul', 'ol'])
|
||||
|
||||
# 如<>??<3F>到表格結<E6A0BC>?
|
||||
if menu_tables:
|
||||
for table in menu_tables:
|
||||
rows = table.find_all('tr')
|
||||
for row in rows:
|
||||
cells = row.find_all('td')
|
||||
if len(cells) >= 2: # ?<3F><>??<3F><>?稱<>??<3F>格
|
||||
name = cells[0].get_text(strip=True)
|
||||
price_text = cells[1].get_text(strip=True)
|
||||
|
||||
# ?<3F><>??<3F>格?<3F><>?
|
||||
price = 0.0
|
||||
try:
|
||||
# 從<>?字中?<3F><>??<3F><>?
|
||||
price_match = re.search(r'\d+', price_text)
|
||||
if price_match:
|
||||
price = float(price_match.group())
|
||||
except:
|
||||
pass
|
||||
|
||||
if name and price > 0:
|
||||
menu_items.append({
|
||||
'name': name,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': price,
|
||||
'description': f'{name} - 美味餐<E591B3>?',
|
||||
'image_url': f'/static/images/{name.replace(" ", "_").lower()}.jpg'
|
||||
})
|
||||
|
||||
# 如<>??<3F>到?<3F>表結<E8A1A8>?
|
||||
elif menu_lists:
|
||||
for menu_list in menu_lists:
|
||||
items = menu_list.find_all('li')
|
||||
for item in items:
|
||||
text = item.get_text(strip=True)
|
||||
if text and any(char.isdigit() for char in text):
|
||||
# ?<3F>試<EFBFBD><E8A9A6>???<3F>目?<3F>稱?<3F>價?? # 尋找?<3F>格模<E6A0BC>?
|
||||
price_match = re.search(r'(\$|NT\$|\$)?\s*(\d+)', text)
|
||||
if price_match:
|
||||
price = float(price_match.group(2))
|
||||
name = re.sub(r'(\$|NT\$|\$)?\s*\d+', '', text).strip()
|
||||
|
||||
if name and price > 0:
|
||||
menu_items.append({
|
||||
'name': name,
|
||||
'category': '餐<EFBFBD>?',
|
||||
'price': price,
|
||||
'description': f'{name} - 精選美<E981B8>?',
|
||||
'image_url': f'/static/images/{name.replace(" ", "_").lower()}.jpg'
|
||||
})
|
||||
|
||||
# 如<>?以<>??<3F><>??<3F><>??<3F>到,使?<3F><>??<3F>方案<E696B9>??<3F><>??<3F>含?<3F>單?<3F><>??<3F>div?<3F>span
|
||||
if not menu_items:
|
||||
# 尋找?<3F>能?<3F>含?<3F>單?<3F>目?<3F><>?<3F><>? possible_menu_elements = soup.find_all(['div', 'span', 'p'])
|
||||
for elem in possible_menu_elements:
|
||||
text = elem.get_text(strip=True)
|
||||
if text and len(text) > 3 and any(char.isdigit() for char in text):
|
||||
price_match = re.search(r'(\$|NT\$|\$)?\s*(\d+)', text)
|
||||
if price_match:
|
||||
price = float(price_match.group(2))
|
||||
name = re.sub(r'(\$|NT\$|\$)?\s*\d+.*', '', text).strip()
|
||||
|
||||
if name and price > 0 and len(name) > 1:
|
||||
menu_items.append({
|
||||
'name': name,
|
||||
'category': '餐<EFBFBD>?',
|
||||
'price': price,
|
||||
'description': f'{name} - 餐廳?<3F>薦',
|
||||
'image_url': f'/static/images/{name.replace(" ", "_").lower()}.jpg'
|
||||
})
|
||||
|
||||
# 如<>?仍然沒<E784B6>??<3F>到?<3F>單?<3F>目,使?<3F><>?例<>??? if not menu_items:
|
||||
menu_items = [
|
||||
{
|
||||
'name': '經典?<3F><>?<3F><>?,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': 120.00,
|
||||
'description': '?<3F>統?<3F>灣?<3F><>?麵<>?湯頭濃<E9A0AD>?',
|
||||
'image_url': '/static/images/beef_noodle.jpg'
|
||||
},
|
||||
{
|
||||
'name': '?<3F>腿<EFBFBD><E885BF>?,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': 95.00,
|
||||
'description': '香<EFBFBD>??<3F>腿?<3F><>???,
|
||||
'image_url': '/static/images/chicken_rice.jpg'
|
||||
},
|
||||
{
|
||||
'name': '?<3F><>?沙<>?',
|
||||
'category': '?<3F><>?',
|
||||
'price': 65.00,
|
||||
'description': '?<3F>鮮?<3F>蔬?<3F><>??<3F>製?<3F><>?',
|
||||
'image_url': '/static/images/salad.jpg'
|
||||
},
|
||||
{
|
||||
'name': '紅<EFBFBD>??<3F><>???,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': 150.00,
|
||||
'description': '?<3F>統江<E7B5B1>??<3F><>??<3F>質鮮嫩',
|
||||
'image_url': '/static/images/lion_head.jpg'
|
||||
},
|
||||
{
|
||||
'name': '?<3F><>???,
|
||||
'category': '?<3F><>?',
|
||||
'price': 60.00,
|
||||
'description': '?<3F>令?<3F><>?清<>?',
|
||||
'image_url': '/static/images/vegetable.jpg'
|
||||
}
|
||||
]
|
||||
|
||||
return menu_items
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"Error scraping menu data: {e}")
|
||||
# 返<>?範<>?資<>?作為?<3F>用
|
||||
return [
|
||||
{
|
||||
'name': '經典?<3F><>?<3F><>?,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': 120.00,
|
||||
'description': '?<3F>統?<3F>灣?<3F><>?麵<>?湯頭濃<E9A0AD>?',
|
||||
'image_url': '/static/images/beef_noodle.jpg'
|
||||
},
|
||||
{
|
||||
'name': '?<3F>腿<EFBFBD><E885BF>?,
|
||||
'category': '主<EFBFBD>?',
|
||||
'price': 95.00,
|
||||
'description': '香<EFBFBD>??<3F>腿?<3F><>???,
|
||||
'image_url': '/static/images/chicken_rice.jpg'
|
||||
}
|
||||
]
|
||||
|
||||
def insert_menu_data():
|
||||
"""Insert sample menu items into database"""
|
||||
conn = get_db_connection()
|
||||
if conn:
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Clear existing data
|
||||
cursor.execute("DELETE FROM menu_items")
|
||||
|
||||
# Insert sample data that matches the new table structure
|
||||
sample_menu_items = [
|
||||
('經典?<3F><>?<3F><>?, '?<EFBFBD>工麵<EFBFBD>?', '?<EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?, True),
|
||||
('香<EFBFBD>??<3F>腿<EFBFBD><E885BF>?, '?<EFBFBD>令?<EFBFBD><EFBFBD>?', '?<EFBFBD>米濃湯', True),
|
||||
('紅<EFBFBD>??<3F><>???, '?<EFBFBD>飯', '紫<EFBFBD>??<EFBFBD>花<EFBFBD><EFBFBD>?, True),
|
||||
('?<3F><>??<3F>麵', None, '?<3F><>?<3F><>?, True),
|
||||
('?<3F>鮭魚<E9AEAD>?', '馬鈴?<3F>泥', '?<3F><>?<3F><>?, True)
|
||||
]
|
||||
|
||||
insert_query = """
|
||||
INSERT INTO menu_items (main_course, side_dish, addon, is_active)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
"""
|
||||
|
||||
for item in sample_menu_items:
|
||||
cursor.execute(insert_query, item)
|
||||
|
||||
conn.commit()
|
||||
print(f"Inserted {len(sample_menu_items)} menu items")
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error inserting menu data: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
"""Main page - display menu items"""
|
||||
conn = get_db_connection()
|
||||
menu_items = []
|
||||
|
||||
if conn:
|
||||
try:
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM menu_items ORDER BY category, name")
|
||||
menu_items = cursor.fetchall()
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error fetching menu items: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return render_template('index.html', menu_items=menu_items)
|
||||
|
||||
@app.route('/api/menu')
|
||||
def api_menu():
|
||||
"""API endpoint to get menu items"""
|
||||
conn = get_db_connection()
|
||||
menu_items = []
|
||||
|
||||
if conn:
|
||||
try:
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM menu_items ORDER BY category, name")
|
||||
menu_items = cursor.fetchall()
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error fetching menu items: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return jsonify(menu_items)
|
||||
|
||||
@app.route('/init')
|
||||
def initialize():
|
||||
"""Initialize database with menu data"""
|
||||
create_menu_table()
|
||||
insert_menu_data()
|
||||
return "Database initialized with sample menu data"
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Initialize database on startup
|
||||
create_menu_table()
|
||||
|
||||
# Check if menu items exist, if not, insert sample data
|
||||
conn = get_db_connection()
|
||||
if conn:
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM menu_items")
|
||||
count = cursor.fetchone()[0]
|
||||
if count == 0:
|
||||
insert_menu_data()
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error checking menu items: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
app.run(debug=True, port=5000)
|
Reference in New Issue
Block a user