Skip to content

Commit

Permalink
Merge branch 'dev' into ca-100
Browse files Browse the repository at this point in the history
  • Loading branch information
naumansharifwork committed Sep 15, 2024
2 parents 90f108a + 42a0d85 commit 1197df4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions clean/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,61 @@ def get_repeated_asset_url(self, objects: List[MetadataDict]):
else:
seen_urls.add(asset_url)
return repeated_urls


@retry(tries=3, delay=15, backoff=2)
def post_url(
url, user_agent="Big Local News (biglocalnews.org)", session=None, **kwargs
):
"""Request the provided URL and return a response object.
Args:
url (str): the url to be requested
user_agent (str): the user-agent header passed with the request (default: biglocalnews.org)
session: a session object to use when making the request. optional
"""
logger.debug(f"Requesting {url}")

# Set the headers
if "headers" not in kwargs:
kwargs["headers"] = {}
kwargs["headers"]["User-Agent"] = user_agent

# Go get it
if session is not None:
logger.debug(f"Requesting with session {session}")
response = session.post(url, **kwargs)
else:
response = requests.post(url, **kwargs)
logger.debug(f"Response code: {response.status_code}")

# Verify that the response is 200
assert response.ok

# Return the response
return response


@retry(tries=3, delay=15, backoff=2)
def get_cookies(url, user_agent="Big Local News (biglocalnews.org)", **kwargs):
"""Request the provided URL and return cookie object.
Args:
url (str): the url to be requested
user_agent (str): the user-agent header passed with the request (default: biglocalnews.org)
"""
logger.debug(f"Requesting {url}")

# Set the headers
if "headers" not in kwargs:
kwargs["headers"] = {}
kwargs["headers"]["User-Agent"] = user_agent
response = requests.get(url, **kwargs)

# Verify that the response is 200
assert response.ok

cookies = response.cookies.get_dict()

# Return the response
return cookies

0 comments on commit 1197df4

Please sign in to comment.