Skip to content

Commit

Permalink
feature: add tv shows (#60)
Browse files Browse the repository at this point in the history
* feature: add tv shows
  • Loading branch information
TrueDevProfile authored Sep 8, 2020
1 parent 9e80f59 commit 3d2d8b7
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 7 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ or pom.xml
build.gradle

dependencies {
implementation 'com.github.TrueDevProfile:kinopoisk-api:0.2.0-beta.8'
implementation 'com.github.TrueDevProfile:kinopoisk-api:0.2.0'
}
or pom.xml

<dependency>
<groupId>com.github.TrueDevProfile</groupId>
<artifactId>kinopoisk-api</artifactId>
<version>0.2.0-beta.8</version>
<version>0.2.0</version>
</dependency>
* After that you can use library's methods. E.g. to get film by kinopoisk id:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.truedev.kinoposk.api.model.deserializer

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
import com.truedev.kinoposk.api.model.tvshow.Episode
import java.time.LocalDate

class StringToEpisodes : JsonDeserializer<List<Episode>>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): List<Episode> {
val node: Any = p.readValueAsTree<JsonNode>()

return (node as ArrayNode).toList()
.map { it.get("episode") }
.map {
Episode(
id = it.get("id").asInt(),
seasonNumber = it.get("seasonNumber").asInt(),
episodeNumber = it.get("episodeNumber").asInt(),
name = it.get("name").asText().ifBlank { null },
nameEng = it.get("nameEng").asText().ifBlank { null },
synopsis = it.get("synopsis")?.asText(),
releaseDate = it.get("releaseDate")
?.let { releaseDate -> LocalDate.parse(releaseDate.asText()) },
dateAccuracy = it.get("dateAccuracy")?.asText()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
data class ContextData(
val id: Int,
val slug: String?,
val title: String,
val originalTitle: String?,
val normalizedTitle: String?,
val year: Int,
val years: List<Year> = listOf(),
val rating: Rating,
val expectations: Expectations,
val currentRating: String,
// todo enum EXPECTATIONS,RATING
val type: String,
val serial: Boolean,
val shortFilm: Boolean,
val completed: Boolean,
val genres: List<Genre> = listOf(),
val countries: List<Country> = listOf(),
val duration: Int,
val trailerId: Int,
val time: Int,
@JsonProperty(value = "fulltime")
val fullTime: Int,
@JsonProperty(value = "formats")
val format: Format
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Country(
val id: Int,
val name: String
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Data.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Data(
val description: String?,
val downloadAvailable: Boolean,
val contextData: ContextData,
val seasons: List<Season> = listOf()
)
16 changes: 16 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Episode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import java.time.LocalDate

@JsonIgnoreProperties(ignoreUnknown = true)
data class Episode(
val id: Int,
val seasonNumber: Int,
val episodeNumber: Int,
val name: String?,
val nameEng: String?,
val synopsis: String?,
val releaseDate: LocalDate?,
val dateAccuracy: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Expectations(
val value: Double?,
val count: Int,
val ready: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Format(
val hasImax: Boolean,
val has3D: Boolean
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Genre.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Genre(
val id: Int,
val name: String,
// translated genre
val slug: String
)
10 changes: 10 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Rating.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Rating(
val value: Double,
val count: Int,
val ready: Boolean = false
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Season.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.truedev.kinoposk.api.model.deserializer.StringToEpisodes
@JsonIgnoreProperties(ignoreUnknown = true)
data class Season(
val number: Int,
@JsonDeserialize(using = StringToEpisodes::class)
val episodes: List<Episode> = listOf()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class TvShowExt(
val success: String,
val data: Data
)
9 changes: 9 additions & 0 deletions src/main/kotlin/com/truedev/kinoposk/api/model/tvshow/Year.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.truedev.kinoposk.api.model.tvshow

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Year(
val start: Int,
val end: Int?
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal class KPApiClientService(private val timeout: Int) {
const val GET_NAVIGATOR_FILTERS = "navigatorFilters"
const val GET_NAVIGATOR = "navigator"
const val GET_DIGITAL = "/k/v1/films/releases/digital"
const val GET_TV_SHOW = "/k/v1/serial/"
}

fun <T> request(url: String, path: String, clazz: Class<T>): ResponseExt<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.truedev.kinoposk.api.model.search.people.SearchPeopleResultExt
import com.truedev.kinoposk.api.model.staff.StaffExt
import com.truedev.kinoposk.api.model.top.TopExt
import com.truedev.kinoposk.api.model.top.Type
import com.truedev.kinoposk.api.model.tvshow.TvShowExt
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_BEST_FILMS_LIST
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_DIGITAL
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_FILM
Expand All @@ -29,6 +30,7 @@ import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_REVIEW_
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_SEARCH_FILM
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_SEARCH_PEOPLE
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_TOP
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.GET_TV_SHOW
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.MAIN_API_URL
import com.truedev.kinoposk.api.service.KPApiClientService.Companion.RELEASE_API_URL
import java.lang.String.valueOf
Expand Down Expand Up @@ -80,7 +82,11 @@ class KinopoiskApiService(timeout: Int = 15000) {
fun getKPReviews(filmId: Int, page: Int = 1): ReviewListExt {
require(filmId > 0) { "Film id should be more than 0" }
require(page > 0) { "Page should be more than 0" }
return kpApiClientService.request(MAIN_API_URL, "$GET_REVIEWS?filmID=$filmId&page=$page", ReviewListExt::class.java)
return kpApiClientService.request(
MAIN_API_URL,
"$GET_REVIEWS?filmID=$filmId&page=$page",
ReviewListExt::class.java
)
.let { ReviewListExt(it.resultCode, it.message, it.response?.data) }
}

Expand Down Expand Up @@ -122,8 +128,10 @@ class KinopoiskApiService(timeout: Int = 15000) {
require(page > 0) { "Page should be more than 0" }
return kpApiClientService.request(
MAIN_API_URL,
"$GET_SEARCH_FILM?keyword=${UrlEscapers.urlFragmentEscaper()
.escape(keyword.replace("[^a-zA-Zа-яА-Я0-9_]".toRegex(), " "))}&page=$page",
"$GET_SEARCH_FILM?keyword=${
UrlEscapers.urlFragmentEscaper()
.escape(keyword.replace("[^a-zA-Zа-яА-Я0-9_]".toRegex(), " "))
}&page=$page",
SearchFimResultExt::class.java
).let { SearchFimResultExt(it.resultCode, it.message, it.response?.data) }
}
Expand All @@ -138,8 +146,10 @@ class KinopoiskApiService(timeout: Int = 15000) {
require(page > 0) { "Page should be more than 0" }
return kpApiClientService.request(
MAIN_API_URL,
"$GET_SEARCH_PEOPLE?keyword=${UrlEscapers.urlFragmentEscaper()
.escape(keyword.replace("[^a-zA-Zа-яА-Я0-9_]".toRegex(), " "))}&page=$page",
"$GET_SEARCH_PEOPLE?keyword=${
UrlEscapers.urlFragmentEscaper()
.escape(keyword.replace("[^a-zA-Zа-яА-Я0-9_]".toRegex(), " "))
}&page=$page",
SearchPeopleResultExt::class.java
).let { SearchPeopleResultExt(it.resultCode, it.message, it.response?.data) }
}
Expand Down Expand Up @@ -246,4 +256,21 @@ class KinopoiskApiService(timeout: Int = 15000) {
)
}
}

/**
* This method retrieves tv show data (series, seasons and so on).
*
*/
fun getTvShowInfo(tvShowId: Int): TvShowExt {
require(tvShowId > 0) { "Tv show id should be more than 0" }
return kpApiClientService.request(
RELEASE_API_URL, "$GET_TV_SHOW$tvShowId",
TvShowExt::class.java
).let {
TvShowExt(
it.response!!.success,
it.response.data
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,11 @@ class KinopoiskApiServiceTest {
assertTrue(kpDigital.data?.items!!.isNotEmpty())
assertTrue(kpDigital.data?.stats?.offset == 0)
}

@Test
fun `test getTvShowInfo will return empty data when id does not exist`() {
val tvShow = kinopoiskApiService.getTvShowInfo(922024)

assertEquals(922024, tvShow.data.contextData.id)
}
}

0 comments on commit 3d2d8b7

Please sign in to comment.