Skip to content

Commit

Permalink
Social networks configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Mar 1, 2024
1 parent 5200821 commit 3a8a065
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ enum class ConfigPropertyKey(val key: String) {
THREADS_USERNAME("threads_username"),
THREADS_PASSWORD("threads_password"),
THREADS_MESSAGE("threads_message"),
BSKY_MESSAGE("bsky_message"),
BSKY_SESSION_TIMEOUT("bsky_session_timeout"),
THREADS_SESSION_TIMEOUT("threads_session_timeout"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package fr.shikkanime.socialnetworks

import com.google.inject.Inject
import fr.shikkanime.dtos.EpisodeDto
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.services.caches.ConfigCacheService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.StringUtils

abstract class AbstractSocialNetwork {
@Inject
Expand All @@ -12,5 +17,35 @@ abstract class AbstractSocialNetwork {
abstract fun logout()

abstract fun sendMessage(message: String)

open fun platformAccount(platform: Platform): String {
return platform.platformName

Check warning on line 22 in src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt#L22

Added line #L22 was not covered by tests
}

private fun information(episodeDto: EpisodeDto): String {
return when (episodeDto.episodeType) {
EpisodeType.SPECIAL -> "L'épisode spécial"
EpisodeType.FILM -> "Le film"
else -> "L'épisode ${episodeDto.number}"

Check warning on line 29 in src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt#L27-L29

Added lines #L27 - L29 were not covered by tests
}
}

fun getEpisodeMessage(episodeDto: EpisodeDto, baseMessage: String): String {
val uncensored = if (episodeDto.uncensored) " non censuré" else ""
val isVoice = if (episodeDto.langType == LangType.VOICE) " en VF " else " "

var configMessage = baseMessage
configMessage = configMessage.replace("{SHIKKANIME_URL}", "${Constant.BASE_URL}/animes/${episodeDto.anime.slug}")
configMessage = configMessage.replace("{URL}", episodeDto.url)
configMessage = configMessage.replace("{PLATFORM_ACCOUNT}", platformAccount(episodeDto.platform))
configMessage = configMessage.replace("{ANIME_HASHTAG}", "#${StringUtils.getHashtag(episodeDto.anime.shortName)}")
configMessage = configMessage.replace("{ANIME_TITLE}", episodeDto.anime.shortName)
configMessage = configMessage.replace("{EPISODE_INFORMATION}", "${information(episodeDto)}${uncensored}")
configMessage = configMessage.replace("{VOICE}", isVoice)
configMessage = configMessage.replace("\\n", "\n")
configMessage = configMessage.trim()
return configMessage

Check warning on line 47 in src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/fr/shikkanime/socialnetworks/AbstractSocialNetwork.kt#L37-L47

Added lines #L37 - L47 were not covered by tests
}

abstract fun sendEpisodeRelease(episodeDto: EpisodeDto, mediaImage: ByteArray)
}
22 changes: 3 additions & 19 deletions src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package fr.shikkanime.socialnetworks

import fr.shikkanime.dtos.EpisodeDto
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.FileManager
import fr.shikkanime.utils.LoggerFactory
Expand Down Expand Up @@ -50,7 +48,7 @@ class BskySocialNetwork : AbstractSocialNetwork() {
private fun checkSession() {
if (!isInitialized) return

if (initializedAt!!.plusMinutes(10).isBefore(ZonedDateTime.now())) {
if (initializedAt!!.plusMinutes(configCacheService.getValueAsInt(ConfigPropertyKey.BSKY_SESSION_TIMEOUT, 10).toLong()).isBefore(ZonedDateTime.now())) {
logout()
login()
}
Expand All @@ -62,27 +60,13 @@ class BskySocialNetwork : AbstractSocialNetwork() {
runBlocking { BskyWrapper.createRecord(accessJwt!!, did!!, message) }
}

private fun information(episodeDto: EpisodeDto): String {
return when (episodeDto.episodeType) {
EpisodeType.SPECIAL -> "L'épisode spécial"
EpisodeType.FILM -> "Le film"
else -> "L'épisode ${episodeDto.number}"
}
}

override fun sendEpisodeRelease(episodeDto: EpisodeDto, mediaImage: ByteArray) {
checkSession()
if (!isInitialized) return

val url = "${Constant.BASE_URL}/animes/${episodeDto.anime.slug}"

Check warning on line 66 in src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt#L66

Added line #L66 was not covered by tests
val uncensored = if (episodeDto.uncensored) " non censuré" else ""
val isVoice = if (episodeDto.langType == LangType.VOICE) " en VF " else " "
val message =
"\uD83D\uDEA8 ${information(episodeDto)}${uncensored} de ${episodeDto.anime.shortName} est maintenant disponible${isVoice}sur ${episodeDto.platform.platformName}\n\nBon visionnage. \uD83C\uDF7F"

val message = getEpisodeMessage(episodeDto, configCacheService.getValueAsString(ConfigPropertyKey.BSKY_MESSAGE) ?: "")
val webpByteArray = FileManager.encodeToWebP(mediaImage)
val imageJson =
runBlocking { BskyWrapper.uploadBlob(accessJwt!!, ContentType.parse("image/webp"), webpByteArray) }
val imageJson = runBlocking { BskyWrapper.uploadBlob(accessJwt!!, ContentType.parse("image/webp"), webpByteArray) }
runBlocking { BskyWrapper.createRecord(accessJwt!!, did!!, message, listOf(BskyWrapper.Image(imageJson, url))) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package fr.shikkanime.socialnetworks

import fr.shikkanime.dtos.EpisodeDto
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.utils.LoggerFactory
import fr.shikkanime.wrappers.ThreadsWrapper
Expand Down Expand Up @@ -54,7 +52,9 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() {
private fun checkSession() {
if (!isInitialized) return

if (initializedAt!!.plusMinutes(10).isBefore(ZonedDateTime.now())) {
if (initializedAt!!.plusMinutes(configCacheService.getValueAsInt(ConfigPropertyKey.THREADS_SESSION_TIMEOUT, 10).toLong())

Check warning on line 55 in src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt#L55

Added line #L55 was not covered by tests
.isBefore(ZonedDateTime.now())
) {
logout()
login()
}
Expand All @@ -66,7 +66,7 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() {
runBlocking { ThreadsWrapper.publish(username!!, deviceId!!, userId!!, token!!, message) }
}

private fun platformAccount(platform: Platform): String {
override fun platformAccount(platform: Platform): String {
return when (platform) {
Platform.CRUN -> "@crunchyroll_fr"
Platform.NETF -> "@netflixfr"
Expand All @@ -75,34 +75,10 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() {
}
}

private fun information(episodeDto: EpisodeDto): String {
return when (episodeDto.episodeType) {
EpisodeType.SPECIAL -> "L'épisode spécial"
EpisodeType.FILM -> "Le film"
else -> "L'épisode ${episodeDto.number}"
}
}

fun getMessage(episodeDto: EpisodeDto): String {
val uncensored = if (episodeDto.uncensored) " non censuré" else ""
val isVoice = if (episodeDto.langType == LangType.VOICE) " en VF " else " "

var configMessage = configCacheService.getValueAsString(ConfigPropertyKey.THREADS_MESSAGE) ?: ""
configMessage = configMessage.replace("{URL}", episodeDto.url)
configMessage = configMessage.replace("{PLATFORM_ACCOUNT}", platformAccount(episodeDto.platform))
configMessage = configMessage.replace("{ANIME_TITLE}", episodeDto.anime.shortName)
configMessage = configMessage.replace("{EPISODE_INFORMATION}", "${information(episodeDto)}${uncensored}")
configMessage = configMessage.replace("{VOICE}", isVoice)
configMessage = configMessage.replace("\\n", "\n")
configMessage = configMessage.trim()
return configMessage
}

override fun sendEpisodeRelease(episodeDto: EpisodeDto, mediaImage: ByteArray) {
checkSession()
if (!isInitialized) return

val message = getMessage(episodeDto)
val message = getEpisodeMessage(episodeDto, configCacheService.getValueAsString(ConfigPropertyKey.THREADS_MESSAGE) ?: "")
runBlocking { ThreadsWrapper.publish(username!!, deviceId!!, userId!!, token!!, message, mediaImage) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package fr.shikkanime.socialnetworks

import fr.shikkanime.dtos.EpisodeDto
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.LoggerFactory
import fr.shikkanime.utils.StringUtils
import twitter4j.Twitter
import twitter4j.TwitterFactory
import twitter4j.conf.ConfigurationBuilder
Expand Down Expand Up @@ -72,7 +68,7 @@ class TwitterSocialNetwork : AbstractSocialNetwork() {
twitter!!.v2.createTweet(text = message)
}

private fun platformAccount(platform: Platform): String {
override fun platformAccount(platform: Platform): String {
return when (platform) {
Platform.ANIM -> "@ADNanime"
Platform.CRUN -> "@Crunchyroll_fr"
Expand All @@ -82,38 +78,11 @@ class TwitterSocialNetwork : AbstractSocialNetwork() {
}
}

private fun information(episodeDto: EpisodeDto): String {
return when (episodeDto.episodeType) {
EpisodeType.SPECIAL -> "L'épisode spécial"
EpisodeType.FILM -> "Le film"
else -> "L'épisode ${episodeDto.number}"
}
}

fun getMessage(episodeDto: EpisodeDto): String {
val uncensored = if (episodeDto.uncensored) " non censuré" else ""
val isVoice = if (episodeDto.langType == LangType.VOICE) " en VF " else " "

var configMessage = configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_MESSAGE) ?: ""
configMessage =
configMessage.replace("{SHIKKANIME_URL}", "${Constant.BASE_URL}/animes/${episodeDto.anime.slug}")
configMessage = configMessage.replace("{URL}", episodeDto.url)
configMessage = configMessage.replace("{PLATFORM_ACCOUNT}", platformAccount(episodeDto.platform))
configMessage =
configMessage.replace("{ANIME_HASHTAG}", "#${StringUtils.getHashtag(episodeDto.anime.shortName)}")
configMessage = configMessage.replace("{ANIME_TITLE}", episodeDto.anime.shortName)
configMessage = configMessage.replace("{EPISODE_INFORMATION}", "${information(episodeDto)}${uncensored}")
configMessage = configMessage.replace("{VOICE}", isVoice)
configMessage = configMessage.replace("\\n", "\n")
configMessage = configMessage.trim()
return configMessage
}

override fun sendEpisodeRelease(episodeDto: EpisodeDto, mediaImage: ByteArray) {
login()
if (!isInitialized) return
if (twitter == null) return
val message = getMessage(episodeDto)
val message = getEpisodeMessage(episodeDto, configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_MESSAGE) ?: "")

val uploadMedia = twitter!!.tweets().uploadMedia(
UUID.randomUUID().toString(),
Expand Down
53 changes: 53 additions & 0 deletions src/main/resources/db/changelog/2024/03/01-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.25.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<property global="false" name="id" value="1709281579180"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'bsky_message'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="bsky_message"/>
<column name="property_value"
value="🚨 {EPISODE_INFORMATION} de {ANIME_TITLE} est maintenant disponible{VOICE}sur {PLATFORM_ACCOUNT}\n\nBon visionnage. 🍿"/>
</insert>
</changeSet>

<changeSet id="${id}-2" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'bsky_session_timeout'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="bsky_session_timeout"/>
<column name="property_value" value="10"/>
</insert>
</changeSet>

<changeSet id="${id}-3" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'threads_session_timeout'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="threads_session_timeout"/>
<column name="property_value" value="60"/>
</insert>
</changeSet>
</databaseChangeLog>
2 changes: 2 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
<include file="/db/changelog/2024/02/09-changelog.xml"/>
<include file="/db/changelog/2024/02/10-changelog.xml"/>
<include file="/db/changelog/2024/02/11-changelog.xml"/>
<!-- March 2024 -->
<include file="/db/changelog/2024/03/01-changelog.xml"/>
</databaseChangeLog>
Loading

0 comments on commit 3a8a065

Please sign in to comment.