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

bug: Redirect URLs for Aliexpress are not working #75

Merged
merged 70 commits into from
Nov 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
7d003d1
bug: Redirect URLs for Aliexpress are not working
hectorzin Nov 17, 2024
9d97249
pre-commit
hectorzin Nov 18, 2024
f6865bb
pre-commit
hectorzin Nov 18, 2024
8bd7ef0
pre-commit
hectorzin Nov 18, 2024
63a9e1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
c4ea2ef
pre-commit
hectorzin Nov 18, 2024
ce2c5f0
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
c4acbac
pre-commit, no time to see how to install it local
hectorzin Nov 18, 2024
c91583a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
912e0a1
precommit
hectorzin Nov 18, 2024
9f715b0
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
9f7afeb
precommit
hectorzin Nov 18, 2024
eb42cfb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
fe4a00c
precommit
hectorzin Nov 18, 2024
42d5252
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
4e7fb98
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
2716030
precommit
hectorzin Nov 18, 2024
ab230e6
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
78c34b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
6f370d7
pre-commit
hectorzin Nov 18, 2024
912a05f
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
85fa6a9
pc
hectorzin Nov 18, 2024
c226c00
pc
hectorzin Nov 18, 2024
c5ef0d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
66b8962
pc
hectorzin Nov 18, 2024
aed7a49
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
930012f
pc
hectorzin Nov 18, 2024
35db8a7
pc
hectorzin Nov 18, 2024
117dcef
pc
hectorzin Nov 18, 2024
81ec9ce
pc
hectorzin Nov 18, 2024
6c48235
pc
hectorzin Nov 18, 2024
2bd000d
pc
hectorzin Nov 18, 2024
244eaa0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
4880a60
pc
hectorzin Nov 18, 2024
244bb12
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
4d12057
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
c2d2705
pc
hectorzin Nov 18, 2024
b6f1a2e
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
092451c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
b1308ad
quitar precommit por ahora
hectorzin Nov 18, 2024
9a33cf3
no pc
hectorzin Nov 18, 2024
007d475
pcno
hectorzin Nov 18, 2024
27dcb77
pc
hectorzin Nov 18, 2024
fd4dee9
pc
hectorzin Nov 18, 2024
2bf2179
pc
hectorzin Nov 18, 2024
bfacbca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
db3a195
pc
hectorzin Nov 18, 2024
3739233
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
4ccf7b3
pc
hectorzin Nov 18, 2024
8a62211
pc
hectorzin Nov 18, 2024
f7a2f70
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
80f7402
pc
hectorzin Nov 18, 2024
369d83f
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
337e9e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
f42bacd
pc
hectorzin Nov 18, 2024
12ecdec
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
4b1b9cd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
13ea210
pc
hectorzin Nov 18, 2024
4e85597
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
05cda76
pc
hectorzin Nov 18, 2024
be680e2
pc
hectorzin Nov 18, 2024
9fd6abb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
bf690c7
pc
hectorzin Nov 18, 2024
ef67f1c
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
9afdba1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
640a918
pc
hectorzin Nov 18, 2024
b4e6e40
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
75c3195
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
e35b5b7
pc
hectorzin Nov 18, 2024
f4b5ddc
Merge branch 'bug/redirect_url_aliexpress_not_working' of https://git…
hectorzin Nov 18, 2024
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
Next Next commit
bug: Redirect URLs for Aliexpress are not working
hectorzin committed Nov 17, 2024
commit 7d003d128fecc55a1a23957c358ec1797287d0c3
71 changes: 62 additions & 9 deletions handlers/aliexpress_api_handler.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import requests
import time

from urllib.parse import urlparse, urlunparse
from urllib.parse import urlparse, urlunparse,parse_qs
from handlers.base_handler import BaseHandler
from handlers.aliexpress_handler import ALIEXPRESS_PATTERN

@@ -100,10 +100,50 @@ async def _convert_to_aliexpress_affiliate(self, source_url):

return None

def _get_real_url(self, link: str) -> str:
"""
Checks for a 'redirectUrl' parameter in the given link and extracts its value if present.
If no 'redirectUrl' is found, returns the original link.
Parameters:
- link: The original URL to analyze.
Returns:
- A string representing the resolved URL or the original link if no redirect exists.
"""
parsed_url = urlparse(link)
query_params = parse_qs(parsed_url.query)

# Check if the 'redirectUrl' parameter exists in the query
if "redirectUrl" in query_params:
redirect_url = query_params["redirectUrl"][0] # Extract the first value
return requests.utils.unquote(redirect_url) # Decode the URL
return link # Return the original link if no redirectUrl exists

def _resolve_redirects(self, message_text: str) -> dict:
"""
Resolves redirected URLs (e.g., redirectUrl) in the message text.
Parameters:
- message_text: The text of the message to process.
Returns:
- A dictionary mapping original URLs to resolved URLs.
"""
urls_in_message = re.findall(r"https?://[^\s]+", message_text)

original_to_resolved = {}
for url in urls_in_message:
resolved_url = self._get_real_url(url) # Resolve redirectUrl if present
original_to_resolved[url] = resolved_url

return original_to_resolved

async def handle_links(self, context) -> bool:
"""Handles AliExpress links and converts them using the Aliexpress API."""

message, modified_text, self.selected_users = self._unpack_context(context)

# Retrieve the AliExpress configuration from self.selected_users
aliexpress_config = self.selected_users.get("aliexpress.com", {}).get(
"aliexpress", {}
@@ -120,10 +160,14 @@ async def handle_links(self, context) -> bool:
f"{message.message_id}: Handling AliExpress links in the message..."
)

new_text = modified_text
# Map original links (with redirectUrl) to resolved links
original_to_resolved = self._resolve_redirects(modified_text)

# Find AliExpress links in the message text
aliexpress_links = re.findall(ALIEXPRESS_PATTERN, new_text)
# Extract resolved URLs that match the pattern
aliexpress_links = [
resolved for original, resolved in original_to_resolved.items()
if re.match(ALIEXPRESS_PATTERN, resolved)
]

if not aliexpress_links:
self.logger.info(
@@ -135,11 +179,20 @@ async def handle_links(self, context) -> bool:
f"{message.message_id}: Found {len(aliexpress_links)} AliExpress links. Processing..."
)

# Convert the links to affiliate links
for link in aliexpress_links:
affiliate_link = await self._convert_to_aliexpress_affiliate(link)
if affiliate_link:
new_text = new_text.replace(link, affiliate_link)
# Map original links to their affiliate counterparts
updated_links = {}

# Convert the resolved links to affiliate links
for original, resolved in original_to_resolved.items():
if resolved in aliexpress_links:
affiliate_link = await self._convert_to_aliexpress_affiliate(resolved)
if affiliate_link:
updated_links[original] = affiliate_link # Replace the original link

# Replace original links with their affiliate counterparts
new_text = modified_text
for original, affiliate in updated_links.items():
new_text = new_text.replace(original, affiliate)

# Add discount codes if they are configured
if discount_codes: