Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add non-product page Amazon sanitizer #64

Merged
merged 1 commit into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import dagger.multibindings.ElementsIntoSet

@Module
@InstallIn(SingletonComponent::class)
object AmazonModule {

@Provides
@IntoSet
fun provideSanitizerRegistration(
@ElementsIntoSet
fun provideSanitizerRegistrations(
amazonSanitizerRegistration: AmazonSanitizerRegistration,
): SanitizerRegistration = amazonSanitizerRegistration
amazonProductSanitizerRegistration: AmazonProductSanitizerRegistration,
): Set<@JvmSuppressWildcards SanitizerRegistration> = setOf(
amazonSanitizerRegistration,
amazonProductSanitizerRegistration,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 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 <http://www.gnu.org/licenses/>.
*/

package com.svenjacobs.app.leon.feature.sanitizer.amazon

import com.svenjacobs.app.leon.core.domain.sanitizer.RegexSanitizer
import javax.inject.Inject

class AmazonProductSanitizer @Inject constructor() : RegexSanitizer(
regex = Regex("ref=[^?&]+|[?&][^=]+=.[^&]*"),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 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 <http://www.gnu.org/licenses/>.
*/

package com.svenjacobs.app.leon.feature.sanitizer.amazon

import android.content.Context
import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerRegistration
import javax.inject.Inject
import javax.inject.Provider

class AmazonProductSanitizerRegistration @Inject constructor(
private val sanitizerProvider: Provider<AmazonProductSanitizer>,
) : SanitizerRegistration {

override val sanitizer: Sanitizer
get() = sanitizerProvider.get()

override val id = SanitizerId("amazon")

override val hasSettingsScreen = false

override fun getName(context: Context) =
context.getString(R.string.feat_sanitizer_amazon_product_name)

override fun matchesDomain(input: String) = DOMAIN_REGEX.containsMatchIn(input)

private companion object {
private val DOMAIN_REGEX = Regex("amazon\\..+/dp/[0-9A-Z]+")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

package com.svenjacobs.app.leon.feature.sanitizer.amazon

import com.svenjacobs.app.leon.core.common.regex.RegexFactory
import com.svenjacobs.app.leon.core.domain.sanitizer.RegexSanitizer
import javax.inject.Inject

class AmazonSanitizer @Inject constructor() : RegexSanitizer(
regex = Regex("ref=[^?&]+|[?&][^=]+=.[^&]*"),
regex = RegexFactory.ofParameter("ref_?"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class AmazonSanitizerRegistration @Inject constructor(
override val sanitizer: Sanitizer
get() = sanitizerProvider.get()

override val id = SanitizerId("amazon")
/**
* Since [AmazonProductSanitizerRegistration] already uses "amazon" and IDs should not be
* changed afterwards, this is just "amazon2".
*/
override val id = SanitizerId("amazon2")

override val hasSettingsScreen = false

Expand All @@ -41,6 +45,6 @@ class AmazonSanitizerRegistration @Inject constructor(
override fun matchesDomain(input: String) = DOMAIN_REGEX.containsMatchIn(input)

private companion object {
private val DOMAIN_REGEX = Regex("amazon\\..+/dp/[0-9A-Z]+")
private val DOMAIN_REGEX = Regex("amazon\\..+/")
}
}
21 changes: 21 additions & 0 deletions feature-sanitizer-amazon/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Léon - The URL Cleaner
~ Copyright (C) 2022 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 <http://www.gnu.org/licenses/>.
-->

<resources>
<string name="feat_sanitizer_amazon_product_name">Amazon Produkte</string>
</resources>
1 change: 1 addition & 0 deletions feature-sanitizer-amazon/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@

<resources>
<string name="feat_sanitizer_amazon_name" translatable="false">Amazon</string>
<string name="feat_sanitizer_amazon_product_name">Amazon Products</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 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 <http://www.gnu.org/licenses/>.
*/

package com.svenjacobs.app.leon.feature.sanitizer.amazon

import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe

class AmazonProductSanitizerTest : WordSpec(
{

"invoke" should {

"remove various Amazon parameters" {
val sanitizer = AmazonProductSanitizer()
val result = sanitizer(
"https://www.amazon.de/Xiaomi-Aktivit%C3%A4tstracker-Trainings-Puls%C3%" +
"BCberwachung-Akkulaufzeit/dp/B091G3FLL7/?_encoding=UTF8&pd_rd_w=xDcJP&pf" +
"_rd_p=bf172aca-3277-41f6-babb-6ce7fc34cf7f&pf_rd_r=ZC6FZ5G6W9K8DEZTPBYW&" +
"pd_rd_r=11b3ec4e-047c-4f37-8302-62dedb8f502b&pd_rd_wg=Ozi90&ref_=pd_gw_c" +
"i_mcx_mr_hp_atf_m",
)

result shouldBe "https://www.amazon.de/Xiaomi-Aktivit%C3%A4tstracker-Trainings-Pu" +
"ls%C3%BCberwachung-Akkulaufzeit/dp/B091G3FLL7/"
}
}
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ class AmazonSanitizerTest : WordSpec(

"invoke" should {

"remove various Amazon parameters" {
"remove ref_ parameter" {
val sanitizer = AmazonSanitizer()
val result = sanitizer(
"https://www.amazon.de/Xiaomi-Aktivit%C3%A4tstracker-Trainings-Puls%C3%" +
"BCberwachung-Akkulaufzeit/dp/B091G3FLL7/?_encoding=UTF8&pd_rd_w=xDcJP&pf" +
"_rd_p=bf172aca-3277-41f6-babb-6ce7fc34cf7f&pf_rd_r=ZC6FZ5G6W9K8DEZTPBYW&" +
"pd_rd_r=11b3ec4e-047c-4f37-8302-62dedb8f502b&pd_rd_wg=Ozi90&ref_=pd_gw_c" +
"i_mcx_mr_hp_atf_m",
"https://www.amazon.de/gp/css/homepage.html?ref_=nav_AccountFlyout_ya",
)

result shouldBe "https://www.amazon.de/Xiaomi-Aktivit%C3%A4tstracker-Trainings-Pu" +
"ls%C3%BCberwachung-Akkulaufzeit/dp/B091G3FLL7/"
result shouldBe "https://www.amazon.de/gp/css/homepage.html"
}
}
},
Expand Down