Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, send_from_directory, abort | |
| import schedule | |
| import threading | |
| import time | |
| import subprocess | |
| import os | |
| import shutil | |
| import stat | |
| import logging | |
| import re | |
| # ロギング設定をDEBUGに | |
| logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s") | |
| # 定数 | |
| REPO_URL = "https://github.com/SharkPool-SP/SharkPools-Extensions.git" | |
| REPO_DIR = "SharkPools-Extensions" | |
| TARGET_FILE = os.path.join(REPO_DIR, "pages", "startup.js") | |
| # Flask アプリケーション設定 | |
| # static_folder=None にして組み込みの静的配信はOFFにする | |
| app = Flask( | |
| __name__, | |
| template_folder="templates", | |
| static_folder=None, | |
| static_url_path=None | |
| ) | |
| # FlaskのロガーもDEBUGに | |
| app.logger.setLevel(logging.DEBUG) | |
| def index(): | |
| return render_template("index.html") | |
| def serve_any(filepath): | |
| app.logger.debug(f"📥 リクエストされたパス: {filepath!r}") | |
| # 1) static フォルダ | |
| static_path = os.path.join("static", filepath) # app.static_folder は None なので手動で指定 | |
| exists_static = os.path.isfile(static_path) | |
| app.logger.debug(f"→ static をチェック: {static_path!r} 存在? {exists_static}") | |
| if exists_static: | |
| app.logger.info(f"✅ static から返却: {filepath}") | |
| directory, filename = os.path.split(static_path) | |
| return send_from_directory(directory, filename) | |
| # 2) リポジトリ直下 | |
| full_path = os.path.join(REPO_DIR, filepath) | |
| exists_repo = os.path.isfile(full_path) | |
| app.logger.debug(f"→ repo をチェック: {full_path!r} 存在? {exists_repo}") | |
| if exists_repo: | |
| directory, filename = os.path.split(full_path) | |
| app.logger.info(f"✅ repo から返却: ディレクトリ={directory!r}, ファイル名={filename!r}") | |
| return send_from_directory(directory, filename) | |
| app.logger.warning(f"❌ ファイルが見つからない: {filepath!r}") | |
| abort(404) | |
| # 読み取り専用ファイルを削除できるようにする | |
| def remove_readonly(func, path, excinfo): | |
| os.chmod(path, stat.S_IWRITE) | |
| func(path) | |
| def clone_or_update_repo(): | |
| logging.info("リポジトリの更新を開始します。") | |
| if not os.path.exists(REPO_DIR): | |
| logging.info("リポジトリをクローン中...") | |
| subprocess.run(["git", "clone", REPO_URL]) | |
| else: | |
| logging.info("リポジトリをプル中...") | |
| result = subprocess.run(["git", "-C", REPO_DIR, "pull"], capture_output=True, text=True) | |
| logging.info(result.stdout) | |
| if result.stderr: | |
| logging.error(result.stderr) | |
| src_target_file = os.path.join(REPO_DIR, "pages", "startup.js") | |
| dest_pages = os.path.join("static", "pages") | |
| if not os.path.exists(src_target_file): | |
| logging.warning(f"{src_target_file} が存在しません。") | |
| else: | |
| if os.path.exists(dest_pages): | |
| shutil.rmtree(dest_pages, onerror=remove_readonly) | |
| shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages) | |
| target_file_copy = os.path.join(dest_pages, "startup.js") | |
| with open(target_file_copy, "r", encoding="utf-8") as f: | |
| content = f.read() | |
| logging.info(f"コピー後のstartup.jsの内容抜粋:\n{content[:200]}") | |
| logging.info("startup.js を修正中...") | |
| new_content = content.replace( | |
| '}, "https://studio.penguinmod.com");', | |
| '}, "*");' | |
| ) | |
| if content == new_content: | |
| logging.warning("置換対象の文字列が見つかりませんでした。") | |
| else: | |
| with open(target_file_copy, "w", encoding="utf-8") as f: | |
| f.write(new_content) | |
| logging.info("置換が完了しました。") | |
| # index.html をコピー | |
| os.makedirs("templates", exist_ok=True) | |
| shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html") | |
| index_path = os.path.join("templates", "index.html") | |
| with open(index_path, "r", encoding="utf-8") as f: | |
| html = f.read() | |
| # <base href=...> を削除 | |
| new_html = re.sub( | |
| r'<base\s+href="https://sharkpools-extensions\.vercel\.app/"\s*/?>', | |
| '', | |
| html, | |
| flags=re.IGNORECASE | |
| ) | |
| # <span> の直後に <h1> を追加 | |
| insertion_text = '<br><h2 style="color: red">これらの拡張機能はSharkPoolによって作成されました。</h2><span style="color: red">CORS制限の問題から、自動で置き換えするクリプトを使用し別のホストでホストされていますが、拡張機能は一日ごとに自動で更新されます。' | |
| pattern = r'(<span style="color:\s*pink">\s*Please Load Extensions Unsandboxed, otherwise they will not Load\.\s*</span>)' | |
| if re.search(pattern, new_html): | |
| new_html = re.sub(pattern, r'\1' + insertion_text, new_html) | |
| logging.info("✅ <h1>タグを挿入しました。") | |
| else: | |
| logging.warning("⚠️ 挿入対象の <span> タグが見つかりませんでした。") | |
| with open(index_path, "w", encoding="utf-8") as f: | |
| f.write(new_html) | |
| logging.info("index.html の修正が完了しました。") | |
| logging.info("リポジトリの更新が完了しました。") | |
| # スケジューラを実行する関数(60分ごと) | |
| def run_scheduler(): | |
| schedule.every(60).minutes.do(clone_or_update_repo) | |
| try: | |
| while True: | |
| schedule.run_pending() | |
| time.sleep(1) | |
| except Exception as e: | |
| logging.error(f"スケジューラでエラーが発生しました: {e}") | |
| # 初回実行およびスケジューラ起動 | |
| clone_or_update_repo() | |
| scheduler_thread = threading.Thread(target=run_scheduler, daemon=True) | |
| scheduler_thread.start() | |
| # アプリ起動 | |
| if __name__ == "__main__": | |
| app.run(debug=True, host="0.0.0.0", port=7860) | |