From c1c608ceb1430b68997ff59cebf4476072b0dde0 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Thu, 29 Feb 2024 15:11:59 +0100 Subject: [PATCH 1/2] Fix social networks error --- .../shikkanime/socialnetworks/BskySocialNetwork.kt | 1 + .../socialnetworks/DiscordSocialNetwork.kt | 4 +++- .../socialnetworks/ThreadsSocialNetwork.kt | 4 ++-- .../socialnetworks/TwitterSocialNetwork.kt | 14 ++++++++++---- .../fr/shikkanime/wrappers/ThreadsWrapper.kt | 8 +++++--- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt b/src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt index e4110d86..d9808731 100644 --- a/src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt +++ b/src/main/kotlin/fr/shikkanime/socialnetworks/BskySocialNetwork.kt @@ -27,6 +27,7 @@ class BskySocialNetwork : AbstractSocialNetwork() { try { val identifier = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.BSKY_IDENTIFIER)) val password = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.BSKY_PASSWORD)) + if (identifier.isBlank() || password.isBlank()) throw Exception("Identifier or password is empty") val session = runBlocking { BskyWrapper.createSession(identifier, password) } accessJwt = requireNotNull(session.getAsString("accessJwt")) did = requireNotNull(session.getAsString("did")) diff --git a/src/main/kotlin/fr/shikkanime/socialnetworks/DiscordSocialNetwork.kt b/src/main/kotlin/fr/shikkanime/socialnetworks/DiscordSocialNetwork.kt index c8c2160a..fe119eda 100644 --- a/src/main/kotlin/fr/shikkanime/socialnetworks/DiscordSocialNetwork.kt +++ b/src/main/kotlin/fr/shikkanime/socialnetworks/DiscordSocialNetwork.kt @@ -30,7 +30,9 @@ class DiscordSocialNetwork : AbstractSocialNetwork() { if (isInitialized) return try { - val builder = JDABuilder.createDefault(configCacheService.getValueAsString(ConfigPropertyKey.DISCORD_TOKEN)) + val token = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.DISCORD_TOKEN)) + if (token.isBlank()) throw Exception("Token is empty") + val builder = JDABuilder.createDefault(token) builder.setActivity(Activity.playing(BASE_URL)) jda = builder.build() jda?.awaitReady() diff --git a/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt b/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt index 739171ea..521abb59 100644 --- a/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt +++ b/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt @@ -28,7 +28,7 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() { try { val username = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.THREADS_USERNAME)) val password = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.THREADS_PASSWORD)) - + if (username.isBlank() || password.isBlank()) throw Exception("Username or password is empty") val generateDeviceId = ThreadsWrapper.generateDeviceId(username, password) val (token, userId) = runBlocking { ThreadsWrapper.login(generateDeviceId, username, password) } @@ -39,7 +39,7 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() { isInitialized = true initializedAt = ZonedDateTime.now() } catch (e: Exception) { - logger.log(Level.SEVERE, "Error while initializing BskySocialNetwork", e) + logger.log(Level.SEVERE, "Error while initializing ThreadsSocialNetwork", e) } } diff --git a/src/main/kotlin/fr/shikkanime/socialnetworks/TwitterSocialNetwork.kt b/src/main/kotlin/fr/shikkanime/socialnetworks/TwitterSocialNetwork.kt index 819e4fe7..4062adb2 100644 --- a/src/main/kotlin/fr/shikkanime/socialnetworks/TwitterSocialNetwork.kt +++ b/src/main/kotlin/fr/shikkanime/socialnetworks/TwitterSocialNetwork.kt @@ -24,13 +24,19 @@ class TwitterSocialNetwork : AbstractSocialNetwork() { if (isInitialized) return try { + val consumerKey = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_CONSUMER_KEY)) + val consumerSecret = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_CONSUMER_SECRET)) + val accessToken = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_ACCESS_TOKEN)) + val accessTokenSecret = requireNotNull(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_ACCESS_TOKEN_SECRET)) + if (consumerKey.isBlank() || consumerSecret.isBlank() || accessToken.isBlank() || accessTokenSecret.isBlank()) throw Exception("Twitter credentials are empty") + twitter = TwitterFactory( ConfigurationBuilder() .setDebugEnabled(true) - .setOAuthConsumerKey(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_CONSUMER_KEY)) - .setOAuthConsumerSecret(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_CONSUMER_SECRET)) - .setOAuthAccessToken(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_ACCESS_TOKEN)) - .setOAuthAccessTokenSecret(configCacheService.getValueAsString(ConfigPropertyKey.TWITTER_ACCESS_TOKEN_SECRET)) + .setOAuthConsumerKey(consumerKey) + .setOAuthConsumerSecret(consumerSecret) + .setOAuthAccessToken(accessToken) + .setOAuthAccessTokenSecret(accessTokenSecret) .build() ).instance diff --git a/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt b/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt index bd390c58..d5dc2aac 100644 --- a/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt +++ b/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt @@ -36,7 +36,7 @@ object ThreadsWrapper { private val httpRequest = HttpRequest() private val secureRandom = SecureRandom() - suspend fun qeSync(): HttpResponse { + private suspend fun qeSync(): HttpResponse { val uuid = UUID.randomUUID().toString() return httpRequest.post( @@ -54,11 +54,13 @@ object ThreadsWrapper { ) } - suspend fun encryptPassword(password: String): Map { + private suspend fun encryptPassword(password: String): Map { // https://github.com/instagram4j/instagram4j/blob/39635974c391e21a322ab3294275df99d7f75f84/src/main/java/com/github/instagram4j/instagram4j/utils/IGUtils.java#L176 val randKey = ByteArray(32).also { secureRandom.nextBytes(it) } val iv = ByteArray(12).also { secureRandom.nextBytes(it) } - val headers = qeSync().headers + val response = qeSync() + require(response.status.value == 200) { "Failed to get qeSync: ${response.status}" } + val headers = response.headers val time = (System.currentTimeMillis() / 1000).toString() val passwordEncryptionKeyID = headers["ig-set-password-encryption-key-id"]!!.toInt() From a7392bd4136cf7abd50cea9aad7c1ca9ad2a9104 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Thu, 29 Feb 2024 16:04:07 +0100 Subject: [PATCH 2/2] Fix Threads platform name and move thead to social network --- .../fr/shikkanime/jobs/FetchEpisodesJob.kt | 37 +++++++++---------- .../socialnetworks/ThreadsSocialNetwork.kt | 2 +- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt b/src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt index eb0365ff..825fef6b 100644 --- a/src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt +++ b/src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt @@ -82,12 +82,7 @@ class FetchEpisodesJob : AbstractJob { if (savedEpisodes.isNotEmpty() && savedEpisodes.size < configCacheService.getValueAsInt(ConfigPropertyKey.SOCIAL_NETWORK_EPISODES_SIZE_LIMIT)) { val dtos = AbstractConverter.convert(savedEpisodes, EpisodeDto::class.java) - - dtos.forEach { - Thread { - sendToSocialNetworks(it) - }.start() - } + dtos.forEach { sendToSocialNetworks(it) } } isRunning = false @@ -104,20 +99,22 @@ class FetchEpisodesJob : AbstractJob { } Constant.abstractSocialNetworks.forEach { socialNetwork -> - try { - socialNetwork.sendEpisodeRelease(dto, mediaImage) - } catch (e: Exception) { - logger.log( - Level.SEVERE, - "Error while sending episode release for ${ - socialNetwork.javaClass.simpleName.replace( - "SocialNetwork", - "" - ) - }", - e - ) - } + Thread { + try { + socialNetwork.sendEpisodeRelease(dto, mediaImage) + } catch (e: Exception) { + logger.log( + Level.SEVERE, + "Error while sending episode release for ${ + socialNetwork.javaClass.simpleName.replace( + "SocialNetwork", + "" + ) + }", + e + ) + } + }.start() } } } \ No newline at end of file diff --git a/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt b/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt index 521abb59..b5f81aba 100644 --- a/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt +++ b/src/main/kotlin/fr/shikkanime/socialnetworks/ThreadsSocialNetwork.kt @@ -71,7 +71,7 @@ class ThreadsSocialNetwork : AbstractSocialNetwork() { Platform.CRUN -> "@crunchyroll_fr" Platform.NETF -> "@netflixfr" Platform.PRIM -> "@primevideofr" - else -> platform.name + else -> platform.platformName } }