Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,81 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
import zipfile
|
| 4 |
-
|
| 5 |
-
from urllib.parse import urlparse
|
| 6 |
|
| 7 |
-
# 创建 Flask 应用实例
|
| 8 |
app = Flask(__name__)
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
def download_file(file_url):
|
| 33 |
-
"""下载单个文件"""
|
| 34 |
-
response = requests.get(file_url)
|
| 35 |
-
if response.status_code != 200:
|
| 36 |
-
raise Exception(f"Unable to download file: {response.status_code}")
|
| 37 |
-
return response.content
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
@app.route("/", methods=["GET", "POST"])
|
| 49 |
-
def index():
|
| 50 |
-
if request.method == "POST":
|
| 51 |
-
github_url = request.form["github_url"]
|
| 52 |
-
|
| 53 |
-
if not is_directory(github_url):
|
| 54 |
-
return "Error: Please provide a valid GitHub folder URL."
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
zip_buffer = create_zip(files)
|
| 72 |
-
return send_file(
|
| 73 |
-
zip_buffer,
|
| 74 |
-
download_name=f"{repo}_{folder_path.replace('/', '_')}.zip",
|
| 75 |
-
as_attachment=True,
|
| 76 |
-
)
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"Error: {str(e)}"
|
| 79 |
|
| 80 |
-
return render_template("index.html")
|
| 81 |
|
| 82 |
-
if __name__ ==
|
| 83 |
-
app.run(host="0.0.0.0"
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
from flask import Flask, render_template, request, send_file, flash
|
| 4 |
import requests
|
| 5 |
+
from git import Repo
|
| 6 |
+
import shutil
|
| 7 |
import zipfile
|
| 8 |
+
import re
|
|
|
|
| 9 |
|
|
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
+
app.secret_key = 'your_secret_key' # 更改为更安全的密钥
|
| 12 |
|
| 13 |
+
def download_github_files(repo_url, file_paths):
|
| 14 |
+
"""从GitHub仓库下载指定路径的文件,并打包成zip。"""
|
| 15 |
+
try:
|
| 16 |
+
temp_dir = tempfile.mkdtemp()
|
| 17 |
+
repo_name = repo_url.split('/')[-1].replace(".git", "")
|
| 18 |
+
local_repo_path = os.path.join(temp_dir, repo_name)
|
| 19 |
+
|
| 20 |
+
# 克隆仓库
|
| 21 |
+
Repo.clone_from(repo_url, local_repo_path)
|
| 22 |
|
| 23 |
+
zip_file_path = os.path.join(temp_dir, f"{repo_name}_files.zip")
|
| 24 |
+
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 25 |
+
for file_path in file_paths:
|
| 26 |
+
abs_file_path = os.path.join(local_repo_path, file_path)
|
| 27 |
+
if os.path.exists(abs_file_path) and os.path.isfile(abs_file_path):
|
| 28 |
+
zf.write(abs_file_path, os.path.basename(abs_file_path))
|
| 29 |
+
elif os.path.exists(abs_file_path) and os.path.isdir(abs_file_path):
|
| 30 |
+
for root, dirs, files in os.walk(abs_file_path):
|
| 31 |
+
for file in files:
|
| 32 |
+
file_abs_path = os.path.join(root, file)
|
| 33 |
+
zf.write(file_abs_path, os.path.relpath(file_abs_path, local_repo_path))
|
| 34 |
|
| 35 |
+
shutil.rmtree(local_repo_path) # 删除克隆的仓库
|
| 36 |
+
if os.path.exists(zip_file_path) and os.path.getsize(zip_file_path) > 0:
|
| 37 |
+
return zip_file_path, f"{repo_name}_files.zip"
|
| 38 |
+
else:
|
| 39 |
+
return None, None
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error during download: {e}")
|
| 42 |
+
return None, None
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
def parse_github_url(url):
|
| 46 |
+
"""解析 GitHub URL,提取仓库 URL 和文件路径。"""
|
| 47 |
+
match = re.match(r'https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)(.*)', url)
|
| 48 |
+
if match:
|
| 49 |
+
owner, repo, branch, path = match.groups()
|
| 50 |
+
repo_url = f'https://github.com/{owner}/{repo}.git'
|
| 51 |
+
file_paths = [path.lstrip('/')] if path else []
|
| 52 |
+
return repo_url, file_paths
|
| 53 |
+
else:
|
| 54 |
+
return None, None
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 58 |
+
def index():
|
| 59 |
+
if request.method == 'POST':
|
| 60 |
+
repo_path = request.form.get('repo_path')
|
| 61 |
+
if not repo_path:
|
| 62 |
+
flash('请提供 GitHub 链接', 'error')
|
| 63 |
+
return render_template('index.html')
|
| 64 |
|
| 65 |
+
repo_url, file_paths = parse_github_url(repo_path)
|
| 66 |
+
if not repo_url:
|
| 67 |
+
flash('无效的 GitHub 链接', 'error')
|
| 68 |
+
return render_template('index.html')
|
| 69 |
|
| 70 |
+
zip_path, zip_filename = download_github_files(repo_url, file_paths)
|
| 71 |
+
if zip_path:
|
| 72 |
+
return send_file(zip_path, as_attachment=True, download_name=zip_filename)
|
| 73 |
+
else:
|
| 74 |
+
flash('下载失败,请检查链接和文件路径。', 'error')
|
| 75 |
+
return render_template('index.html')
|
| 76 |
|
| 77 |
+
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
| 79 |
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
app.run(debug=True, host="0.0.0.0")
|