any-env-code / app.py
izuemon's picture
Update app.py
8f064ef verified
from flask import Flask, request, jsonify, redirect, Response
import os
import requests
import subprocess
import sys
app = Flask(__name__)
processes = {
"yt": {
"file": "yt.py",
"proc": None,
},
"news": {
"file": "news.py",
"proc": None,
},
"chatgpt": {
"file": "chatgpt.py",
"proc": None,
},
"jihou": {
"file": "jihou.py",
"proc": None,
},
"join": {
"file": "join.py",
"proc": None,
},
"ngwords": {
"file": "ngwords.py",
"proc": None,
},
}
def start_processes():
for name, info in processes.items():
proc = info["proc"]
if proc is None or proc.poll() is not None:
info["proc"] = subprocess.Popen(
[sys.executable, info["file"]],
stdout=sys.stdout,
stderr=sys.stderr,
env=os.environ,
)
print(f"{info['file']} を起動しました")
#----------
@app.route("/drive.com/files")
def index():
ip = request.remote_addr
print(f"アクセスIP: {ip}")
return redirect("https://drive.google.com/")
@app.route("/channel-io-managers")
def get_managers():
limit = request.args.get("limit")
since = request.args.get("since")
params = {}
if limit:
params["limit"] = limit
if since:
params["since"] = since
channel_id = request.args.get("channelid", "200605")
url = f"https://desk-api.channel.io/desk/channels/{channel_id}/managers"
headers = {
"accept": "application/json",
"x-account": os.getenv("channeliotokenmain"),
}
res = requests.get(url, headers=headers, params=params)
if res.status_code != 200:
return jsonify({"error": res.text}), res.status_code
return jsonify(res.json().get("managers", []))
@app.route("/cors-proxy", methods=["GET"])
def corsproxy():
url = request.args.get("url")
if not url:
return "url パラメータが必要です", 400
if not url.startswith(("http://", "https://")):
return "http または https のURLのみ使用できます", 400
resp = requests.get(url, headers=request.headers, timeout=60)
response = Response(resp.content, resp.status_code)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PATCH, OPTIONS"
if "Content-Type" in resp.headers:
response.headers["Content-Type"] = resp.headers["Content-Type"]
return response
@app.route("/cors-proxy", methods=["POST", "PATCH"])
def corsproxy_post():
url = request.args.get("url")
if not url:
return "url パラメータが必要です", 400
if not url.startswith(("http://", "https://")):
return "http または https のURLのみ使用できます", 400
resp = requests.request(
method=request.method,
url=url,
headers=request.headers,
data=request.data,
timeout=60,
)
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET, POST, PATCH, OPTIONS",
}
return Response(resp.content, resp.status_code, headers=headers)
if __name__ == "__main__":
if os.environ.get("WERKZEUG_RUN_MAIN") != "true":
start_processes()
app.run(host="0.0.0.0", port=7860)