import json
from mq_http_sdk.mq_exception import MQExceptionBase
from mq_http_sdk.mq_producer import TopicMessage
from mq_http_sdk.mq_client import MQClient
from common.common import Common


class MQ:
    instance_id = "MQ_INST_1894469520484605_BXhXuzkZ"

    def __init__(self, topic_name) -> None:
        self.mq_client = MQClient("http://1894469520484605.mqrest.cn-qingdao-public.aliyuncs.com", "LTAI4G7puhXtLyHzHQpD6H7A",
                                  "nEbq3xWNQd1qLpdy2u71qFweHkZjSG")
        self.producer = self.mq_client.get_producer(self.instance_id, topic_name)

    def send_msg(self, video_dict):
        strategy = video_dict["strategy"]
        platform = video_dict["platform"]
        try:
            msg = TopicMessage(json.dumps(video_dict))
            message_key = "{}-{}-{}".format(platform, strategy, video_dict['out_video_id'])
            # msg.set_message_key(platform + "-" + strategy + "-" + video_dict["out_video_id"])
            msg.set_message_key(message_key)
            re_msg = self.producer.publish_message(msg)
            Common.logger(strategy, platform).info("Publish Message Succeed. MessageID:%s, BodyMD5:%s\n" %
                                                   (re_msg.message_id, re_msg.message_body_md5))
        except MQExceptionBase as e:
            Common.logger(strategy, platform).error("Publish Message Fail. Exception:%s\n" % e)
