project: add download api
This commit is contained in:
parent
f34a5c2829
commit
2801234391
@ -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
|
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
|
COPY . /app
|
||||||
|
|
||||||
|
28
app.py
28
app.py
@ -4,6 +4,8 @@ import requests
|
|||||||
from fake_useragent import UserAgent
|
from fake_useragent import UserAgent
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
|
|
||||||
|
import tools
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -102,5 +104,31 @@ def proxy():
|
|||||||
}), 500
|
}), 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__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5000)
|
app.run(host='0.0.0.0', port=5000)
|
||||||
|
24
tools.py
Normal file
24
tools.py
Normal file
@ -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)
|
Reference in New Issue
Block a user