Add Accept header to requests at MangasProject (#3919)

* Add Accept header to requests at MangasProject.

* Change first to firstOrNull.
This commit is contained in:
Alessandro Jean 2020-07-26 23:44:12 -03:00 committed by GitHub
parent d498d0c6aa
commit 351832ff64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 23 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'mangásPROJECT'
pkgNameSuffix = 'pt.mangasproject'
extClass = '.MangasProjectFactory'
extVersionCode = 10
extVersionCode = 11
libVersion = '1.2'
}

View File

@ -46,11 +46,12 @@ abstract class MangasProject(
.build()
override fun headersBuilder(): Headers.Builder = Headers.Builder()
.add("User-Agent", USER_AGENT)
.add("Referer", baseUrl)
.add("User-Agent", USER_AGENT)
// Use internal headers to allow "Open in WebView" to work.
private fun sourceHeadersBuilder(): Headers.Builder = headersBuilder()
.add("Accept", ACCEPT_JSON)
.add("X-Requested-With", "XMLHttpRequest")
private val sourceHeaders: Headers by lazy { sourceHeadersBuilder().build() }
@ -102,7 +103,12 @@ abstract class MangasProject(
.add("search", query)
.build()
return POST("$baseUrl/lib/search/series.json", sourceHeaders, form)
val newHeaders = sourceHeadersBuilder()
.add("Content-Length", form.contentLength().toString())
.add("Content-Type", form.contentType().toString())
.build()
return POST("$baseUrl/lib/search/series.json", newHeaders, form)
}
override fun searchMangaParse(response: Response): MangasPage {
@ -122,8 +128,6 @@ abstract class MangasProject(
title = obj["name"].string
thumbnail_url = obj["cover"].string
url = obj["link"].string
author = obj["author"].string
artist = obj["artist"].string
}
override fun mangaDetailsParse(response: Response): SManga {
@ -153,8 +157,8 @@ abstract class MangasProject(
description = seriesData.select("span.series-desc").text()
status = parseStatus(seriesBlocked, isCompleted)
author = seriesAuthors[false]?.joinToString("; ") ?: author
artist = seriesAuthors[true]?.joinToString("; ") ?: author
author = seriesAuthors[false]?.joinToString(", ") ?: author
artist = seriesAuthors[true]?.joinToString(", ") ?: author
genre = seriesData.select("div#series-data ul.tags li")
.joinToString { it.text() }
}
@ -170,7 +174,7 @@ abstract class MangasProject(
if (manga.status != SManga.LICENSED)
return super.fetchChapterList(manga)
return Observable.error(Exception("Mangá licenciado e removido pela editora."))
return Observable.error(Exception(MANGA_REMOVED))
}
override fun chapterListRequest(manga: SManga): Request {
@ -193,7 +197,7 @@ abstract class MangasProject(
if (!result["chapters"]!!.isJsonArray)
return emptyList()
val mangaUrl = response.request().header("Referer")!!
val mangaUrl = response.request().header("Referer")!!.replace(baseUrl, "")
val mangaId = mangaUrl.substringAfterLast("/")
var page = 1
@ -219,7 +223,7 @@ abstract class MangasProject(
SChapter.create().apply {
name = "Cap. ${obj["number"].string}" + (if (chapterName == "") "" else " - $chapterName")
date_upload = parseChapterDate(obj["date_created"].string.substringBefore("T"))
date_upload = DATE_FORMATTER.tryParseDate(obj["date_created"].string.substringBefore("T"))
scanlator = release["scanlators"]!!.array
.map { scanObj -> scanObj.obj["name"].string }
.sorted()
@ -230,14 +234,6 @@ abstract class MangasProject(
}
}
private fun parseChapterDate(date: String?): Long {
return try {
DATE_FORMATTER.parse(date).time
} catch (e: ParseException) {
0L
}
}
override fun pageListRequest(chapter: SChapter): Request {
val newHeaders = headersBuilder()
.set("Referer", baseUrl + chapter.url)
@ -266,11 +262,10 @@ abstract class MangasProject(
return result
val document = result.asJsoup()
val token = document.select("script[src*=\"reader.\"]").first()
?.let {
HttpUrl.parse(it.attr("abs:src"))!!
.queryParameter("token")
} ?: throw Exception("Não foi possível obter o token de leitura.")
val token = document.select("script[src*=\"reader.\"]").firstOrNull()
?.attr("abs:src")
?.let { HttpUrl.parse(it)!!.queryParameter("token") }
?: throw Exception(TOKEN_NOT_FOUND)
return chain.proceed(pageListApiRequest(request.url().toString(), token))
}
@ -296,13 +291,25 @@ abstract class MangasProject(
return GET(page.imageUrl!!, newHeaders)
}
private fun SimpleDateFormat.tryParseDate(date: String): Long {
return try {
parse(date)?.time ?: 0L
} catch (e: ParseException) {
0L
}
}
private fun Response.asJsonObject(): JsonObject = JSON_PARSER.parse(body()!!.string()).obj
companion object {
private const val ACCEPT_JSON = "application/json, text/javascript, */*; q=0.01"
private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"
private val JSON_PARSER by lazy { JsonParser() }
private val DATE_FORMATTER by lazy { SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH) }
private const val MANGA_REMOVED = "Mangá licenciado e removido pela editora."
private const val TOKEN_NOT_FOUND = "Não foi possível obter o token de leitura."
}
}