Python 为UnityAndroid端自动化接入Tradplus广告SDK
2023-12-29 16:59:47
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')
文章来源:https://blog.csdn.net/qq_39162566/article/details/135291907
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!