From 2801234391352b70eb058e51befc1b5c2aa791b6 Mon Sep 17 00:00:00 2001 From: zhouzhongping Date: Thu, 18 Jan 2024 11:17:32 +0800 Subject: [PATCH] project: add download api --- Dockerfile | 3 +++ app.py | 28 ++++++++++++++++++++++++++++ tools.py | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tools.py diff --git a/Dockerfile b/Dockerfile index 38d2c3c..27b5699 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,9 @@ RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv # 安装依赖,同样使用临时的镜像源 RUN pipenv install --deploy --ignore-pipfile -i https://pypi.tuna.tsinghua.edu.cn/simple +# 安装 wget +RUN apt-get update && apt-get install -y wget + # 拷贝应用代码 COPY . /app diff --git a/app.py b/app.py index 3d816e1..ed50c28 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,8 @@ import requests from fake_useragent import UserAgent from flask import Flask, request, jsonify +import tools + app = Flask(__name__) @@ -102,5 +104,31 @@ def proxy(): }), 500 +@app.route('/download', methods=['GET']) +def proxy(): + url = request.args.get('url') + if not url: + return jsonify({ + 'status': 'error', + 'msg': 'No URL provided', + 'data': {} + }), 400 + + content = tools.download_html_with_wget(url) + + if content is None: + return jsonify({ + 'status': 'error', + 'msg': 'Unable to download the content', + 'data': {} + }), 500 + + return jsonify({ + 'status': 'success', + 'msg': 'Request successful', + 'data': content + }), 200 + + if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..ef02673 --- /dev/null +++ b/tools.py @@ -0,0 +1,24 @@ +import os +import subprocess +import tempfile + + +def download_html_with_wget(url): + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: + tmp_file_name = tmp_file.name + + command = ['wget', '-q', '-O', tmp_file_name, url] + + try: + subprocess.run(command, check=True) + with open(tmp_file_name, 'r') as file: + content = file.read() + return content + + except subprocess.CalledProcessError as e: + print(f'An error occurred: {e}') + return None + + finally: + if os.path.exists(tmp_file_name): + os.remove(tmp_file_name)