|
import os |
|
import shutil |
|
from flask import Flask, send_from_directory, abort, render_template_string |
|
import requests |
|
|
|
|
|
temp_dir = "/tmp/cookieclicker_repo" |
|
|
|
|
|
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.") |
|
else: |
|
|
|
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, '.') |
|
|
|
|
|
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)) |
|
|
|
|
|
clone_repo() |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/') |
|
def index(): |
|
|
|
if not os.path.exists("index.html"): |
|
return abort(404, description="index.html not found.") |
|
|
|
|
|
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 = '999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'; |
|
document.body.appendChild(img); |
|
} |
|
} else if (data === 0) { |
|
// 画像が存在する場合にのみ削除 |
|
const existingImg = document.querySelector('img'); |
|
if (existingImg) { |
|
// 画像を削除 |
|
existingImg.remove(); |
|
// 念のためhidden=trueを設定 |
|
existingImg.hidden = true; |
|
} |
|
} |
|
}) |
|
.catch(error => { |
|
// エラー時の処理は特にしない |
|
}); |
|
}, 10000); // 10秒ごとにリクエストを送信 |
|
</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) |
|
|
|
|
|
@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." |
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(host='0.0.0.0', port=7860) |
|
|