tmdb api improvements #43

Merged
Seil0 merged 3 commits from fix/sort_tmdb_results into develop 1 year ago
  1. 4
      app/build.gradle
  2. 6
      app/src/main/java/org/mosad/teapod/ui/activity/main/MainActivity.kt
  3. 28
      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()

6
app/src/main/java/org/mosad/teapod/ui/activity/main/MainActivity.kt

@ -31,7 +31,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onDismiss
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import kotlinx.coroutines.*
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ActivityMainBinding
@ -51,7 +51,7 @@ import org.mosad.teapod.util.exitAndRemoveTask
import java.net.SocketTimeoutException
import kotlin.system.measureTimeMillis
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
private lateinit var binding: ActivityMainBinding
private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
@ -72,7 +72,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
theme.applyStyle(getThemeResource(), true)
binding = ActivityMainBinding.inflate(layoutInflater)
binding.navView.setOnNavigationItemSelectedListener(this)
binding.navView.setOnItemSelectedListener(this)
setContentView(binding.root)
supportFragmentManager.commit {

28
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,10 +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(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)
@ -58,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