package com.shsening.autouploadvideos
|
|
|
import android.app.Instrumentation
|
import android.content.Context
|
import android.os.SystemClock
|
import android.os.Handler
|
|
class ActionKuaishou (private var fragment:UploadFragment, private var context:Context, private var threadHandler: Handler, private var videoInfo:Utils.VideoUploadTaskInfo){
|
|
|
private var webView = fragment.webView
|
|
|
var jsCode_CheckContentLoaded = """
|
document.querySelector("#joyride-wrapper") !== null
|
""".trimIndent()
|
var jsCode_CheckVideoUploadFinished = """
|
document.querySelector("#preview-tours > section > div > div:nth-child(3) > div > div:nth-child(2)").innerText =='重新上传'
|
""".trimIndent()
|
var jsCode_PublishVideo_Finished ="document.querySelector('#main-container > div > div > div:nth-child(1) > h2') !== null"
|
|
var jsPath_PublishVideo ="#joyride-wrapper > main > div:nth-child(2) > div:nth-child(2) > div > div:nth-child(6) > div > div"
|
|
|
/**
|
* setting video description information .
|
* Title,description(including # and @ ) publish datetime etc
|
*/
|
private fun updatePostDescription(){
|
|
WebviewUtils.addValueToDiv(webView,"#work-description-edit",videoInfo.description)
|
|
// do not allow download
|
WebviewUtils.simpleElementClick(webView,"#setting-tours > div:nth-child(2) > div > label:nth-child(2)")
|
|
// do not show same city
|
WebviewUtils.simpleElementClick(webView,"#setting-tours > div:nth-child(2) > div > label:nth-child(3)")
|
|
//Scheduled publish
|
WebviewUtils.simpleElementClick(webView,"#setting-tours > div:nth-child(4) > div > div > label:nth-child(2)")
|
|
addValueToDatePickerInput(true,"#setting-tours > div:nth-child(4) > div > div:nth-child(2) > div > div > input",videoInfo.datetime)
|
// 在主线程中设置延迟执行
|
threadHandler.postDelayed(Runnable {
|
startToPublishVideo()
|
}, 3000)
|
}
|
|
|
fun checkIfContentLoaded() {
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_CheckContentLoaded) { result ->
|
if (result == "true") {
|
simulateSwipeAndUploadVideo()
|
} else {
|
webView.postDelayed({
|
checkIfContentLoaded()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
|
private fun startToPublishVideo(){
|
WebviewUtils.simpleElementClick(webView,jsPath_PublishVideo)
|
checkVideoPublishFinished()
|
}
|
private fun checkVideoUploadFinished(){
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_CheckVideoUploadFinished) { result ->
|
if (result == "true") {
|
// 等待视频检测和生成封面
|
threadHandler.postDelayed({updatePostDescription()},3000)
|
|
} else {
|
// 元素不存在,1秒后再次检查
|
webView.postDelayed({
|
checkVideoUploadFinished()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
private fun checkVideoPublishFinished(){
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_PublishVideo_Finished) { result ->
|
if (result == "true") {
|
// video published successfully!!!
|
fragment.startUploadSingleTask()
|
} else {
|
// 元素不存在,1秒后再次检查
|
webView.postDelayed({
|
checkVideoPublishFinished()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
|
|
private fun addValueToDatePickerInput(first:Boolean, jsPath: String, targetValue: String) {
|
|
var datetimes = Utils.validateAndReturnDateTime(targetValue) ?: return
|
|
var dpPanelText = datetimes[0].toString()+"年"+Utils.getMonthAbbreviation(datetimes[1]);
|
|
var dateRowColum = Utils.getDatePosition(datetimes[0],datetimes[1],datetimes[2])?:return;
|
|
val jsCode = """
|
try {
|
var dp = document.querySelector('${jsPath}');
|
dp.dispatchEvent(new MouseEvent('mousedown', {view: window,bubbles: true,cancelable: true}));
|
|
// need time to popup date-picker
|
setTimeout(() => {
|
var datePanel = document.querySelector('.ant-picker-date-panel');
|
var datePanelTitle = datePanel.querySelector('div > div > div');
|
if (datePanelTitle && datePanelTitle.innerText != '${dpPanelText}'){
|
datePanel.querySelector('div >button:nth-child(4)').click();
|
}
|
datePanel.querySelector('div:nth-child(2) > table > tbody > tr:nth-child(${dateRowColum[0]}) > td:nth-child(${dateRowColum[1]})').click()
|
|
//switch to time selection
|
var timePanel= document.querySelector('.ant-picker-time-panel');
|
timePanel.querySelector('div:nth-child(2) > ul:nth-child(1) > li:nth-child(${(datetimes[3]+1)})').click();
|
timePanel.querySelector('div:nth-child(2) > ul:nth-child(2) > li:nth-child(${(datetimes[4]+1)})').click();
|
|
// click ok
|
setTimeout(() => {
|
document.querySelector('.ant-picker-footer > ul > li > button').click();
|
console.log('video publish button has been clicked.');
|
setTimeout(() => {
|
var reConfirmBtn = document.querySelector('#microSupport > div:nth-child(2) > div > div.ant-modal-wrap.ant-modal-confirm-centered.ant-modal-centered > div > div.ant-modal-content > div > div > div.ant-modal-confirm-btns > button.ant-btn.ant-btn-primary');
|
if (reConfirmBtn){
|
reConfirmBtn.click();
|
}
|
}, 500);
|
}, 500);
|
}, 500);
|
} catch (error) {
|
console.error('发生错误:', error);
|
}
|
"""
|
webView.evaluateJavascript(jsCode,null)
|
|
|
}
|
|
private fun simulateSwipeAndUploadVideo() {
|
val instrumentation = Instrumentation()
|
val displayMetrics = context.resources.displayMetrics
|
val width = displayMetrics.widthPixels
|
val height = displayMetrics.heightPixels
|
|
val startX = width.toFloat() * 0.9f // 从右侧滑动
|
val endX = width.toFloat() * 0.3f // 滑动到左侧
|
val y = height / 2f // Y 坐标在屏幕中间
|
|
Thread {
|
Utils.instrumentationSwipe(instrumentation, startX, y, endX, y)
|
SystemClock.sleep(500)
|
Utils.instrumentationClick(instrumentation, width *4/5f, height *2/ 3f)
|
// back to main thread
|
threadHandler.post {
|
checkVideoUploadFinished()
|
}
|
}.start()
|
}
|
|
}
|