Generate a totally random user-agent (#5885)

This commit is contained in:
Victorien Berlot 2021-02-16 20:21:46 +01:00 committed by GitHub
parent 71bb1fc520
commit cf62cb5ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'Scantrad'
pkgNameSuffix = 'fr.scantrad'
extClass = '.Scantrad'
extVersionCode = 13
extVersionCode = 14
libVersion = '1.2'
}

View File

@ -42,17 +42,8 @@ class Scantrad : ParsedHttpSource() {
.addNetworkInterceptor(rateLimitInterceptor)
.build()
protected open val userAgentRandomizer1 = "${Random.nextInt(9).absoluteValue}"
protected open val userAgentRandomizer2 = "${Random.nextInt(10,99).absoluteValue}"
protected open val userAgentRandomizer3 = "${Random.nextInt(100,999).absoluteValue}"
override fun headersBuilder() = Headers.Builder().apply {
add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/8$userAgentRandomizer1.0.4$userAgentRandomizer3.1$userAgentRandomizer2 Safari/537.36"
)
}
override fun headersBuilder(): Headers.Builder = Headers.Builder()
.add("User-Agent", generateRandomUserAgent(Random.nextInt(10, 30).absoluteValue))
// Popular
@ -224,4 +215,13 @@ class Scantrad : ParsedHttpSource() {
override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("Not used")
override fun getFilterList() = FilterList()
// Misc
private fun generateRandomUserAgent(length: Int): String {
val allowedChars: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..length)
.map { allowedChars.random() }
.joinToString("")
}
}