"""
@author: luojunhui
@description: 票圈后台的接口方法
"""
import requests
from config import apolloConfig


class PQAPI(object):
    """
    PQ API
    """
    longConfig = apolloConfig(env="prod")

    @classmethod
    def getPQVideoListDetail(cls, video_list):
        """
        获取票圈视频详情信息
        :param: video_list: 视频id 列表
        :return: Detail
        """
        url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
        data = {
            "videoIdList": video_list
        }
        header = {
            "Content-Type": "application/json",
        }
        response = requests.request(
            "POST",
            url=url,
            headers=header,
            json=data
        )
        return response.json()

    @classmethod
    def changeVideoStatus(cls, videoId, statusCode=5):
        """
        通过接口，来修改视频状态
        :param videoId:  视频 id
        :param statusCode: (1 审核中，2 不通过 3 待修改，4 自己可见 5 通过) 默认等于 5，把视频设置为可见
        :return: True / False
        """
        url = "https://admin.piaoquantv.com/manager/video/audit/v2/updateAuditStatus"
        payload = "videoId={}&auditStatus={}&updateReasonJson=&rejectReasonJson=%5B%7B%22reason%22%3A%22%E9%95%BF%E6%96%87%E8%87%AA%E5%8A%A8%E4%B8%8B%E6%9E%B6%22%2C%22reasonId%22%3A-1%7D%5D&adminUid=206".format(
            videoId, statusCode)
        headers = {
            'accept': 'application/json',
            'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8',
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
            'cookie': cls.longConfig.getConfigValue("pq_cookie"),
            'origin': 'https://admin.piaoquantv.com',
            'priority': 'u=1, i',
            'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"macOS"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
        }
        response = requests.request(
            "POST",
            url,
            headers=headers,
            data=payload,
            timeout=10
        )
        if response.status_code == 200:
            if response.json().get("code") == 0:
                return True
            else:
                return False
        else:
            return False

    @classmethod
    def publish_to_pq(cls, oss_path, uid, title):
        """
        :return:
        """
        url = "https://vlogapi.piaoquantv.com/longvideoapi/crawler/video/send"
        headers = {
            "User-Agent": "PQSpeed/486 CFNetwork/1410.1 Darwin/22.6.0",
            "cookie": "JSESSIONID=4DEA2B5173BB9A9E82DB772C0ACDBC9F; JSESSIONID=D02C334150025222A0B824A98B539B78",
            "referer": "http://appspeed.piaoquantv.com",
            "token": "524a8bc871dbb0f4d4717895083172ab37c02d2f",
            "accept-language": "zh-CN,zh-Hans;q=0.9",
            "Content-Type": "application/x-www-form-urlencoded",
        }
        payload = {
            "deviceToken": "9ef064f2f7869b3fd67d6141f8a899175dddc91240971172f1f2a662ef891408",
            "fileExtensions": "MP4",
            "loginUid": uid,
            "networkType": "Wi-Fi",
            "platform": "iOS",
            "requestId": "fb972cbd4f390afcfd3da1869cd7d001",
            "sessionId": "362290597725ce1fa870d7be4f46dcc2",
            "subSessionId": "362290597725ce1fa870d7be4f46dcc2",
            "title": title,
            "token": "524a8bc871dbb0f4d4717895083172ab37c02d2f",
            "uid": uid,
            "versionCode": "486",
            "versionName": "3.4.12",
            "videoFromScene": "1",
            "videoPath": oss_path,
            "viewStatus": "1",
            "appType": 888888,
            "repeatStatus": 1
        }
        response = requests.post(
            url=url,
            headers=headers,
            data=payload
        )
        return response

