Python 为UnityAndroid端自动化接入Tradplus广告SDK

Tradplus介绍

在这里插入图片描述

常规接入

进入Android开发文档

技术文档: https://docs.tradplusad.com/docs/tradplussdk_android_doc_v6/download

选择渠道配置

在这里插入图片描述

生成接入代码

在这里插入图片描述

人工依赖

容易出错 新人入手也比较麻烦
在这里插入图片描述

下载官网同版本的 Unity插件

在这里插入图片描述

使用自动化工具接入

首次 你需要打两个标记来定位

在Traplus接入开头处 加上

    /// TradPlus Start //

在这里插入图片描述
在Tradplus接入结尾处 加上

    /// TradPlus End //

在这里插入图片描述

运行工具 控制台会列出最新的十个Tradplus版本 任选其一

在这里插入图片描述

然后拖入项目路径后回车开始植入最新版

在这里插入图片描述
在这里插入图片描述

植入完成后 控制台会输出 end. 这时候工具运行目录下 会自动下载好指定Tradplus版本的Unity插件

在这里插入图片描述

其它声明

渠道配置

修改配置文件 ads 需要接入的广告渠道配置
仅需要在第一次和每次运营更换广告渠道的时候 配置一次
在这里插入图片描述

ADMOB BIDDING自定义配置

一般情况下这个版本号是Tradplus官方的人来告诉你这是哪个版本号 改一次就好了
在这里插入图片描述

源码


主运行脚本

#默认服务端口

import requests 
import time
import implant
import downloadUnityPlugins

api = 'https://www.geek7.top:8000/api'
adsChannel = ['UnityAds']
region = '2' # 1: 中国  2: 其它地区


adsChannel.clear()
sdkversionList = []
versionstr = ""
def log( msg ):
    print( msg )

def InitChannel():
    adsChannel.clear()
    with open('./bin/ads','r') as fp:
        for line in fp.readlines():
            adsChannel.append(line.replace('\n',''))

def Run( sdk_version = "10.2.0.1",overrid = True ):
    global versionstr
    InitChannel()
    data = {
   
        'token' : time.time(),
        'pipe':'tradplussdk',
        'code':829,
        'adchannels':','.join(adsChannel),
        'version':sdk_version,
        'region' : region
    }
    res = requests.get(api,data) 
    j = res.json()
    if 'version' not in j:
        log('erro: not foud version property in result.data')
        return
    if 'data' not in j:
        log('erro: data not in depends')
        return
    appGradleCode = j['data']
    if None == appGradleCode:
        log('GetDependencies fail')
    else:
        print('input you "Assets\\Plugins\\Android" full path')
        print('example: D:\\Git\\2dtoilet\\2dtoilet-client\\Assets\\Plugins')
        print('Or you can try the Android folder to this window.')
        print('current tradplus version list:')
        v = j['version']
        versionstr = '|'.join(v.split(',')[0:10])
        print(versionstr)
        if not overrid:
            sdkversionList.clear()
            sdkversionList.extend(v.split(','))
            return
        srcpath = input('input proj:\n')
        realpath = srcpath.replace('\\','/')
        implant.Run(realpath,appGradleCode)
        u3dzip = j['u3dzip']
        print(f'download unity plugin: {
     u3dzip}')
        downloadUnityPlugins.dowanlodZip(u3dzip,'./tradplus_unity_plugin_zips')

if __name__ == "__main__":
    log('begin.')
    Run( "10.2.0.1", False )
    
    version = ""
    while True:
        version = input('input you tradplus version: ')
        print(f'pulling the current version dependency of Tradplus: {
     version}')
        if version in sdkversionList:
            break
        else:
            print(f'current version fail. :{
     version}')
            print(versionstr)
    Run( version )
    input('end.')


自动化植入mainTemplate.gradle脚本

import os


def Run( projpath, dependsContent ):
    print(f'project path: {
     projpath}')
    print(f'depend content: {
     dependsContent}')

    if not projpath.endswith('mainTemplate.gradle'):
       projpath = os.path.join(projpath,'mainTemplate.gradle')

    filterstr = ''
    dependlines = dependsContent.splitlines()
    tradplusContent = False
    admob = False
    admob_bidding = False

    admob_bidding_str = "22.1.0.0"
    with open('./bin/ADMOB BIDDING','r') as fp:
        lines = fp.readlines()
        if len(lines) > 0:
            admob_bidding_str = lines[0]
            print(f"ADMOB BIDDING : {
     admob_bidding_str}")

    for l in dependlines:
        if l.startswith('dependencies {'):
            tradplusContent = True
        elif l.startswith('android {'):
            filterstr = filterstr[0:-2]
            break
        elif tradplusContent:
            real = l.replace('\"','\'')
            if admob:
                admob = False
                si = real.find('\'')
                ei = real.find('\'',si+1)
                com = real[si:ei+1]
                _import = '''    implementation(%s) {
        exclude module: "play-services-measurement-sdk-api"
    }\n'''%(com)
                filterstr += _import
                admob_bidding = True
                continue
            if admob_bidding and '// ' in real:
                _import = '''    //ADMOB BIDDING
    implementation ('com.applovin.mediation:google-adapter:%s'){
        exclude module: "play-services-measurement-sdk-api"
    }\n'''%(admob_bidding_str)
                filterstr += _import
                admob_bidding = False
            if '// Admob' in real:
                admob = True

            filterstr += f'{
     real}\n'


    output = ''
    with open(projpath,'r',encoding='UTF-8') as fp:
        begin_write = False
        lines = fp.readlines()
        for line in  lines:
            if line.startswith('    /// TradPlus Start //'):
                output += '    /// TradPlus Start //\n'
                output += filterstr
                begin_write = True
            elif line.startswith('    /// TradPlus End //'):
                output += '    /// TradPlus End //\n'
                begin_write = False
            elif not begin_write:
                output += line

    if len(output) != 0 and output != '':
        print('Under implantation.')
        with open(projpath,'w',encoding='UTF-8') as fp:
            fp.write(output)
        print('Complete implantation.')
    else:
        print('got a little problem')


下载unity最新插件脚本

import requests
import zipfile
import tempfile
import os

def get_data( url ):
    response = requests.get(url)
    return url, response.content

def dowanlodZip( url , dest = "./zip"):
    url, data = get_data( url )  # data为byte字节
    _tmp_file = tempfile.TemporaryFile()  # 创建临时文件
    print(_tmp_file)
 
    _tmp_file.write(data)  # byte字节数据写入临时文件
    # _tmp_file.seek(0)
    
    print(f'extract to: {
     os.path.join(os.getcwd(),dest)}')
    zf = zipfile.ZipFile(_tmp_file, mode='r')
    for names in zf.namelist():
        f = zf.extract(names, dest)  # 解压到zip目录文件下
        print(f)
    zf.close()
    print('extract completed')

 

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-30 10:38:03       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-30 10:38:03       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-30 10:38:03       78 阅读
  4. Python语言-面向对象

    2023-12-30 10:38:03       88 阅读

热门阅读

  1. 蓝桥杯python比赛历届真题99道经典练习题 (8-12)

    2023-12-30 10:38:03       51 阅读
  2. 结构体--高考数组

    2023-12-30 10:38:03       66 阅读
  3. STM32传输FPGA业务

    2023-12-30 10:38:03       56 阅读
  4. Windows下Qt使用MSVC编译出现需要转为unicode的提示

    2023-12-30 10:38:03       53 阅读
  5. Vue - 事件处理详解

    2023-12-30 10:38:03       55 阅读
  6. Docker搭建kafka集群

    2023-12-30 10:38:03       51 阅读