soiz1 commited on
Commit
d5896b5
·
verified ·
1 Parent(s): 898a359

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +5 -7
  2. app.py +116 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,12 +1,10 @@
1
  ---
2
- title: Sharkpool Extensions
3
- emoji: 👀
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.38.0
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Sharkpool Extension
3
+ emoji: 🚀
4
+ colorFrom: purple
5
+ colorTo: indigo
6
  sdk: gradio
 
 
7
  pinned: false
8
  ---
9
 
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, send_from_directory
2
+ import schedule
3
+ import threading
4
+ import time
5
+ import subprocess
6
+ import os
7
+ import shutil
8
+ import stat
9
+ import logging
10
+
11
+ # ロギング設定
12
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
13
+
14
+ # 定数
15
+ REPO_URL = "https://github.com/SharkPool-SP/SharkPools-Extensions.git"
16
+ REPO_DIR = "SharkPools-Extensions"
17
+ TARGET_FILE = os.path.join(REPO_DIR, "pages", "startup.js")
18
+
19
+ # Flask アプリケーション設定
20
+ app = Flask(__name__, template_folder="templates", static_folder="static")
21
+
22
+ @app.route("/")
23
+ def index():
24
+ return render_template("index.html")
25
+
26
+ @app.route('/<path:filename>')
27
+ def serve_static(filename):
28
+ return send_from_directory(app.static_folder, filename)
29
+
30
+ # 読み取り専用ファイルを削除できるようにする
31
+ def remove_readonly(func, path, excinfo):
32
+ os.chmod(path, stat.S_IWRITE)
33
+ func(path)
34
+
35
+ def clone_or_update_repo():
36
+ logging.info("リポジトリの更新を開始します。")
37
+
38
+ if not os.path.exists(REPO_DIR):
39
+ logging.info("リポジトリをクローン中...")
40
+ subprocess.run(["git", "clone", REPO_URL])
41
+ else:
42
+ logging.info("リポジトリをプル中...")
43
+ result = subprocess.run(["git", "-C", REPO_DIR, "pull"], capture_output=True, text=True)
44
+ logging.info(result.stdout)
45
+ if result.stderr:
46
+ logging.error(result.stderr)
47
+
48
+ src_target_file = os.path.join(REPO_DIR, "pages", "startup.js")
49
+ dest_pages = os.path.join("static", "pages")
50
+
51
+ if not os.path.exists(src_target_file):
52
+ logging.warning(f"{src_target_file} が存在しません。")
53
+ else:
54
+ if os.path.exists(dest_pages):
55
+ shutil.rmtree(dest_pages, onerror=remove_readonly)
56
+ shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages)
57
+
58
+ target_file_copy = os.path.join(dest_pages, "startup.js")
59
+
60
+ with open(target_file_copy, "r", encoding="utf-8") as f:
61
+ content = f.read()
62
+
63
+ logging.info(f"コピー後のstartup.jsの内容抜粋:\n{content[:200]}")
64
+
65
+ logging.info("startup.js を修正中...")
66
+ new_content = content.replace(
67
+ '}, "https://studio.penguinmod.com");',
68
+ '}, "*");'
69
+ )
70
+ if content == new_content:
71
+ logging.warning("置換対象の文字列が見つかりませんでした。")
72
+ else:
73
+ with open(target_file_copy, "w", encoding="utf-8") as f:
74
+ f.write(new_content)
75
+ logging.info("置換が完了しました。")
76
+
77
+ # index.html をコピー
78
+ os.makedirs("templates", exist_ok=True)
79
+ shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html")
80
+
81
+ # ここで <base href="..."> を削除
82
+ index_path = os.path.join("templates", "index.html")
83
+ with open(index_path, "r", encoding="utf-8") as f:
84
+ html = f.read()
85
+
86
+ import re
87
+ # <base href="https://sharkpools-extensions.vercel.app/"> を削除
88
+ new_html = re.sub(r'<base\s+href="https://sharkpools-extensions\.vercel\.app/"\s*/?>', '', html, flags=re.IGNORECASE)
89
+
90
+ if html == new_html:
91
+ logging.warning("<base href=...> タグが見つかりませんでした。")
92
+ else:
93
+ with open(index_path, "w", encoding="utf-8") as f:
94
+ f.write(new_html)
95
+ logging.info("<base href=...> タグの削除が完了しました。")
96
+
97
+ logging.info("リポジトリの更新が完了しました。")
98
+
99
+ # スケジューラを実行する関数(1分ごと)
100
+ def run_scheduler():
101
+ schedule.every(60).minutes.do(clone_or_update_repo)
102
+ try:
103
+ while True:
104
+ schedule.run_pending()
105
+ time.sleep(1)
106
+ except Exception as e:
107
+ logging.error(f"スケジューラでエラーが発生しました: {e}")
108
+
109
+ # 初回実行およびスケジューラ起動
110
+ clone_or_update_repo()
111
+ scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
112
+ scheduler_thread.start()
113
+
114
+ # アプリ起動
115
+ if __name__ == "__main__":
116
+ app.run(debug=True, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ flask
2
+ schedule