summaryrefslogtreecommitdiff
path: root/src/gather.ts
blob: 725241894c646863355a52d476ef62ec2cc940ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export interface ProductInfo {
    productId: string,
    companyName: string,
    productName: string,
    price: number,
    accessDate: string,
    picture: string | null

}

interface WooliesResponse {
    Product: {
        Stockcode: number,
        DisplayName: string,
        Price: number,
        LargeImageFile: string
    }
}

interface ColesResponse {
    pricing: {
        now: number,
    }
    id: number,
    name: string,
    imageUris: [{
        uri: string
    }]
}

// const COLES_ROOT_URL = "apigw.coles.com.au"
// const WOOLIES_ROOT_URL = "www.woolworths.com.au"

const COLES_ROOT_URL = "localhost:3002"
const WOOLIES_ROOT_URL = "localhost:3002"



export async function fetchWooliesProduct(productID: string): Promise<ProductInfo | null> {
    const options = {
        method: 'GET',
        // credentials: 'include',
        headers: {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0',
            "Cookie": '_abck=8D926507FE8BB3E2030100E0A40908AD~-1~YAAQDV8wF4yrp+WKAQAAcBa1+QqOLH6Cr1PPGhoBK6dzldNoO6fT9E5Yh2JWjug69wmscVVDCxTl3AtqhLO700yB1l/aMekoSEMfXWdB1vAkiaNMKVPNjc+x4hh7Yon6NLceF4NYulFEKqcjo1BEgGtnYYv8xgJW0jcO0i4kIdNAZaszCrXxefAMTskVsvWcZvmAuqcLr72EmDY6BNBSCQgzm2KX4uAM7D79DQVKY5TDR79ITzKmCZghdYGdNrXtSxksNB8rllTWUT+5UD45fpB13jYdxmwy6Lu51gBNuYrQ/lmQuJdFIzc3czVYvQI64uDlWbA17Swz/9En3cCF0/IGtuAUx/6PfJVfAdFyfZrcD3d7Sl+vdzlhiZ3smo/1/ocITzZggDurPckyVMvy5DPmZDx1mwvkn83yOt0cP7c=~-1~-1~-1;'
          }
        
        
    };

    const url = `http://${WOOLIES_ROOT_URL}/apis/ui/product/detail/${productID}?isMobile=false&useVariant=false`
    const response = fetch(url, options)
        // .json()
        // .then(response => console.log(response))
        .then(response => response.json())
        .then(x => {console.log(url, x); return x})
        .then(data => data as WooliesResponse)
        .then(formatWooliesProduct)
    // .catch(err => console.error(err));

    return response
}

export async function fetchColesProduct(productID: string): Promise<ProductInfo | null> {
    const options = {
        method: 'GET',
        headers: {
            'Ocp-Apim-Subscription-Key': 'dd6ae58532d743978508555a59a199ac',
            'User-Agent': 'okhttp/4.10.0'
          }
    };

    const url = `https://${COLES_ROOT_URL}/digital/colesappbff/v3/api/2/products/${productID}?type=sku&storeId=697&shoppingMethod=clickAndCollect&includeLiquor=true`
    // const url = `http://localhost:3002/apis/ui/product/detail/${productID}?isMobile=false&useVariant=false`
    const response = fetch(url, options)
        // .json()
        // .then(response => console.log(response))
        .then(x => {console.log(x); return x})
        .then(response => response.json())
        .then(data => data as WooliesResponse)
        .then(formatWooliesProduct)
    // .catch(err => console.error(err));

    return response
}


function formatWooliesProduct(json: WooliesResponse): ProductInfo {
    const data = json.Product
    return {
        "productId": String(data.Stockcode),
        "companyName": "Woolies",
        "productName": data.DisplayName,
        "price": data.Price,
        "accessDate": String(Date.now()),
        "picture": data.LargeImageFile
    }
}


function formatColesProduct(json: ColesResponse): ProductInfo {
    return {
        "productId": String(json.id),
        "companyName": "Coles",
        "productName": json.name,
        "price": json.pricing.now,
        "accessDate": String(Date.now()),
        "picture": json.imageUris[0]?.uri
    }
}