Skip to content

Commit

Permalink
Add episode logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Dec 8, 2023
1 parent 56ffc1d commit 6497fb2
Show file tree
Hide file tree
Showing 25 changed files with 594 additions and 268 deletions.
2 changes: 1 addition & 1 deletion hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<property name="connection.url">jdbc:postgresql://localhost:5432/shikkanime</property>
<property name="connection.username">postgres</property>
<property name="connection.password">mysecretpassword</property>
<property name="show_sql">false</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
</session-factory>
</hibernate-configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ data class FromToZonedDateTimeKeyCache(
result = 31 * result + to.hashCode()
return result
}

override fun toString(): String {
return "FromToZonedDateTimeKeyCache(from=$from, to=$to)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AdminController {
@Path("/platforms")
@Post
private fun postPlatform(@BodyParam parameters: Parameters): Response {
println(parameters)
val redirectResponse = RedirectResponse("/admin/platforms")

val platformName = parameters["platform"] ?: return redirectResponse
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/Anime.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.shikkanime.entities

import jakarta.persistence.*
import java.time.ZonedDateTime
import java.util.*

@Entity
@Table(name = "anime")
data class Anime(
override val uuid: UUID? = null,
@ManyToOne(optional = false, fetch = FetchType.LAZY)
val country: Country? = null,
@Column(nullable = false)
val name: String? = null,
@Column(nullable = false, name = "release_date")
val releaseDate: ZonedDateTime = ZonedDateTime.now(),
@Column(nullable = false)
var image: String? = null,
@Column(nullable = true, columnDefinition = "TEXT")
var description: String? = null,
) : ShikkEntity(uuid)
34 changes: 2 additions & 32 deletions src/main/kotlin/fr/shikkanime/entities/Country.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,15 @@ package fr.shikkanime.entities

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Index
import jakarta.persistence.Table
import java.util.*

@Entity
@Table(
name = "country",
indexes = [
Index(name = "idx_country_country_code", columnList = "country_code")
]
)
@Table(name = "country")
data class Country(
override val uuid: UUID? = null,
@Column(nullable = false, unique = true)
val name: String? = null,
@Column(nullable = false, unique = true, name = "country_code")
val countryCode: String? = null,
) : ShikkEntity(uuid) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Country) return false
if (!super.equals(other)) return false

if (uuid != other.uuid) return false
if (name != other.name) return false
if (countryCode != other.countryCode) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + (uuid?.hashCode() ?: 0)
result = 31 * result + (name?.hashCode() ?: 0)
result = 31 * result + (countryCode?.hashCode() ?: 0)
return result
}

override fun toString(): String {
return "Country(uuid=$uuid, name=$name, countryCode=$countryCode)"
}
}
) : ShikkEntity(uuid)
39 changes: 39 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/Episode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.shikkanime.entities

import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import jakarta.persistence.*
import java.time.ZonedDateTime
import java.util.*

@Entity
@Table(name = "episode")
data class Episode(
override val uuid: UUID? = null,
@ManyToOne(optional = false, fetch = FetchType.LAZY)
var platform: Platform? = null,
@ManyToOne(optional = false, fetch = FetchType.LAZY)
var anime: Anime? = null,
@Column(nullable = false, name = "episode_type")
@Enumerated(EnumType.STRING)
val episodeType: EpisodeType? = null,
@Column(nullable = false, name = "lang_type")
@Enumerated(EnumType.STRING)
val langType: LangType? = null,
@Column(nullable = false)
val hash: String? = null,
@Column(nullable = false, name = "release_date")
val releaseDate: ZonedDateTime = ZonedDateTime.now(),
@Column(nullable = false)
var season: Int? = null,
@Column(nullable = false)
var number: Int? = null,
@Column(nullable = true, columnDefinition = "TEXT")
var title: String? = null,
@Column(nullable = false, columnDefinition = "TEXT")
var url: String? = null,
@Column(nullable = false, columnDefinition = "TEXT")
var image: String? = null,
@Column(nullable = false)
var duration: Long = -1
) : ShikkEntity(uuid)
38 changes: 3 additions & 35 deletions src/main/kotlin/fr/shikkanime/entities/Metric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package fr.shikkanime.entities

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Index
import jakarta.persistence.Table
import java.time.ZonedDateTime
import java.util.*

@Entity
@Table(
name = "metric",
indexes = [
Index(name = "idx_metrics_date", columnList = "date")
]
)
@Table(name = "metric")
data class Metric(
override val uuid: UUID? = null,
@Column(name = "cpu_load")
Expand All @@ -22,32 +16,6 @@ data class Metric(
val memoryUsage: Long = 0,
@Column(name = "database_size")
val databaseSize: Long = 0,
@Column(name = "date")
@Column(nullable = false)
val date: ZonedDateTime = ZonedDateTime.now(),
) : ShikkEntity(uuid) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Metric) return false

