tmdb api improvements

* sort tmdb results by name
* remove season information in media title before searching
This commit is contained in:
Jannik 2021-07-03 15:51:52 +02:00
parent 5555269877
commit e0a6485ed7
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 21 additions and 47 deletions

View File

@ -10,8 +10,8 @@ android {
applicationId "org.mosad.teapod"
minSdkVersion 23
targetSdkVersion 30
versionCode 4180 //00.04.100
versionName "0.4.2-alpha2"
versionCode 4190 //00.04.190
versionName "0.4.2-beta1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resValue "string", "build_time", buildTime()

View File

@ -4,9 +4,9 @@ import android.util.Log
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import kotlinx.coroutines.*
import org.mosad.teapod.util.DataTypes.MediaType
import java.net.URL
import java.net.URLEncoder
import org.mosad.teapod.util.DataTypes.MediaType
class TMDBApiController {
@ -21,7 +21,11 @@ class TMDBApiController {
private val imageUrl = "https://image.tmdb.org/t/p/w500"
suspend fun search(title: String, type: MediaType): TMDBResponse {
val searchTerm = title.replace("(Sub)", "").trim()
// remove unneeded text from the media title before searching
val searchTerm = title.replace("(Sub)", "")
.replace(Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)"), "")
.replace(Regex("(Staffel|Season)\\s?[0-9]+"), "")
.trim()
return when (type) {
MediaType.MOVIE -> searchMovie(searchTerm)
@ -38,48 +42,14 @@ class TMDBApiController {
private suspend fun searchTVShow(title: String): TMDBResponse = withContext(Dispatchers.IO) {
val url = URL("$searchTVUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
val response = JsonParser.parseString(url.readText()).asJsonObject
println(url)
println(response)
// println(response)
return@withContext if (response.get("total_results").asInt > 0) {
val results = response.get("results").asJsonArray
var result = results.asJsonArray.first()
var bestMatch = Int.MAX_VALUE
results.forEach { it ->
val name = getStringNotNull(it.asJsonObject, "name")
val rating = name.compareTo(title)
println("name: $name, title: $title")
when {
rating < 0 -> {
println("rating < ($rating): $it")
if ((rating * -1) < bestMatch) {
bestMatch = (rating * -1)
result = it
println("best < ($bestMatch): $it")
}
}
rating == 0 -> {
println("rating == ($rating): $it")
bestMatch = 0
result = it
println("best == ($bestMatch): $it")
}
else -> {
println("rating > ($rating): $it")
if (rating < bestMatch) {
bestMatch = rating
result = it
println("best > ($bestMatch): $it")
}
}
}
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
getStringNotNull(it.asJsonObject, "name")
}
result.asJsonObject.let {
return@withContext if (sortedResults.isNotEmpty()) {
sortedResults.first().asJsonObject.let {
val id = getStringNotNull(it, "id").toInt()
val overview = getStringNotNull(it, "overview")
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
@ -98,8 +68,12 @@ class TMDBApiController {
val response = JsonParser.parseString(url.readText()).asJsonObject
// println(response)
return@withContext if (response.get("total_results").asInt > 0) {
response.get("results").asJsonArray.first().asJsonObject.let {
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
getStringNotNull(it.asJsonObject, "name")
}
return@withContext if (sortedResults.isNotEmpty()) {
sortedResults.first().asJsonObject.let {
val id = getStringNotNull(it,"id").toInt()
val overview = getStringNotNull(it,"overview")
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)