diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt new file mode 100644 index 000000000..e8c064f7c --- /dev/null +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -0,0 +1,47 @@ +package io.legado.app.help.storage + +import io.legado.app.utils.GSON +import java.util.regex.Pattern + +object OldRule { + private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) + private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE) + + fun toNewUrl(oldUrl: String): String { + val map = HashMap() + var url = oldUrl + var mather = headerPattern.matcher(url) + if (mather.find()) { + val header = mather.group() + url = url.replace(header, "") + map["headers"] = header.substring(8) + } + var urlList = url.split("|") + url = urlList[0] + if (urlList.size > 1) { + map["charset"] = urlList[1].split("=")[1] + } + mather = jsPattern.matcher(url) + val jsList = arrayListOf() + while (mather.find()) { + jsList.add(mather.group()) + url = url.replace(jsList.last(), "$${jsList.size - 1}") + } + url = url.replace("{", "<").replace("}", ">") + url = url.replace("searchKey", "{{key}}") + url = url.replace("searchPage", "{{page}}") + for ((index, item) in jsList.withIndex()) { + url = url.replace("$$index", item.replace("searchKey", "key").replace("searchPage", "page")) + } + urlList = url.split("@") + url = urlList[0] + if (urlList.size > 1) { + map["method"] = "POST" + map["body"] = urlList[1] + } + if (map.size > 0) { + url += "," + GSON.toJson(map) + } + return url + } +} \ No newline at end of file