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' extName = 'mangásPROJECT'
pkgNameSuffix = 'pt.mangasproject' pkgNameSuffix = 'pt.mangasproject'
extClass = '.MangasProjectFactory' extClass = '.MangasProjectFactory'
extVersionCode = 10 extVersionCode = 11
libVersion = '1.2' libVersion = '1.2'
} }

View File

@ -46,11 +46,12 @@ abstract class MangasProject(
.build() .build()
override fun headersBuilder(): Headers.Builder = Headers.Builder() override fun headersBuilder(): Headers.Builder = Headers.Builder()
.add("User-Agent", USER_AGENT)
.add("Referer", baseUrl) .add("Referer", baseUrl)
.add("User-Agent", USER_AGENT)
// Use internal headers to allow "Open in WebView" to work. // Use internal headers to allow "Open in WebView" to work.
private fun sourceHeadersBuilder(): Headers.Builder = headersBuilder() private fun sourceHeadersBuilder(): Headers.Builder = headersBuilder()
.add("Accept", ACCEPT_JSON)
.add("X-Requested-With", "XMLHttpRequest") .add("X-Requested-With", "XMLHttpRequest")
private val sourceHeaders: Headers by lazy { sourceHeadersBuilder().build() } private val sourceHeaders: Headers by lazy { sourceHeadersBuilder().build() }
@ -102,7 +103,12 @@ abstract class MangasProject(
.add("search", query) .add("search", query)
.build() .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 { override fun searchMangaParse(response: Response): MangasPage {
@ -122,8 +128,6 @@ abstract class MangasProject(
title = obj["name"].string title = obj["name"].string
thumbnail_url = obj["cover"].string thumbnail_url = obj["cover"].string
url = obj["link"].string url = obj["link"].string
author = obj["author"].string
artist = obj["artist"].string
} }
override fun mangaDetailsParse(response: Response): SManga { override fun mangaDetailsParse(response: Response): SManga {
@ -153,8 +157,8 @@ abstract class MangasProject(
description = seriesData.select("span.series-desc").text() description = seriesData.select("span.series-desc").text()
status = parseStatus(seriesBlocked, isCompleted) status = parseStatus(seriesBlocked, isCompleted)
author = seriesAuthors[false]?.joinToString("; ") ?: author author = seriesAuthors[false]?.joinToString(", ") ?: author
artist = seriesAuthors[true]?.joinToString("; ") ?: author artist = seriesAuthors[true]?.joinToString(", ") ?: author
genre = seriesData.select("div#series-data ul.tags li") genre = seriesData.select("div#series-data ul.tags li")
.joinToString { it.text() } .joinToString { it.text() }
} }
@ -170,7 +174,7 @@ abstract class MangasProject(
if (manga.status != SManga.LICENSED) if (manga.status != SManga.LICENSED)
return super.fetchChapterList(manga) 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 { override fun chapterListRequest(manga: SManga): Request {
@ -193,7 +197,7 @@ abstract class MangasProject(
if (!result["chapters"]!!.isJsonArray) if (!result["chapters"]!!.isJsonArray)
return emptyList() return emptyList()
val mangaUrl = response.request().header("Referer")!! val mangaUrl = response.request().header("Referer")!!.replace(baseUrl, "")
val mangaId = mangaUrl.substringAfterLast("/") val mangaId = mangaUrl.substringAfterLast("/")
var page = 1 var page = 1
@ -219,7 +223,7 @@ abstract class MangasProject(
SChapter.create().apply { SChapter.create().apply {
name = "Cap. ${obj["number"].string}" + (if (chapterName == "") "" else " - $chapterName") 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 scanlator = release["scanlators"]!!.array
.map { scanObj -> scanObj.obj["name"].string } .map { scanObj -> scanObj.obj["name"].string }
.sorted() .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 { override fun pageListRequest(chapter: SChapter): Request {
val newHeaders = headersBuilder() val newHeaders = headersBuilder()
.set("Referer", baseUrl + chapter.url) .set("Referer", baseUrl + chapter.url)
@ -266,11 +262,10 @@ abstract class MangasProject(
return result return result
val document = result.asJsoup() val document = result.asJsoup()
val token = document.select("script[src*=\"reader.\"]").first() val token = document.select("script[src*=\"reader.\"]").firstOrNull()
?.let { ?.attr("abs:src")
HttpUrl.parse(it.attr("abs:src"))!! ?.let { HttpUrl.parse(it)!!.queryParameter("token") }
.queryParameter("token") ?: throw Exception(TOKEN_NOT_FOUND)
} ?: throw Exception("Não foi possível obter o token de leitura.")
return chain.proceed(pageListApiRequest(request.url().toString(), token)) return chain.proceed(pageListApiRequest(request.url().toString(), token))
} }
@ -296,13 +291,25 @@ abstract class MangasProject(
return GET(page.imageUrl!!, newHeaders) 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 private fun Response.asJsonObject(): JsonObject = JSON_PARSER.parse(body()!!.string()).obj
companion object { 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 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 JSON_PARSER by lazy { JsonParser() }
private val DATE_FORMATTER by lazy { SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH) } 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."
} }
} }