import requests
import json
import time
import random

headers = {'Content-Type': 'application/json'}

URL = 'http://52.80.137.106:8080/receive/tkio/'
# URL = 'http://log.trackingio.com/receive/tkio/'
# URL = 'http://120.53.212.71/receive/tkio/'
# URL = 'http://tlog.trackingio.com/receive/tkio/'
# URL = 'http://jisulog.sortda.com/receive/tkio/'


# 时间戳
current_time_13 = int(round(time.time() * 1000))
# 时间和日期
when = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# 日期
ds = time.strftime('%Y-%m-%d', time.localtime(time.time()))
# 设备品牌
manufacturer = 'vivo'
# 设备系统
ryos = 'android'
# 设备类型
rydevicetype = 'vivox9'
# 网络类型
network = 'wifi'
# 分辨率
resolution = '1344*750'
# 运行商
op = '中国移动'
# 支付类型   支付宝(alipay),银联(unionpay),微信支付(weixinpay),易宝支付(yeepay)
paymenttype = 'alipay'
# 货币类型
currencytype = 'CNY'
# 支付的货币金额
currencyamount = '45'
when1 = '2020-06-04 10:00:00'
# (设备型号)
model = ''
# (操作系统版本)
ryosversion = ''
carrier = '中国联通'
lv = '1.5.0'


class allEvent:
    # 激活
    def installUpload(self, i, IP, appkey, cmpaignid):
        s = json.dumps({
            # "what":'install',
            "appid": appkey,
            "context": {
                "_apptype": "quickApp",

                '_category': 'APPMARKET',
                "_campaignid": cmpaignid,
                # "_channelid": cmpaignid,
                "_deviceid": i,
                "_imei": i,
                "_androidid": i,
                "_ip": IP,
                '_manufacturer': manufacturer,
                '_ryos': ryos,
                '_rydevicetype': rydevicetype,
                '_network': network,
                '_resolution': resolution,
                '_op': op,
                '_timestamp': current_time_13,
                'uniqueName': '2221',
                "_model": model,
                "_ryosversion": ryosversion,
                # "mobdna_info": "{\"mobdna_oid\":\"95820f88217ec8cbdf621919bb81c0a5\",\"mobdna_appkey\":\"16dc0cb882d3dbec4f7f66251a84d6dd\",\"mobdna_rdid\":\"60AF84954A2AF80A07249708703519CB\",\"mobdna_requestid\":\"463cfd5564d24d30a607a18a1e8ae2d8\"}",

                # 'adgroupId': '324322'

                # "_manufacturer": "OPPO",
                # "_oaid_limited": "false",
                # "_meid": "unknown",
                # "_network_type": "4G",
                # "_istablet": "phone",
                # "_timestamp": "1589152752083",
                # "_imei2": "unknown",
                # "_ipv6": "2409:896a:3850:c1e0:2:1:d3ac:8c80",
                # "_tz": "+8",
                # "_ry_ts": "1589152758942",
                # "_pkgname": "com.qmhd.game.gwgxq",
                # "_audio_volume": "0",
                # "_app_version": "1.0.5",
                # "_model": "OPPO A83",
                # "_ip": IP,
                # "_ucid": "WsfjVbhuJTMDAHekTHFuIZ7c",
                # "_ukid": "unknown",
                # "_lib": "Android",
                # "_androidid": "d24a4684afe476e",
                # "_ryandroid8version": "25",
                # "_mac": "9C:0C:DF:86:44:B1",
                # "_ryos": "Android",
                # "_oaid": "unknown",
                # "_bd_vid": "unknown",
                # "_create_timestamp": "1589152751924",
                # "_ryosversion": "7.1.1",
                # "_imei": i,
                # "_resolution": "720*1360",
                # "_lib_version": "1.5.4",
                # "_deviceid": i,
                # "_campaignid": cmpaignid,
                # "_rydevicetype": "OPPO A83",
                # "_carrier": "unknown",
                # "_screen_brightness": "255"

            },
            'when': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
            # time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
            'ds': ds,
            'who': 'lxx_test'

        })
        p = requests.post(url=URL + 'install', data=s, headers=headers, timeout=5)
        print(p.text)
        print(when)

    # 注册
    def registerEvent(self, i, IP, appkey):
        s = json.dumps({
            'what': 'register',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13

            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL + 'register', data=s, headers=headers, timeout=5)
        print(p.text)

    # 登陆
    def loginEvent(self, i, IP, appkey):
        s = json.dumps({
            'what': 'loggedin',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13
            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL + 'loggedin', data=s, headers=headers, timeout=5)
        print(p.text)

    # 付费

    def payEvent(self, i, IP, appkey):
        # 订单号
        transactionid = ''.join(str(random.choice(range(10))) for _ in range(19))
        s = json.dumps({
            'what': 'payment',
            'appid': appkey,
            'context': {
                "_apptype": "quickApp",

                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_transactionid': transactionid,
                '_paymenttype': paymenttype,
                '_currencytype': currencytype,
                '_currencyamount': currencyamount,

            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL + 'payment', data=s, headers=headers, timeout=5)
        print(p.text)

        # 订单

    def orderEvent(self, i, IP, appkey):
        # 订单号
        transactionid = ''.join(str(random.choice(range(10))) for _ in range(19))
        s = json.dumps({
            'what': 'order',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_transactionid': transactionid,
                '_paymenttype': paymenttype,
                '_currencytype': currencytype,
                '_currencyamount': currencyamount,

            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL + 'order', data=s, headers=headers, timeout=5)
        print(p.text)

    # 自定义事件
    def ziding_event(self, i, IP, appkey, event):
        s = json.dumps({
            'what': event,
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                # 识别是否为自定义事件,默认传1
                '_isreyundefaultevent': '1'

            },
            'when': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL + 'event', data=s, headers=headers, timeout=5)
        print(p.text)

    # 自定义事件
    def invoke_test(self, i, IP, appkey, cmpaignid):
        s = json.dumps({
            "what": "invoke",
            "appType": "",
            "appid": appkey,
            "context": {
                "_campaignid": cmpaignid,
                "_rydevicetype": "iPhone",
                "_ip": IP,
                "_ryosversion": ryosversion,
                "_manufacturer": "苹果",
                "frequency": "0.000",
                "_app_version": "2.0",
                "_isreyundefaultevent": "1",
                "_deviceid": i,
                "_device_gps": "unknown",
                "_jbk": "0",
                "_timestamp": "1565941809351",
                "_imei": i,
                # "_idfa": "DBC93722-E34B-440C-B7A7-FB6641D0D9AB",
                "_idfv": i,
                "_lib_version": lv,
                "_carrier": carrier,
                "_ry_ts": "1565941809416",
                "_create_timestamp": "1565941809351",
                "_tz": "+8",
                "_pkgname": "com.reyun.sdktrackingtest",
                "_ipv6": "fe80::1cf8:ea60:3508:1dc2",
                "modify": "0",
                "_ry_origin_when": "2019-08-16 15:50:09",
                "_resolution": "736*414",
                "_ryos": "ios",
                "_model": model,
                "_mac": "02:00:00:00:00:00",
                "_network_type": "WIFI",
                "_lats": "1"
            },
            "where": "event",
            "when": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
            "ds": when,
            "who": "unknown"
        })
        p = requests.post(url=URL + 'event', data=s, headers=headers, timeout=5)
        print("自定义" + p.text)