if (uuid != other.uuid) return false
if (cpuLoad != other.cpuLoad) return false
if (memoryUsage != other.memoryUsage) return false
if (databaseSize != other.databaseSize) return false
if (date != other.date) return false

return true
}

override fun hashCode(): Int {
var result = uuid?.hashCode() ?: 0
result = 31 * result + cpuLoad.hashCode()
result = 31 * result + memoryUsage.hashCode()
result = 31 * result + databaseSize.hashCode()
result = 31 * result + date.hashCode()
return result
}

override fun toString(): String {
return "Metric(uuid=$uuid, cpuLoad=$cpuLoad, memoryUsage=$memoryUsage, databaseSize=$databaseSize, date=$date)"
}
}
) : ShikkEntity(uuid)
32 changes: 3 additions & 29 deletions src/main/kotlin/fr/shikkanime/entities/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,7 @@ data class Platform(
@Column(nullable = false, unique = true)
val name: String? = null,
@Column(nullable = false)
val url: String? = null,
var url: String? = null,
@Column(nullable = false)
val image: String? = null
) : ShikkEntity(uuid) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Platform

if (uuid != other.uuid) return false
if (name != other.name) return false
if (url != other.url) return false
if (image != other.image) return false

return true
}

override fun hashCode(): Int {
var result = uuid?.hashCode() ?: 0
result = 31 * result + (name?.hashCode() ?: 0)
result = 31 * result + (url?.hashCode() ?: 0)
result = 31 * result + (image?.hashCode() ?: 0)
return result
}

override fun toString(): String {
return "Platform(uuid=$uuid, name=$name, url=$url, image=$image)"
}
}
var image: String? = null
) : ShikkEntity(uuid)
19 changes: 1 addition & 18 deletions src/main/kotlin/fr/shikkanime/entities/ShikkEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,4 @@ open class ShikkEntity(
@Id
@UuidGenerator
open val uuid: UUID? = null,
) : Serializable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ShikkEntity) return false

if (uuid != other.uuid) return false

return true
}

override fun hashCode(): Int {
return uuid?.hashCode() ?: 0
}

override fun toString(): String {
return "ShikkEntity(uuid=$uuid)"
}
}
) : Serializable
8 changes: 8 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/enums/EpisodeType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.shikkanime.entities.enums

enum class EpisodeType {
EPISODE,
SPECIAL,
FILM,
;
}
7 changes: 7 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/enums/LangType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.shikkanime.entities.enums

enum class LangType {
SUBTITLES,
VOICE,
;
}
11 changes: 10 additions & 1 deletion src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package fr.shikkanime.jobs

import fr.shikkanime.entities.Episode
import fr.shikkanime.services.EpisodeService
import fr.shikkanime.utils.Constant
import jakarta.inject.Inject
import java.time.ZonedDateTime

class FetchEpisodesJob : AbstractJob() {
@Inject
private lateinit var episodeService: EpisodeService

override fun run() {
val zonedDateTime = ZonedDateTime.now().withNano(0)
val episodes = mutableListOf<Episode>()

Constant.abstractPlatforms.forEach { abstractPlatform ->
println("Fetching episodes for ${abstractPlatform.getPlatform().name}...")

try {
abstractPlatform.fetchEpisodes(zonedDateTime)
episodes.addAll(abstractPlatform.fetchEpisodes(zonedDateTime))
} catch (e: Exception) {
println("Error while fetching episodes for ${abstractPlatform.getPlatform().name}: ${e.message}")
e.printStackTrace()
}
}

episodes.forEach { episodeService.saveOrUpdate(it) }
}
}
7 changes: 2 additions & 5 deletions src/main/kotlin/fr/shikkanime/platforms/AbstractPlatform.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.shikkanime.platforms

import fr.shikkanime.entities.Country
import fr.shikkanime.entities.Episode
import fr.shikkanime.entities.Platform
import fr.shikkanime.services.CountryService
import fr.shikkanime.services.PlatformService
import fr.shikkanime.utils.Constant
import jakarta.inject.Inject
import kotlinx.coroutines.runBlocking
Expand All @@ -16,9 +16,6 @@ abstract class AbstractPlatform<C : PlatformConfiguration> {
val content: Map<String, String> = emptyMap(),
)

@Inject
protected lateinit var platformService: PlatformService

@Inject
protected lateinit var countryService: CountryService

Expand All @@ -28,7 +25,7 @@ abstract class AbstractPlatform<C : PlatformConfiguration> {
protected abstract fun getConfigurationClass(): Class<C>
abstract fun getPlatform(): Platform
abstract suspend fun fetchApiContent(zonedDateTime: ZonedDateTime): Api
abstract fun fetchEpisodes(zonedDateTime: ZonedDateTime): List<String>
abstract fun fetchEpisodes(zonedDateTime: ZonedDateTime): List<Episode>
abstract fun reset()

protected fun getCountries() = countryService.findAllByCode(configuration!!.availableCountries)
Expand Down
Loading

0 comments on commit 6497fb2

Please sign in to comment.