Browse Source

tmdb api improvements

* sort tmdb results by name
* remove season information in media title before searching
pull/43/head
Jannik 12 months ago
parent
commit
e0a6485ed7
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 4
      app/build.gradle
  2. 66
      app/src/main/java/org/mosad/teapod/util/TMDBApiController.kt

4
app/build.gradle

@ -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()

66
app/src/main/java/org/mosad/teapod/util/TMDBApiController.kt

@ -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)
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")
}
}
}
}
// println(response)
result.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)
@ -96,10 +66,14 @@ class TMDBApiController {
private suspend fun searchMovie(title: String): TMDBResponse = withContext(Dispatchers.IO) {
val url = URL("$searchMovieUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
val response = JsonParser.parseString(url.readText()).asJsonObject
//println(response)
// println(response)
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
getStringNotNull(it.asJsonObject, "name")
}
return@withContext if (response.get("total_results").asInt > 0) {
response.get("results").asJsonArray.first().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)

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close