Skip to content

Commit

Permalink
Cache result of get_agents() and handle request exception
Browse files Browse the repository at this point in the history
  • Loading branch information
TDKorn committed Jun 23, 2024
1 parent bca9697 commit cae9ef2
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions magento/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import sys
import logging
import requests
import functools

from typing import Union, List, Type
from logging import Logger, FileHandler, StreamHandler, Handler


AGENTS = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36']
DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'


def parse_domain(domain: str):
Expand All @@ -34,17 +35,19 @@ def parse_domain(domain: str):
raise ValueError("Invalid format provided for ``domain``")


@functools.lru_cache
def get_agents() -> list:
"""Scrapes a list of user agents. Returns a default list if the scrape fails."""
if (response := requests.get('https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome')).ok:
section = response.text.split('<h2>Latest Chrome on Windows 10 User Agents</h2>')[1]
raw_agents = section.split('code\">')[1:]
agents = [agent.split('<')[0] for agent in raw_agents]
for a in agents:
if a not in AGENTS:
AGENTS.append(a)
# If function fails, will still return the hardcoded list
return AGENTS
try:
response = requests.get('https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome')
if response.ok:
section = response.text.split('<h2>Latest Chrome on Windows 10 User Agents</h2>')[1]
return [agent.split('<')[0] for agent in section.split('code\">')[1:]]
else:
raise RuntimeError("Unable to retrieve user agents")

except Exception:
return [DEFAULT_USER_AGENT]


def get_agent(index=0) -> str:
Expand Down

0 comments on commit cae9ef2

Please sign in to comment.