From 435e9d593ed0a39bcf97d5bd8514e19da17ab657 Mon Sep 17 00:00:00 2001 From: Andi Date: Sat, 18 Mar 2023 12:20:01 +0700 Subject: [PATCH] convenient input for `get_shopee_shop_product` --- LICENSE | 17 -------------- README.md | 18 +++++++++++++++ shopee.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/LICENSE b/LICENSE index 43dc269..c6bb9fa 100644 --- a/LICENSE +++ b/LICENSE @@ -19,20 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ------------------------------------------------------------------------------- - -Below are the third party license - ------------------------------------------------------------------------------- - -Axios - https://github.com/axios/axios - -# Copyright (c) 2014-present Matt Zabriskie & Collaborators - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/README.md b/README.md index 6f4015a..de3f145 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,24 @@ You can also using product URL from the browser url bar like: https://shopee.co.id/Audio-Technica-ATH-M20xBT-Wireless-Over-Ear-Headphones-i.5696604.15949131744 ``` +To get list of available products from shop: + +```js +import { get_shopee_shop_product } from './shopee.js' + +var response = get_shopee_shop_product('xiaomi.official.id'); // 51925611 +response.then((data) => { + data.forEach((item, index) => { console.log('%s: %s', index+1, item.name); }); +}); +``` + +It can also use shopid instead of username as an argument, e.g. `51925611`. + +Get shop argument from url: + +- `https://shopee.co.id/shop/51925611` +- `https://shopee.co.id/xiaomi.official.id` + ### Caution Often, the stocks of each tier variation (color, size, type, etc.) do not match at all with the display on the web page. But 0 stock (out of stock) always remains the same. The sold item countings also are not accurate or maybe it has a multiplier factor, etc. Don't know what is it. After all, we may only need information as to whether the item is in stock or out of stock. diff --git a/shopee.js b/shopee.js index ecb9301..3690a70 100644 --- a/shopee.js +++ b/shopee.js @@ -5,6 +5,7 @@ const DEFAULT_USER_AGENT = 'Best Deals for Affiliate'; const PRETEND_TO_BE_A_BROWSER = 'Mozilla/5.0 (X11; Linux x86_64) ' + 'AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/110.0.0.0 Safari/537.36'; + const IMAGE_PATH = 'https://cf.shopee.co.id/file/'; const endpoint = 'https://shopee.co.id/api/v4/flash_sale'; @@ -19,8 +20,12 @@ const instance = axios.create({ }); async function get_promotion() { - const url = endpoint + '/get_all_sessions?category_personalization_type=0'; - return await instance.get(url) + const url = endpoint + '/get_all_sessions'; + return await instance.get(url, { + params: { + category_personalization_type: 0 + } + }) .then(({ data }) => { var promotions = data.data.sessions; promotions = promotions.map((session_item) => { @@ -94,8 +99,12 @@ async function get_product(promotionid, itemids) { } export async function get_flash_catid() { - const url = endpoint + '/get_all_sessions?category_personalization_type=0'; - return await instance.get(url) + const url = endpoint + '/get_all_sessions'; + return await instance.get(url, { + param: { + category_personalization_type: 0 + } + }) .then(({ data }) => { var categories = data.data.sessions[0].categories; categories = categories.map((item) => { @@ -221,7 +230,7 @@ async function get_product_detail(instance, product_url) { }; instance.defaults.headers['user-agent'] = PRETEND_TO_BE_A_BROWSER; instance.defaults.headers['af-ac-enc-dat'] = null; - return await instance.get(url.addQuery(params)) + return await instance.get(url, { params: params }) .then(async(response) => { return await response.data; }) .catch((error) => { return { 'error': true, 'message': error.message }; }); } @@ -333,12 +342,45 @@ export async function get_shopee_product_detail(url) { }; } +async function get_shopid(username) { + const url = 'https://shopee.co.id/api/v4/shop/get_shop_base'; + const dummy_request_url = 'https://shopee.co.id/api/v4/pages/is_short_url/'; + instance.defaults.headers['user-agent'] = PRETEND_TO_BE_A_BROWSER; + instance.defaults.headers['cookie'] = await instance + .get(dummy_request_url, { params: { path: username } }) + .then((response) => { + return response.headers['set-cookie'].map((cookie) => { + return cookie.split(';')[0]; + }).join('; '); + }) + .catch((error) => { + console.error({ + 'error': true, 'message': `${error.message} (ref: dummy request)` + }); + return null; + }); + var data = await instance.get(url, { + params: { + username: username + } + }) + .then(({ data }) => { return data; }) + .catch((error) => { + console.error({ + 'error': true, 'message': `${error.message} (ref: ${url})` + }); + return null; + }); + instance.defaults.headers['user-agent'] = DEFAULT_USER_AGENT; + return data.data.shopid; +} + /* catalog_id can be available to retrieve by using cookie setup if catalog_id is not defined, all product should be collected */ -export async function get_shopee_shop_product(shop_id, catalog_id) { +export async function get_shopee_shop_product(shop, catalog_id) { const parse = (item) => { return { shopid: item.shopid, @@ -382,6 +424,12 @@ export async function get_shopee_shop_product(shop_id, catalog_id) { url: `https://shopee.co.id/product/${item.shopid}/${item.itemid}`, } }; + if (Number(shop)) { + var shop_id = shop; + } else { + var shop_id = await get_shopid(shop); + }; + // console.log(shop_id); var domain = 'https://shopee.co.id/api/v4/recommend/recommend'; var offset = 0; var limit = 150; // default web 30 @@ -401,13 +449,12 @@ export async function get_shopee_shop_product(shop_id, catalog_id) { tab_name: 'popular', upstream: '' }; - var url = domain.addQuery(params); - var data = await instance.get(url) + var data = await instance.get(domain, { params: params }) .then(({ data }) => { return { 'error': false, 'data': data.data.sections[0].data }; }) .catch((error) => { - return { 'error': true, 'message': error.message } + return { 'error': true, 'message': `${error.message} (ref: ${domain})` } }); if (!data.error) { offset += data.data.item.length; @@ -426,6 +473,7 @@ export async function get_shopee_shop_product(shop_id, catalog_id) { products = data; }; }; + // console.log(instance.defaults.headers); if (products.length) console.log('Items count: %s', products.length); return products; } \ No newline at end of file