put notes/d547d4b0-7745-4bec-9c96-eee1a7cd4452.json
Browse files
notes/d547d4b0-7745-4bec-9c96-eee1a7cd4452.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"id": "d547d4b0-7745-4bec-9c96-eee1a7cd4452",
|
3 |
+
"title": "marketplaceSearch.js",
|
4 |
+
"content": "/**\n * marketplaceSearchModule(defaultFuncs, api, ctx) -> (params, cb?) => Promise<{ listings, pageInfo, sessionId }>\n * Search Facebook Marketplace listings via GraphQL.\n *\n * Args:\n * - params: {\n * query?: string,\n * latitude?: number,\n * longitude?: number,\n * locationId?: string,\n * radiusKm?: number,\n * count?: number,\n * cursor?: string | null\n * }\n * Returns: {\n * listings: Array<{ id, title, price, location, image, deliveryTypes, categoryId, isSold, isLive }>,\n * pageInfo: { endCursor, hasNextPage },\n * sessionId: string | null\n * }\n *\n * Example:\n * const searchMarketplace = marketplaceSearchModule(defaultFuncs, api, ctx);\n * const result = await searchMarketplace({ query: \"bicycle\", count: 10 });\n *\n * Author: @tas33n, 27/08/2025\n */\n\nconst { ensureTokens, pickPart, postGraphQL } = require('../../../utils/marketplaceUtils');\n\nmodule.exports = function marketplaceSearchModule(defaultFuncs, api, ctx) {\n return function marketplaceSearch(params, callback) {\n let resolveFunc = () => {};\n let rejectFunc = () => {};\n const promise = new Promise((resolve, reject) => { resolveFunc = resolve; rejectFunc = reject; });\n\n if (typeof params === \"function\") { callback = params; params = {}; }\n params = params || {};\n callback = callback || function (err, data) { if (err) return rejectFunc(err); resolveFunc(data); };\n\n (async () => {\n try {\n if (typeof api.refreshFb_dtsg === \"function\") { try { await api.refreshFb_dtsg(); } catch {} }\n ensureTokens(ctx);\n\n const {\n query = \"\",\n latitude = 23.8103, // Dhaka default\n longitude = 90.4125,\n locationId = \"101889586519301\",\n radiusKm = 25,\n count = 24,\n cursor = null\n } = params;\n\n const variables = {\n buyLocation: { latitude, longitude },\n contextual_data: null,\n count, cursor,\n params: {\n bqf: { callsite: \"COMMERCE_MKTPLACE_WWW\", query },\n browse_request_params: {\n commerce_enable_local_pickup: true,\n commerce_enable_shipping: true,\n commerce_search_and_rp_available: true,\n commerce_search_and_rp_category_id: [],\n commerce_search_and_rp_condition: null,\n commerce_search_and_rp_ctime_days: null,\n filter_location_latitude: latitude,\n filter_location_longitude: longitude,\n filter_price_lower_bound: 0,\n filter_price_upper_bound: 214748364700,\n filter_radius_km: radiusKm\n },\n custom_request_params: {\n browse_context: null,\n contextual_filters: [],\n referral_code: null,\n referral_ui_component: null,\n saved_search_strid: null,\n search_vertical: \"C2C\",\n seo_url: null,\n serp_landing_settings: { virtual_category_id: \"\" },\n surface: \"SEARCH\",\n virtual_contextual_filters: []\n }\n },\n savedSearchID: null,\n savedSearchQuery: query,\n scale: 1,\n shouldIncludePopularSearches: false,\n topicPageParams: { location_id: locationId, url: null }\n };\n\n const DOC_ID = \"24146556225043257\";\n const payload = await postGraphQL(defaultFuncs, ctx, variables, DOC_ID);\n const selected = pickPart(payload, (p) => p?.data?.marketplace_search?.feed_units);\n const normalized = normalizeSearch(selected);\n return callback(null, normalized);\n } catch (err) {\n return callback(err);\n }\n })();\n\n return promise;\n };\n\n function n(x) { return Number.isFinite(+x) ? +x : null; }\n\n function normalizeSearch(payload) {\n const root = payload?.data?.marketplace_search?.feed_units;\n const edges = root?.edges ?? [];\n const listings = edges.map((e) => e?.node?.listing).filter(Boolean).map((l) => {\n const price = l?.listing_price || {};\n const formatted = price.formatted_amount ?? price.formatted_amount_zeros_stripped ?? price.amount;\n return {\n id: l?.id ?? null,\n title: l?.marketplace_listing_title ?? l?.custom_title ?? null,\n price: { amount: n(price.amount), amountString: price.amount ?? null, formatted: formatted ?? null },\n location: {\n city: l?.location?.reverse_geocode?.city ?? null,\n cityDisplay: l?.location?.reverse_geocode?.city_page?.display_name ?? null\n },\n image: l?.primary_listing_photo?.image?.uri ?? null,\n deliveryTypes: l?.delivery_types ?? [],\n categoryId: l?.marketplace_listing_category_id ?? null,\n isSold: !!l?.is_sold,\n isLive: !!l?.is_live\n };\n });\n\n const pageInfo = root?.page_info || {};\n return {\n listings,\n pageInfo: { endCursor: pageInfo?.end_cursor ?? null, hasNextPage: !!pageInfo?.has_next_page },\n sessionId: root?.session_id ?? null\n };\n }\n};",
|
5 |
+
"language": "javascript",
|
6 |
+
"createdAt": 1756349598248,
|
7 |
+
"updatedAt": 1756349598248
|
8 |
+
}
|