Skip to content

Commit

Permalink
Fix opening shopping ads on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
coskundeniz committed Oct 27, 2024
1 parent e30e222 commit 2489c9b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ The followings are the default values in the config file.
4. Connect ADB to your phone wirelessly.
* `adb connect <phone_ip>:5555`

* Shopping ads will still be opened on browser.

* Watch an example run

[![Send to Android](assets/send_to_android_cover.png)](https://youtu.be/XrZeECSXASg)
Expand Down
3 changes: 2 additions & 1 deletion adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ def open_url(url: str, device_id: str) -> None:
"shell",
"am",
"start",
"-n",
"com.android.chrome/com.google.android.apps.chrome.Main",
"-a",
"android.intent.action.VIEW",
"-d",
url,
"com.android.chrome",
]
result = subprocess.run(command, capture_output=True, text=True)

Expand Down
15 changes: 11 additions & 4 deletions search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from config_reader import config
from logger import logger
from stats import SearchStats
from utils import Direction, add_cookies, solve_recaptcha, get_random_sleep
from utils import Direction, add_cookies, solve_recaptcha, get_random_sleep, resolve_redirect


LinkElement = selenium.webdriver.remote.webelement.WebElement
Expand Down Expand Up @@ -222,9 +222,12 @@ def click_shopping_ads(self, shopping_ads: AdList) -> None:
if self._hooks_enabled:
hooks.before_ad_click_hook(self._driver)

self._handle_browser_click(
ad_link_element, ad_link, True, original_window_handle, category="Shopping"
)
if config.behavior.send_to_android and self._android_device_id:
self._handle_android_click(ad_link_element, ad_link, True, category="Shopping")
else:
self._handle_browser_click(
ad_link_element, ad_link, True, original_window_handle, category="Shopping"
)

except Exception:
logger.debug(f"Failed to click ad element [{ad_title}]!")
Expand Down Expand Up @@ -280,6 +283,8 @@ def _extract_link_info(self, link: Any, is_ad_element: bool) -> tuple:
:param link: (ad, ad_link, ad_title) for ads LinkElement for non-ads
:type is_ad_element: bool
:param is_ad_element: Whether it is an ad or non-ad link
:rtype: tuple
:returns: (link_element, link_url, ad_title) tuple
"""

if is_ad_element:
Expand Down Expand Up @@ -314,6 +319,8 @@ def _handle_android_click(

url = link_url if category == "Shopping" else link_element.get_attribute("href")

url = resolve_redirect(url)

adb_controller.open_url(url, self._android_device_id)

click_time = datetime.now().strftime("%H:%M:%S")
Expand Down
18 changes: 18 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,21 @@ def get_locale_language(country_code: str) -> str:
logger.debug(f"Locale language code for {country_code}: {locale_language[0]}")

return locale_language


def resolve_redirect(url: str) -> str:
"""Resolve any redirects and return the final destination URL
:type url: str
:param url: Input url to resolve
:rtype: str
:returns: Final destination URL
"""

try:
response = requests.get(url, allow_redirects=True)
return response.url

except requests.RequestException as exp:
logger.error(f"Error resolving URL redirection: {exp}")
return url

0 comments on commit 2489c9b

Please sign in to comment.