import datetime import logging import os import traceback import requests from flask import json, jsonify # To read json data from flask import request # To receive headers from flask import Flask # To be able to start the application from werkzeug.utils import secure_filename from Apietl.constant.ConfConstant import ConfConstant from Apietl.kwai_etl.report.AccountReport import AccountReport from Apietl.bytedance_etl.report.AdvertiserReport import AdvertiserReport from Apietl.utlis.EtlFirstDeliveryValidMaterials import EtlFirstDeliveryValidMaterials from Apietl.utlis.UpLoadFile import UpLoadFile app = Flask(__name__) @app.route('/') def api_root(): return 'Welcome guys' @app.route('/webhook/account_report', methods=['POST']) def api_webhook_messages(): print(json.loads(request.data)) my_info = json.loads(request.data) account_id = my_info["account_id"] start_date = my_info["start_date"] end_date = my_info["end_date"] date_type = my_info["date_type"] AccountReport().handler(date_type, account_id, start_date, end_date) return '200' @app.route('/webhook/bytedance_account_report', methods=['POST']) def api_webhook_bytedance_messages(): print(json.loads(request.data)) my_info = json.loads(request.data) account_id = my_info["advertiser_id"] start_date = my_info["start_date"] end_date = my_info["end_date"] date_type = my_info["date_type"] AdvertiserReport().handler(date_type, account_id, start_date, end_date) return '200' @app.route('/webhook/get_file', methods=['POST']) def api_webhook_get_file(): payload = {} headers = { 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)', 'Accept': '*/*', 'Host': 'api.tjyourong.com.cn', 'Connection': 'keep-alive' } if 'file' not in request.files: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error') response = requests.request("GET", url, headers=headers, data=payload) print(response.text, 'error1') return '200' file = request.files['file'] if file.filename == '': url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error') response = requests.request("GET", url, headers=headers, data=payload) print(response.text, 'error2') return '200' try: file.save('/home/excel/' + 'upload.csv') status = UpLoadFile.uploadFile('upload.csv') if status == 0: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='success') requests.request("GET", url, headers=headers, data=payload) else: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error') requests.request("GET", url, headers=headers, data=payload) material_status = EtlFirstDeliveryValidMaterials().handler() if material_status == 0: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='success') requests.request("GET", url, headers=headers, data=payload) else: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error') requests.request("GET", url, headers=headers, data=payload) os.remove('/data/excel/' + 'upload.csv') return '200' except Exception as e: url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error') response = requests.request("GET", url, headers=headers, data=payload) print(response.text, 'error3') return '200' if __name__ == '__main__': app.run(port=8765, host=ConfConstant.URL, debug=True)