copymanga: add a preference to change image cdn (#7384)

* copymanga: add a preference to change image cdn

Closed #6649

* copymanga: fix typo
This commit is contained in:
zhongfly 2021-06-02 21:57:45 +08:00 committed by GitHub
parent e8b6a225aa
commit 7cc0764041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 8 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'CopyManga'
pkgNameSuffix = 'zh.copymanga'
extClass = '.CopyManga'
extVersionCode = 8
extVersionCode = 9
libVersion = '1.2'
}
apply from: "$rootDir/common.gradle"

View File

@ -41,6 +41,10 @@ class CopyManga : ConfigurableSource, HttpSource() {
override val supportsLatest = true
private val popularLatestPageSize = 50 // default
private val searchPageSize = 12 // default
val replaceWith277 = Regex("mirror277")
val replaceWith77 = Regex("mirror77")
private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
@ -70,9 +74,9 @@ class CopyManga : ConfigurableSource, HttpSource() {
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
// when perform html search, sort by popular
var apiUrlString = "$baseUrl/api/kb/web/search/comics?limit=$searchPageSize&offset=${(page - 1) * searchPageSize}&platform=2&q=$query&q_type="
var htmlUrlString = "$baseUrl/comics?offset=${(page - 1) * popularLatestPageSize}&limit=$popularLatestPageSize"
var requestUrlString: String
val apiUrlString = "$baseUrl/api/kb/web/search/comics?limit=$searchPageSize&offset=${(page - 1) * searchPageSize}&platform=2&q=$query&q_type="
val htmlUrlString = "$baseUrl/comics?offset=${(page - 1) * popularLatestPageSize}&limit=$popularLatestPageSize"
val requestUrlString: String
val params = filters.map {
if (it is MangaFilter) {
@ -107,7 +111,12 @@ class CopyManga : ConfigurableSource, HttpSource() {
}
val manga = SManga.create().apply {
title = _title
thumbnail_url = document.select("div.comicParticulars-title-left img").first().attr("data-src")
var picture = document.select("div.comicParticulars-title-left img").first().attr("data-src")
if (!preferences.getBoolean(change_cdn_tomainland, false)) {
picture = replaceWith277.replace(picture, "mirror2")
picture = replaceWith77.replace(picture, "mirror")
}
thumbnail_url = picture
description = document.select("div.comicParticulars-synopsis p.intro").first().text().trim()
}
@ -187,7 +196,12 @@ class CopyManga : ConfigurableSource, HttpSource() {
val ret = ArrayList<Page>(pageArray.length())
for (i in 0 until pageArray.length()) {
ret.add(Page(i, "", pageArray.getJSONObject(i).getString("url")))
var page = pageArray.getJSONObject(i).getString("url")
if (!preferences.getBoolean(change_cdn_tomainland, false)) {
page = replaceWith277.replace(page, "mirror2")
page = replaceWith77.replace(page, "mirror")
}
ret.add(Page(i, "", page))
}
return ret
@ -324,7 +338,12 @@ class CopyManga : ConfigurableSource, HttpSource() {
ret.add(
SManga.create().apply {
title = _title
thumbnail_url = obj.getString("cover")
var picture = obj.getString("cover")
if (!preferences.getBoolean(change_cdn_tomainland, false)) {
picture = replaceWith277.replace(picture, "mirror2")
picture = replaceWith77.replace(picture, "mirror")
}
thumbnail_url = picture
author = Array<String?>(authorArray.length()) { i -> authorArray.getJSONObject(i).getString("name") }.joinToString(", ")
status = SManga.UNKNOWN
url = "/comic/${obj.getString("path_word")}"
@ -338,7 +357,12 @@ class CopyManga : ConfigurableSource, HttpSource() {
private fun mangaFromPage(element: Element): SManga {
val manga = SManga.create()
element.select("div.exemptComicItem-img > a > img").first().let {
manga.thumbnail_url = it.attr("data-src")
var picture = it.attr("data-src")
if (!preferences.getBoolean(change_cdn_tomainland, false)) {
picture = replaceWith277.replace(picture, "mirror2")
picture = replaceWith77.replace(picture, "mirror")
}
manga.thumbnail_url = picture
}
element.select("div.exemptComicItem-txt > a").first().let {
manga.setUrlWithoutDomain(it.attr("href"))
@ -410,10 +434,27 @@ class CopyManga : ConfigurableSource, HttpSource() {
}
}
}
val cdnPreference = androidx.preference.CheckBoxPreference(screen.context).apply {
key = change_cdn_tomainland
title = "转换图片CDN为国内"
summary = "加载图片使用国内CDN,可以使用代理的情况下请不要打开此选项"
setOnPreferenceChangeListener { _, newValue ->
try {
val setting = preferences.edit().putBoolean(change_cdn_tomainland, newValue as Boolean).commit()
setting
} catch (e: Exception) {
e.printStackTrace()
false
}
}
}
screen.addPreference(zhPreference)
screen.addPreference(cdnPreference)
}
companion object {
private const val SHOW_Simplified_Chinese_TITLE_PREF = "showSCTitle"
private const val change_cdn_tomainland = "changeCDN"
}
}