Add shared browser session bootstrap
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
import os
|
||||
import csv
|
||||
import json
|
||||
import time
|
||||
import re
|
||||
from dotenv import load_dotenv
|
||||
from calendar import monthrange
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import browser_cookie3
|
||||
from curl_cffi import requests
|
||||
|
||||
from retailer_sessions import load_costco_session
|
||||
|
||||
BASE_URL = "https://ecom-api.costco.com/ebusiness/order/v1/orders/graphql"
|
||||
RETAILER = "costco"
|
||||
|
||||
@@ -210,16 +209,7 @@ ITEM_FIELDS = [
|
||||
"is_coupon_line",
|
||||
]
|
||||
|
||||
|
||||
def load_config():
|
||||
load_dotenv()
|
||||
return {
|
||||
"authorization": os.getenv("COSTCO_X_AUTHORIZATION", "").strip(),
|
||||
"client_id": os.getenv("COSTCO_X_WCS_CLIENTID", "").strip(),
|
||||
"client_identifier": os.getenv("COSTCO_CLIENT_IDENTIFIER", "").strip(),
|
||||
}
|
||||
|
||||
def build_headers(config):
|
||||
def build_headers(auth_headers):
|
||||
headers = {
|
||||
"accept": "*/*",
|
||||
"content-type": "application/json-patch+json",
|
||||
@@ -232,18 +222,14 @@ def build_headers(config):
|
||||
"Gecko/20100101 Firefox/148.0"
|
||||
),
|
||||
}
|
||||
if config["authorization"]:
|
||||
headers["costco-x-authorization"] = config["authorization"]
|
||||
if config["client_id"]:
|
||||
headers["costco-x-wcs-clientId"] = config["client_id"]
|
||||
if config["client_identifier"]:
|
||||
headers["client-identifier"] = config["client_identifier"]
|
||||
headers.update(auth_headers)
|
||||
return headers
|
||||
|
||||
def build_session(config):
|
||||
def build_session():
|
||||
retailer_session = load_costco_session()
|
||||
session = requests.Session()
|
||||
session.cookies.update(browser_cookie3.firefox(domain_name=".costco.com"))
|
||||
session.headers.update(build_headers(config))
|
||||
session.cookies.update(retailer_session.cookies)
|
||||
session.headers.update(build_headers(retailer_session.headers))
|
||||
return session
|
||||
|
||||
|
||||
@@ -596,17 +582,10 @@ def main(outdir, document_type, document_sub_type, window_days, months_back):
|
||||
outdir = Path(outdir)
|
||||
raw_dir = outdir / "raw"
|
||||
try:
|
||||
config = load_config()
|
||||
click.echo(
|
||||
"auth headers present: "
|
||||
f"authorization={bool(config['authorization'])}, "
|
||||
f"client_id={bool(config['client_id'])}, "
|
||||
f"client_identifier={bool(config['client_identifier'])}"
|
||||
)
|
||||
session = build_session(config)
|
||||
session = build_session()
|
||||
except Exception as exc:
|
||||
raise click.ClickException(
|
||||
f"failed to load Costco Firefox cookies: {exc}"
|
||||
f"failed to load Costco browser session: {exc}"
|
||||
) from exc
|
||||
start_date, end_date = resolve_date_range(months_back)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user