cookie2 / app.py
soiz's picture
Update app.py
ffbcd61 verified
raw
history blame
4.47 kB
import os
import shutil
from flask import Flask, send_from_directory, abort
# リポジトリをクローンするディレクトリ
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/ozh/cookieclicker.git {temp_dir}")
if result != 0:
print("Error: Failed to clone the repository.")
else:
# index.htmlを削除(存在する場合)
index_html_path = os.path.join(temp_dir, 'index.html')
if os.path.exists(index_html_path):
# もしカレントディレクトリに既にindex.htmlが存在する場合は削除
if os.path.exists('index.html'):
os.remove('index.html')
# 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))
# クローンを実行
clone_repo()
#HTMLの自動修正==========================
#1
from bs4 import BeautifulSoup
# index.htmlを読み込む関数
def modify_index_html():
try:
with open('index.html', 'r', encoding='utf-8') as file:
# BeautifulSoupでHTMLをパース
soup = BeautifulSoup(file, 'lxml')
# id="sectionRight"を取得
section_right = soup.find(id="sectionRight")
if section_right:
# id="store"を除くすべての子要素を空にする
for element in section_right.find_all(True): # Trueはすべてのタグを対象
if element.get('id') != 'store':
element.clear() # 要素の内容を空にする
# 新しい要素を作成して追加する
new_link = soup.new_tag('a', href="javascript:var s=document.createElement('script');s.type='text/javascript';s.src='https://huggingface.co/spaces/soiz/cookie/raw/main/cl.js';document.body.appendChild(s);void(0);")
new_link.string = "javascriptコードを実行"
# id="store"の前に追加
store_element = section_right.find(id="store")
if store_element:
store_element.insert_before(new_link) # id="store"の前に追加
# 変更を保存する
with open('index.html', 'w', encoding='utf-8') as file:
file.write(str(soup))
print("index.html has been modified successfully.")
except Exception as e:
print(f"An error occurred: {e}")
# 関数を実行
modify_index_html()
#=======
#2
import re
# main.jsファイルのパスを指定
file_path = 'main.js'
# ファイルを読み込んで内容を置換する関数
def replace_code(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 置換処理
new_content = re.sub(r'if\s*\(\s*top\s*!=\s*self\s*\)\s*Game\.ErrorFrame\s*\(\s*\);', 'if (false) Game.ErrorFrame();', content)
# ファイルに書き込み
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
print("置換が完了しました。")
# 関数を実行
replace_code(file_path)
#修正用コードここまで============================
# 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.")
return send_from_directory('.', 'index.html')
# 静的ファイルを提供するためのルート
@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."
if __name__ == '__main__':
# port 7860でFlaskアプリを起動
app.run(host='0.0.0.0', port=7860)