From 0aafbff9f0aff46ba566edbd37aaf89df0344857 Mon Sep 17 00:00:00 2001 From: xyrolaith <18216850+xyrolaith@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:33:58 +0200 Subject: [PATCH] feat: Add Jodel support (#462) --- .../app/leon/startup/ContainerInitializer.kt | 2 + .../domain/sanitizer/jodel/JodelSanitizer.kt | 34 +++++++++++++ core-domain/src/main/res/values/strings.xml | 1 + .../sanitizer/jodel/JodelSanitizerTest.kt | 50 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt create mode 100644 core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt index 6b81a2d4..8252c811 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt @@ -41,6 +41,7 @@ import com.svenjacobs.app.leon.core.domain.sanitizer.google.GoogleSearchSanitize import com.svenjacobs.app.leon.core.domain.sanitizer.heise.HeiseSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.instagram.InstagramSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.jdoqocy.JdoqocySanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.jodel.JodelSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.lazada.LazadaSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.linksynergy.LinkSynergySanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.netflix.NetflixSanitizer @@ -95,6 +96,7 @@ class ContainerInitializer : DistinctInitializer { HeiseSanitizer(), InstagramSanitizer(), JdoqocySanitizer(), + JodelSanitizer(), LazadaSanitizer(), LinkSynergySanitizer(), NetflixSanitizer(), diff --git a/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt new file mode 100644 index 00000000..b3406c7f --- /dev/null +++ b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt @@ -0,0 +1,34 @@ +package com.svenjacobs.app.leon.core.domain.sanitizer.jodel + +import android.content.Context +import kotlin.io.encoding.Base64 +import kotlin.io.encoding.ExperimentalEncodingApi +import com.svenjacobs.app.leon.core.common.url.decodeUrl +import com.svenjacobs.app.leon.core.domain.R +import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId +import org.json.JSONObject + +class JodelSanitizer : Sanitizer { + override val id = SanitizerId("jodel") + + override fun getMetadata(context: Context) = Sanitizer.Metadata( + name = context.getString(R.string.sanitizer_jodel_name), + ) + + @OptIn(ExperimentalEncodingApi::class) + override fun invoke(input: String): String { + val encoded = URL_REGEX.find(input)?.groupValues?.getOrNull(1) ?: return input + val base64Data = decodeUrl(encoded) + val jsonString = Base64.Default.decode(base64Data) + val jsonObject = JSONObject(jsonString.decodeToString()) + val url = jsonObject.getString("\$android_url") + return url + } + + override fun matchesDomain(input: String) = URL_REGEX.containsMatchIn(input) + + private companion object { + private val URL_REGEX = Regex("^https?://shared\\.jodel\\.com/a/key_live_.*&data=([^?&]*)") + } +} diff --git a/core-domain/src/main/res/values/strings.xml b/core-domain/src/main/res/values/strings.xml index 46795690..8f2dd20c 100644 --- a/core-domain/src/main/res/values/strings.xml +++ b/core-domain/src/main/res/values/strings.xml @@ -38,6 +38,7 @@ heise online Instagram Jdoqocy + Jodel Lazada LinkSynergy Netflix diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt new file mode 100644 index 00000000..f99df7cd --- /dev/null +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt @@ -0,0 +1,50 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2023 Sven Jacobs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.svenjacobs.app.leon.core.domain.sanitizer.jodel + +import io.kotest.core.spec.style.WordSpec +import io.kotest.matchers.shouldBe + +class JodelSanitizerTest : + WordSpec( + { + + "invoke" should { + + "extract Jodel sharing URL" { + val sanitizer = JodelSanitizer() + val result = sanitizer( + "http://shared.jodel.com/a/key_live_abZZZZgPxyz82xxxxAAAAdefghi4YYY1" + + "?%24identity_id=123456789012345678&feature=shared_post&campaign=image" + + "_DE_FrontPage&type=0&duration=0&source=android&data=eyIkY2Fub25pY2FsX" + + "2lkZW50aWZpZXIiOiJzYWpcL2JhZGMwZmZlZWJhZGMwZmZlZTAxMjM0NSIsIiRwdWJsaW" + + "NseV9pbmRleGFibGUiOiJ0cnVlIiwicG9zdElkIjoiYmFkYzBmZmVlYmFkYzBmZmVlMDE" + + "yMzQ1IiwiJGRlc2t0b3BfdXJsIjoiaHR0cHM6XC9cL3NoYXJlLmpvZGVsLmNvbVwvcG9z" + + "dD9wb3N0SWQ9YmFkYzBmZmVlYmFkYzBmZmVlMDEyMzQ1IiwicmVmZXJyZXJfaWQiOiJhY" + + "mNkZWYwMTIzNDU2Nzg5ZGVhZGMwZGUiLCIkYW5kcm9pZF91cmwiOiJodHRwczpcL1wvc2" + + "hhcmUuam9kZWwuY29tXC9wb3N0P3Bvc3RJZD1iYWRjMGZmZWViYWRjMGZmZWUwMTIzNDU" + + "iLCIkaW9zX3VybCI6Imh0dHBzOlwvXC9zaGFyZS5qb2RlbC5jb21cL3Bvc3Q%2FcG9zdE" + + "lkPWJhZGMwZmZlZWJhZGMwZmZlZTAxMjM0NSJ9?channel=copy", + ) + + result shouldBe "https://share.jodel.com/post?postId=badc0ffeebadc0ffee012345" + } + } + }, + )