package com.shsening.autouploadvideos
|
|
|
import android.app.Instrumentation
|
import android.content.Context
|
import android.os.SystemClock
|
import android.os.Handler
|
import android.os.Bundle
|
import android.os.Looper
|
import androidx.appcompat.app.AppCompatActivity
|
import com.shsening.autouploadvideos.action.PlatformActionInterface
|
import com.shsening.autouploadvideos.utils.Utils
|
|
class ActionRedbook (private var activity: UploadSessionActivity, private var threadHandler: Handler, private var videoInfo: Utils.VideoUploadTaskInfo):
|
PlatformActionInterface {
|
|
|
private var webView = activity.sessionProfileWebView
|
|
|
var jsCode_CheckContentLoaded = """
|
document.querySelector('input[type="file"][class="upload-input"]') !== null
|
""".trimIndent()
|
var jsCode_CheckVideoUploadFinished = """
|
document.querySelector("#publish-container > div.post-page > div.body > div.content > div.media-area-new > div > div > div.content > div.operator > span:nth-child(1)") !==null
|
""".trimIndent()
|
var jsCode_PublishVideo_Finished ="document.querySelector('#web > div > div > div > div.upload-content > div.tips-wrapper > div:nth-child(3) > div > p:nth-child(3)') !== null"
|
|
var jsPath_PublishVideo ="button.publishBtn"
|
|
|
|
private fun updatePostDescription(){
|
WebviewUtils.addValueToInput(webView,"#publish-container > div > div.body > div.content > div.input.titleInput > div.d-input-wrapper.d-inline-block.c-input_inner > div > input",videoInfo.title)
|
WebviewUtils.addValueToDiv(webView,"#quillEditor > div > p",videoInfo.description)
|
WebviewUtils.simpleElementClick(webView,"#topicBtn")
|
SystemClock.sleep(500)
|
WebviewUtils.simpleElementClick(webView,"#quill-mention-item-0")
|
WebviewUtils.simpleElementClick(webView,"#topicBtn")
|
SystemClock.sleep(500)
|
WebviewUtils.simpleElementClick(webView,"#quill-mention-item-1")
|
|
// WebviewUtils.simpleElementClick(webView,"#el-id-3410-26 > label:nth-child(1)")
|
WebviewUtils.simpleElementClick(webView,"html > body > div:nth-of-type(1) > div > div:nth-of-type(2) > div > div:nth-of-type(2) > main > div:nth-of-type(3) > div > div > div:nth-of-type(1) > div > div > div > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(10) > div > div:nth-of-type(5) > div:nth-of-type(2) > label:nth-of-type(2)")
|
addValueToDatePickerInput(true,"html > body > div:nth-of-type(1) > div > div:nth-of-type(2) > div > div:nth-of-type(2) > main > div:nth-of-type(3) > div > div > div:nth-of-type(1) > div > div > div > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(10) > div > div:nth-of-type(5) > div:nth-of-type(2) > div > div:nth-of-type(1) > div",videoInfo.datetime)
|
// 在主线程中设置延迟执行
|
threadHandler.postDelayed(Runnable {
|
startToPublishVideo()
|
}, 5000)
|
}
|
|
|
override 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!!!
|
activity.checkAndStartNextTask()
|
|
|
} 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()+"年 "+datetimes[1].toString()+"月";
|
|
var dateRowColum = Utils.getDatePosition(datetimes[0],datetimes[1],datetimes[2])?:return;
|
|
val jsCode = """
|
try {
|
var dp = document.querySelector('${jsPath}');
|
dp.focus(); // 确保输入框获得焦点
|
dp.click();
|
|
// need time to popup date-picker
|
setTimeout(() => {
|
var dpPanel = document.querySelector('.semi-popover-content');
|
var dpPanelSpan = dpPanel.querySelector('div > div > div > div:nth-child(1) > div.semi-datepicker-navigation > div > button > span > span');
|
if (dpPanelSpan && dpPanelSpan.textContent != '${dpPanelText}'){
|
dpPanel.querySelector('div > div > div > div > div:nth-child(1) > div.semi-datepicker-navigation > button:nth-child(3)').click();
|
}
|
dpPanel.querySelector('div > div > div > div > div:nth-child(1) > div.semi-datepicker-month > div.semi-datepicker-weeks > div:nth-child(${dateRowColum[0]}) > div:nth-child(${dateRowColum[1]})').click()
|
|
//switch to time selection
|
dpPanel.querySelector('div > div > div > div > div.semi-datepicker-switch > div.semi-datepicker-switch-time').click();
|
setTimeout(() => {
|
dpPanel.querySelector('div > div > div > div.semi-datepicker-tpk > div > div > div.semi-scrolllist-body > div.semi-scrolllist-item-wheel.undefined-list-hour > div.semi-scrolllist-list-outer > ul > li:nth-child(${(datetimes[3]+1)})').click();
|
dpPanel.querySelector('div > div > div > div.semi-datepicker-tpk > div > div > div.semi-scrolllist-body > div.semi-scrolllist-item-wheel.undefined-list-minute > div.semi-scrolllist-list-outer > ul > li:nth-child(${(datetimes[4]+1)})').click();
|
}, 500);
|
}, 500);
|
} catch (error) {
|
console.error('发生错误:', error);
|
}
|
"""
|
webView.evaluateJavascript(jsCode,null)
|
|
|
}
|
|
|
private fun simulateSwipeAndUploadVideo() {
|
val instrumentation = Instrumentation()
|
val displayMetrics = activity.resources.displayMetrics
|
val width = displayMetrics.widthPixels
|
val height = displayMetrics.heightPixels
|
|
val startX = width.toFloat() * 0.9f // 从右侧滑动
|
val endX = width.toFloat() * 0.1f // 滑动到左侧
|
val y = height / 3f // Y 坐标在屏幕中间
|
|
Thread {
|
Utils.instrumentationSwipe(instrumentation, startX, y, endX, y)
|
SystemClock.sleep(500)
|
Utils.instrumentationSwipe(instrumentation, startX, y, endX, y)
|
SystemClock.sleep(500)
|
Utils.instrumentationClick(instrumentation, width / 2f, height * 2 / 4f)
|
// back to main thread
|
threadHandler.post {
|
checkVideoUploadFinished()
|
}
|
}.start()
|
}
|
|
}
|