| import os |
| import pandas as pd |
| from flask import Flask, render_template |
|
|
| |
| app = Flask(__name__) |
|
|
| |
| PRODUCT_SHEET_URL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSHOvfG4hGVCBQYsjOpC0QhamhVKWwFojz4IRc32B6HOvRDaEqc9dNRy8E8pLyJDnDo2mGvJ4oJANtk/pub?gid=319581950&single=true&output=csv" |
| VIDEO_SHEET_URL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSHOvfG4hGVCBQYsjOpC0QhamhVKWwFojz4IRc32B6HOvRDaEqc9dNRy8E8pLyJDnDo2mGvJ4oJANtk/pub?gid=1503614356&single=true&output=csv" |
| |
|
|
| def load_data(url): |
| """ |
| Hàm tải dữ liệu từ Google Sheet CSV và trả về danh sách (list of dictionaries). |
| """ |
| try: |
| df = pd.read_csv(url, dtype=str) |
| df = df.fillna("") |
| return df.to_dict('records') |
| except Exception as e: |
| print(f"[LỖI LOAD_DATA] Lỗi khi tải dữ liệu từ {url}: {e}") |
| return [] |
|
|
| @app.route('/') |
| def home(): |
| """Trang chủ của website""" |
| products_raw = load_data(PRODUCT_SHEET_URL) |
| videos = load_data(VIDEO_SHEET_URL) |
| |
| products_processed = [] |
| |
| for product in products_raw: |
| try: |
| |
| |
| file_id = product.get('image_id') |
| |
| if file_id: |
| |
| product['image_url_processed'] = f"https://drive.google.com/thumbnail?id={file_id}&sz=w500" |
| else: |
| |
| product['image_url_processed'] = "https://placehold.co/300x300/eee/333?text=No+Image" |
| |
| |
| |
| |
| shopee_link = product.get('link_shopee', '') |
| |
| if shopee_link and not shopee_link.startswith(('http://', 'https://')): |
| |
| product['shopee_link_processed'] = f"https://{shopee_link}" |
| elif not shopee_link: |
| |
| product['shopee_link_processed'] = '#' |
| else: |
| |
| product['shopee_link_processed'] = shopee_link |
| |
| |
| products_processed.append(product) |
| |
| except Exception as e: |
| print(f"Lỗi xử lý sản phẩm {product.get('name')}: {e}") |
| continue |
|
|
| |
| return render_template('index.html', products=products_processed, videos=videos) |
|
|
| if __name__ == '__main__': |
| |
| |
| port = int(os.environ.get("PORT", 7860)) |
| app.run(host='0.0.0.0', port=port) |