Skip to content

Commit

Permalink
Add exception details to log file
Browse files Browse the repository at this point in the history
  • Loading branch information
coskundeniz committed Jun 5, 2023
1 parent a533ad6 commit f8b763e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ optional arguments:
### Examples

```
"~/" represents home folder on Linux/Mac. If you use Windows, don't use it. If you put your files into project directory, you can just specify the file name, otherwise you should give the full path.
"~/" represents home folder on Linux/Mac. If you use Windows, don't use it.
If you put your files into project directory, you can just specify the file name,
otherwise you should give the full path.
```

* Search for "wireless keyboard" with the default 4 seconds visit time on clicked ad pages.
Expand Down
25 changes: 18 additions & 7 deletions ad_clicker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import traceback
from argparse import ArgumentParser

from config import logger, update_log_formats
Expand Down Expand Up @@ -82,14 +83,24 @@ def main():

driver = create_webdriver(proxy, args.auth, args.headless, args.incognito)

search_controller = SearchController(driver, args.query, args.ad_visit_time, args.excludes)
ads = search_controller.search_for_ads()
try:
search_controller = SearchController(driver, args.query, args.ad_visit_time, args.excludes)
ads = search_controller.search_for_ads()

if not ads:
logger.info("No ads in the search results!")
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)}")

if not ads:
logger.info("No ads in the search results!")
else:
logger.info(f"Found {len(ads)} ads")
search_controller.click_ads(ads)
search_controller.end_search()


Expand Down
9 changes: 9 additions & 0 deletions run_ad_clicker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import psutil
import random
import traceback
import subprocess
import multiprocessing
from typing import Optional
Expand Down Expand Up @@ -204,3 +205,11 @@ def main() -> None:
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()
10 changes: 9 additions & 1 deletion run_in_loop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
import subprocess
import multiprocessing
from argparse import ArgumentParser
Expand Down Expand Up @@ -104,4 +105,11 @@ def main() -> None:

if __name__ == "__main__":

main()
try:
main()
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)}")
6 changes: 4 additions & 2 deletions search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def search_for_ads(self) -> AdList:
"""

logger.info(f"Starting search for '{self._search_query}'")
sleep(1)

try:
search_input_box = self._driver.find_element(*self.SEARCH_INPUT)
Expand Down Expand Up @@ -92,6 +93,7 @@ def search_for_ads(self) -> AdList:
except TimeoutException:
logger.error("Timed out waiting for results!")
self.end_search()
raise SystemExit()

return ad_links

Expand Down Expand Up @@ -133,10 +135,10 @@ def click_ads(self, ads: AdList) -> None:
logger.debug(f"Ad element [{ad_title}] has changed. Skipping scroll into view...")

def end_search(self) -> None:
"""Close the browsers"""
"""Close the browser"""

if self._driver:
# delete all cookies before quitting
logger.info("Closing the browser...")
self._driver.delete_all_cookies()
self._driver.quit()

Expand Down

0 comments on commit f8b763e

Please sign in to comment.