#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: AnsjerFormal
@software: PyCharm
@DATE: 2019/6/1 17:25
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: ETkObject.py
@Contact: chanjunkai@163.com
"""

import base64
import urllib.parse
from random import Random


class ETkObject(object):
    def __init__(self, etk=''):
        self.uid = ''
        self.parseUid(etk)

    def parseUid(self, etk):
        try:
            c = base64.b64decode(etk)
            c = c[2:-2]
            c = urllib.parse.unquote(c.decode('utf-8'))
            c = base64.b64decode(c)
            uid = c.decode('utf-8')
            if len(uid) == 20:
                self.uid = uid
                print(uid)
            elif len(uid) == 14:
                self.uid = uid
                print(uid)
        except Exception as e:
            print(repr(e))

    def encrypt(self,data):
        s = data.encode()
        s = base64.b64encode(s)
        startStr = self.randomParam()
        endStr = self.randomParam()
        s = '{startStr}{s}{endStr}'.format(startStr=startStr,s=s.decode('utf-8'),endStr=endStr)
        s = base64.b64encode(s.encode())
        s=s.decode('utf-8')
        return s

    def randomParam(self):
        characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
                       'tUuVvWwXxYyZz0123456789'
        length = len(characterSet) - 1

        random = Random()
        ss = ''
        for index in range(2):
            ss += characterSet[random.randint(0, length)]
        return ss


# etkObj = ETkObject(etk='')
# PP = etkObj.encrypt('jLBDSU9547NTRSMF111A')
# print('encode_data:')
# print(PP)
# print('decode_data:')
# eobj = ETkObject(etk=PP)
# uid = eobj.uid
# print(uid)

