any-env-code / app.py
izuemon's picture
Update app.py
a196b17 verified
raw
history blame
3.25 kB
from flask import Flask, request, jsonify, redirect, Response
import os
import requests
app = Flask(__name__)
@app.route("/drive.com/files")
def index():
ip = request.remote_addr # クライアントIP取得
print(f"アクセスIP: {ip}") # ログ表示
return redirect("https://drive.google.com/")
@app.route("/channel-io-managers", methods=["GET"])
def get_managers():
# URLパラメータ取得
limit = request.args.get("limit")
since = request.args.get("since")
# パラメータがある場合だけ付ける
params = {}
if limit:
params["limit"] = limit
if since:
params["since"] = since
if request.args.get("channelid"):
CHANNEL_ID = request.args.get("channelid")
else:
CHANNEL_ID = "200605"
BASE_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/managers"
headers = {
"accept": "application/json",
"x-account": os.environ.get("channeliotokenmain") # ←環境変数 token
}
res = requests.get(BASE_URL, headers=headers, params=params)
# APIエラー処理
if res.status_code != 200:
return jsonify({"error": res.text}), res.status_code
json_data = res.json()
# managersの部分だけ返す
return jsonify(json_data.get("managers", []))
@app.route("/cors-proxy")
def corsproxy():
url = request.args.get("url")
if not url:
return "url パラメータが必要です", 400
if not url.startswith("http://") and not url.startswith("https://"):
return "http または https のURLのみ使用できます", 400
try:
resp = requests.get(url, headers=request.headers, timeout=60)
# 元のレスポンスヘッダを継承
response = Response(resp.content, resp.status_code)
# CORSヘッダを追記(上書きしない)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
# ★ Content-Typeを元のままに設定 ★
if "Content-Type" in resp.headers:
response.headers["Content-Type"] = resp.headers["Content-Type"]
return response
except Exception as e:
return f"エラー: {str(e)}", 500
@app.route("/cors-proxy", methods=["POST"])
def corsproxy_post():
url = request.args.get("url")
if not url:
return "url パラメータが必要です", 400
if not url.startswith("http://") and not url.startswith("https://"):
return "http または https のURLのみ使用できます", 400
try:
resp = requests.post(
url,
data=request.data,
headers=request.headers,
timeout=60
)
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS"
}
return Response(resp.content, resp.status_code, headers=headers)
except Exception as e:
return f"エラー: {str(e)}", 500
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=7860)