Skip to content

Commit

Permalink
fix: firefox drive
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jul 27, 2021
1 parent b61e21b commit ec1870e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ val MEDIA_REGEX = """(?<=bilibili\.com/bangumi/media/md)(\d+)""".toRegex()

### SeleniumConfig.yml

1. `device_name` 截图设备类型,默认 `iPad`
1. `ua` 截图设备UA
1. `width` 截图宽度
1. `height` 截图高度
1. `setup` 是否启用截图,默认 `true`

## 安装
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object BiliHelperPlugin : KotlinPlugin(
BiliSearchCommand.register()

if (selenium) {
driver = RemoteWebDriver()
driver = RemoteWebDriver(ua = SeleniumToolConfig.ua)
}

BiliTasker.startAll()
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ internal val SetupSelenium by BiliHelperPlugin::selenium

internal val RemoteWebDriver by BiliHelperPlugin::driver

internal val DeviceName by lazy { SeleniumToolConfig.device }

enum class CacheType : Mutex by Mutex() {
DYNAMIC,
VIDEO,
Expand Down Expand Up @@ -111,7 +109,11 @@ private suspend fun Url.screenshot(type: CacheType, path: String, refresh: Boole
if (exists().not() || refresh) {
parentFile.mkdirs()
runCatching {
RemoteWebDriver.getScreenshot(this@screenshot.toString())
RemoteWebDriver.getScreenshot(
url = this@screenshot.toString(),
width = SeleniumToolConfig.width,
height = SeleniumToolConfig.height
)
}.onFailure {
logger.warning({ "使用SeleniumTool失败" }, it)
}.getOrThrow().let {
Expand Down
14 changes: 10 additions & 4 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/data/SeleniumToolConfig.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package xyz.cssxsh.mirai.plugin.data

import net.mamoe.mirai.console.data.ReadOnlyPluginConfig
import net.mamoe.mirai.console.data.ValueName
import net.mamoe.mirai.console.data.ValueDescription
import net.mamoe.mirai.console.data.value

object SeleniumToolConfig : ReadOnlyPluginConfig("SeleniumConfig") {
@ValueName("device_name")
val device: String by value("iPad")
@ValueName("setup")
private const val IPAD = "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"

@ValueDescription("截图UA")
val ua: String by value(IPAD)
@ValueDescription("截图宽度")
val width: Int by value(768)
@ValueDescription("截图高度")
val height: Int by value(1024)
@ValueDescription("是否截图模式")
val setup: Boolean by value(true)
}
24 changes: 7 additions & 17 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/tools/SeleniumTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import io.github.karlatemp.mxlib.logger.NopLogger
import io.github.karlatemp.mxlib.selenium.MxSelenium
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout
import org.openqa.selenium.Capabilities
import org.openqa.selenium.OutputType
import org.openqa.selenium.PageLoadStrategy
import org.openqa.selenium.WindowType
import org.openqa.selenium.*
import org.openqa.selenium.chromium.ChromiumOptions
import org.openqa.selenium.firefox.FirefoxDriverLogLevel
import org.openqa.selenium.firefox.FirefoxOptions
import org.openqa.selenium.remote.ProtocolHandshake
import org.openqa.selenium.remote.RemoteWebDriver
import xyz.cssxsh.mirai.plugin.*
import java.io.File
import java.time.Duration
import java.util.logging.Level
Expand Down Expand Up @@ -60,21 +56,14 @@ internal val DriverConsumer: (Capabilities) -> Unit = { capabilities ->
"excludeSwitches",
listOf("enable-automation", "ignore-certificate-errors")
)
if (DeviceName.isNotBlank()) {
setExperimentalOption("mobileEmulation", mapOf("deviceName" to DeviceName))
}
}
is FirefoxOptions -> capabilities.apply {
setHeadless(true)
setPageLoadStrategy(PageLoadStrategy.NORMAL)
setLogLevel(FirefoxDriverLogLevel.FATAL)
setAcceptInsecureCerts(true)
if (DeviceName.isNotBlank()) {
setCapability(
FirefoxOptions.FIREFOX_OPTIONS,
mapOf("mobileEmulation" to mapOf("deviceName" to DeviceName))
)
}
// XXX 手动关闭 webgl
addPreference("webgl.disabled", true)
}
else -> throw IllegalArgumentException("未设置参数的浏览器")
}
Expand All @@ -92,15 +81,15 @@ private val Interval = Duration.ofSeconds(3)

private const val HOME_PAGE = "https://t.bilibili.com/h5/dynamic/detail/508396365455813655"

fun RemoteWebDriver(home: String = HOME_PAGE): RemoteWebDriver {
fun RemoteWebDriver(ua: String? = null, home: String = HOME_PAGE): RemoteWebDriver {

val thread = Thread.currentThread()
val oc = thread.contextClassLoader

val driver = runCatching {
thread.contextClassLoader = KtorHttpClientFactory::class.java.classLoader

MxSelenium.newDriver(null, DriverConsumer).apply {
MxSelenium.newDriver(ua, DriverConsumer).apply {
// 诡异的等级
setLogLevel(Level.ALL)
manage().timeouts().apply {
Expand All @@ -116,9 +105,10 @@ fun RemoteWebDriver(home: String = HOME_PAGE): RemoteWebDriver {
return driver.getOrThrow()
}

suspend fun RemoteWebDriver.getScreenshot(url: String): ByteArray {
suspend fun RemoteWebDriver.getScreenshot(url: String, width: Int, height: Int): ByteArray {
val pre = windowHandle
val new = switchTo().newWindow(WindowType.TAB)
manage().window().size = Dimension(width, height)
new.get(url)
runCatching {
withTimeout(Timeout.toMillis()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class SeleniumToolTest {
private val dir = File("./test")

init {
System.setProperty("mxlib.selenium.browser", "firefox")
setupSelenium(dir)
}

Expand All @@ -19,14 +20,16 @@ internal class SeleniumToolTest {
468989129984490798
)

private fun dynamic(id: Long) = "https://t.bilibili.com/h5/dynamic/detail/$id"
private fun dynamic(id: Long) = "https://t.bilibili.com/$id"

private val ipad = "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"

@Test
fun getScreenShot(): Unit = runBlocking {
val driver = RemoteWebDriver()
val driver = RemoteWebDriver(ua = ipad)
driver.get("https://t.bilibili.com/h5/dynamic/detail/508396365455813655")
list.forEach { id ->
driver.getScreenshot(dynamic(id)).let {
driver.getScreenshot(url = dynamic(id), width = 768, height = 1024).let {
dir.resolve("${id}.png").writeBytes(it)
}
}
Expand Down

0 comments on commit ec1870e

Please sign in to comment.