File size: 4,492 Bytes
83510eb
 
9883435
83510eb
9883435
83510eb
 
 
9883435
83510eb
 
 
 
 
 
 
 
 
 
 
 
9883435
83510eb
9883435
83510eb
 
 
 
 
 
9883435
83510eb
 
9883435
83510eb
 
 
 
9883435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83510eb
 
 
 
 
 
 
 
 
 
 
 
9883435
83510eb
 
 
 
 
 
 
 
 
 
 
9883435
83510eb
 
 
 
 
 
 
9883435
83510eb
 
 
 
 
 
 
 
 
 
9883435
 
83510eb
 
 
 
 
 
 
9883435
 
83510eb
 
 
 
9883435
 
83510eb
 
 
 
 
 
 
9883435
 
 
 
 
 
 
 
 
 
83510eb
9883435
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import shutil
from flask import Flask, send_from_directory, abort, render_template_string, send_file
import requests
import zipfile

# リポジトリをクローンするディレクトリ
temp_dir = "/tmp/cookieclicker_repo"
zip_filename = "build.zip"  # ZIPファイル名

# リポジトリのクローンを行う
def clone_repo():
    # 一時ディレクトリが存在する場合は削除
    if os.path.exists(temp_dir):
        shutil.rmtree(temp_dir)

    print("Cloning the repository...")
    result = os.system(f"git clone https://github.com/izum00/cookieclicker.git {temp_dir}")
    
    if result != 0:
        print("Error: Failed to clone the repository.")
        return
    else:
        # index.htmlの移動
        index_html_path = os.path.join(temp_dir, 'index.html')
        if os.path.exists(index_html_path):
            if os.path.exists('index.html'):
                os.remove('index.html')
        shutil.move(index_html_path, '.')

        # static ディレクトリ作成と移動
        if not os.path.exists('static'):
            os.mkdir('static')

        for item in os.listdir(temp_dir):
            if item != 'index.html':
                shutil.move(os.path.join(temp_dir, item), os.path.join('static', item))

        # ZIPを作成
        create_zip()


# ZIPファイル作成関数
def create_zip():
    if os.path.exists(zip_filename):
        os.remove(zip_filename)

    with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
        # index.html をZIPへ
        zipf.write("index.html", "index.html")

        # static 以下を全部ZIPへ
        for root, dirs, files in os.walk("static"):
            for file in files:
                full_path = os.path.join(root, file)
                arcname = os.path.relpath(full_path, ".")  # static からの相対パス
                zipf.write(full_path, arcname)

    print("ZIP created:", zip_filename)


# クローン実行
clone_repo()

# Flaskアプリケーションの設定
app = Flask(__name__)

# ルートでindex.htmlを表示
@app.route('/')
def index():
    # index.htmlが存在しない場合は404エラー
    if not os.path.exists("index.html"):
        return abort(404, description="index.html not found.")

    # index.htmlの内容を読み込んでJS追加
    with open("index.html", "r") as file:
        index_html_content = file.read()

    js_code = """
<script>
    setInterval(() => {
        fetch('https://huggingface.co/spaces/soiz/cookie/raw/main/tof')
            .then(response => response.json())
            .then(data => {
                if (data === 1) {
                    const existingImg = document.querySelector('img');
                    if (!existingImg) {
                        const img = document.createElement('img');
                        img.src = 'https://huggingface.co/spaces/soiz/cookie/raw/main/1.png';
                        img.style.position = 'fixed';
                        img.style.top = '0';
                        img.style.left = '0';
                        img.style.width = '100vw';
                        img.style.height = '100vh';
                        img.style.zIndex = '999999999999999999999999999999';
                        document.body.appendChild(img);
                    }
                } else if (data === 0) {
                    const existingImg = document.querySelector('img');
                    if (existingImg) {
                        existingImg.remove();
                        existingImg.hidden = true;
                    }
                }
            })
            .catch(error => {});
    }, 10000);
</script>
    """

    index_html_content = index_html_content.replace("</body>", js_code + "</body>")

    return render_template_string(index_html_content)


# 静的ファイル提供
@app.route('/<path:filename>')
def static_files(filename):
    return send_from_directory('static', filename)


# main.js の存在確認
@app.route('/check_main_js')
def check_main_js():
    if os.path.exists('static/main.js'):
        return "main.js exists."
    else:
        return "main.js does not exist."


# ⭐ ZIP ダウンロード用ページ
@app.route('/download_build')
def download_build():
    if not os.path.exists(zip_filename):
        return abort(404, description="ZIP file not found.")
    return send_file(zip_filename, as_attachment=True)


# Flask起動
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)