Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Fix exception handling for IP get location and remove psutil dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
coskundeniz committed Dec 9, 2023
1 parent ae0329b commit ca0937f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
5 changes: 4 additions & 1 deletion ad_clicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,18 @@ def main():
else:
logger.info(f"Found {len(ads)} ads")
search_controller.click_ads(ads)
search_controller.end_search()

except Exception as exp:
logger.error("Exception occurred. See the details in the log file.")

message = str(exp).split("\n")[0]
logger.debug(f"Exception: {message}")
details = traceback.format_tb(exp.__traceback__)
logger.debug(f"Exception details: \n{''.join(details)}")

logger.debug(f"Exception cause: {exp.__cause__}") if exp.__cause__ else None

finally:
if search_controller:
search_controller.end_search()

Expand Down
8 changes: 3 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
async-generator==1.10
attrs==22.1.0
black==22.10.0
attrs==23.1.0
certifi==2023.7.22
chardet==4.0.0
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.4
configparser==5.0.2
crayons==0.4.0
exceptiongroup==1.0.0rc9
exceptiongroup==1.1.3
h11==0.14.0
idna==2.10
mypy-extensions==0.4.3
outcome==1.2.0
pathspec==0.10.1
platformdirs==2.5.2
psutil==5.9.3
PySocks==1.7.1
random-user-agent==1.0.1
requests==2.31.0
Expand All @@ -27,6 +25,6 @@ trio==0.22.0
trio-websocket==0.9.2
typing_extensions==4.4.0
undetected-chromedriver==3.5.3
urllib3==2.0.6
urllib3==2.0.7
websockets==10.3
wsproto==1.2.0
31 changes: 2 additions & 29 deletions run_ad_clicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
-- Orhan Veli
"""

import psutil
import random
import traceback
import subprocess
Expand Down Expand Up @@ -128,27 +127,6 @@ def start_tool(
subprocess.run(command)


def cleanup() -> None:
"""If there is processes remained running, terminate them"""

logger.debug("Cleaning up resources...")

PROCESS_NAME = "ad_clicker"

for process in psutil.process_iter():

if (
process.name() == "python"
and PROCESS_NAME in process.cmdline()[1]
and process.cmdline()[1] != "run_ad_clicker.py"
):
logger.debug(
f"Terminating process: {' '.join(process.cmdline()[:2])}, PID: {process.pid}"
)

process.terminate()


def main() -> None:

arg_parser = get_arg_parser()
Expand Down Expand Up @@ -199,8 +177,6 @@ def main() -> None:
# wait for all tasks to complete
_, _ = wait(futures)

cleanup()

# 2nd way - same query on each browser
elif args.multiprocess_style == 2:

Expand Down Expand Up @@ -229,8 +205,6 @@ def main() -> None:
# wait for all tasks to complete
_, _ = wait(futures)

cleanup()

else:
logger.error("Invalid multiprocess style!")

Expand All @@ -239,13 +213,12 @@ def main() -> None:

try:
main()
except KeyboardInterrupt:
cleanup()

except Exception as exp:
logger.error("Exception occurred. See the details in the log file.")

message = str(exp).split("\n")[0]
logger.debug(f"Exception: {message}")
details = traceback.format_tb(exp.__traceback__)
logger.debug(f"Exception details: \n{''.join(details)}")

cleanup()
12 changes: 10 additions & 2 deletions search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,16 @@ def end_search(self) -> None:

if self._driver:
logger.info("Closing the browser...")
self._driver.delete_all_cookies()
self._driver.quit()

try:
self._driver.delete_all_cookies()
self._driver.quit()

except OSError as exp:
logger.debug(exp)

self._driver = None


def _load(self) -> None:
"""Load Google main page"""
Expand Down
33 changes: 27 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,44 @@ def get_location(
try:
response = requests.get("https://api.ipify.org", proxies=proxies_header)
ip_address = response.text

if not ip_address:
raise Exception("Failed with https://api.ipify.org")

break

except:
logger.debug("Failed with api.ipify.org")
except Exception as exp:
logger.debug(exp)

try:
logger.debug("Trying with ipv4.webshare.io...")
response = requests.get(
"https://ipv4.webshare.io/", proxies=proxies_header, timeout=5
)
ip_address = response.text

if not ip_address:
raise Exception("Failed with https://ipv4.webshare.io")

break

except:
logger.debug("Failed with ipv4.webshare.io")
except Exception as exp:
logger.debug(exp)

try:
logger.debug("Trying with ipconfig.io...")
response = requests.get(
"https://ipconfig.io/json", proxies=proxies_header, timeout=5
)
ip_address = response.json().get("ip")

if not ip_address:
raise Exception("Failed with https://ipconfig.io/json")

break

except:
logger.debug("Failed with ipconfig.io")
except Exception as exp:
logger.debug(exp)

if cycle == 1:
break
Expand Down Expand Up @@ -277,6 +290,14 @@ def create_webdriver(
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument(f"--user-agent={user_agent_str}")

# disable WebRTC IP tracking
webrtc_preferences = {
"webrtc.ip_handling_policy": "disable_non_proxied_udp",
"webrtc.multiple_routes_enabled": False,
"webrtc.nonproxied_udp_enabled": False,
}
chrome_options.add_experimental_option("prefs", webrtc_preferences)

if incognito:
chrome_options.add_argument("--incognito")

Expand Down

0 comments on commit ca0937f

Please sign in to comment.