refactor: remove facebook cookie overrides

This commit is contained in:
2026-04-21 21:45:42 -04:00
parent 847ce28590
commit a7a5eca7ad
2 changed files with 25 additions and 34 deletions

View File

@@ -25,7 +25,6 @@ const FACEBOOK_COOKIE_CONFIG: CookieConfig = {
name: "Facebook",
domain: ".facebook.com",
envVar: "FACEBOOK_COOKIE",
filePath: "./cookies/facebook.json",
};
interface FacebookAdNode {
@@ -211,20 +210,10 @@ export function parseFacebookCookieString(cookieString: string): Cookie[] {
}
/**
* Load Facebook cookies with priority: URL param > ENV var > file
* @param cookiesSource - Optional cookie JSON string from URL parameter (highest priority)
* @param _cookiePath - Deprecated, uses default path from config
* Load Facebook cookies from FACEBOOK_COOKIE
*/
export async function ensureFacebookCookies(
cookiesSource?: string,
_cookiePath?: string,
): Promise<Cookie[]> {
return ensureCookies(
_cookiePath
? { ...FACEBOOK_COOKIE_CONFIG, filePath: _cookiePath }
: FACEBOOK_COOKIE_CONFIG,
cookiesSource,
);
export async function ensureFacebookCookies(): Promise<Cookie[]> {
return ensureCookies(FACEBOOK_COOKIE_CONFIG);
}
class HttpError extends Error {
@@ -825,11 +814,8 @@ export default async function fetchFacebookItems(
REQUESTS_PER_SECOND = 1,
LOCATION = "toronto",
MAX_ITEMS = 25,
cookiesSource?: string,
cookiePath?: string,
) {
// Load Facebook cookies with priority: URL param > ENV var > file
const cookies = await ensureFacebookCookies(cookiesSource, cookiePath);
const cookies = await ensureFacebookCookies();
// Format cookies for HTTP header
const domain = "www.facebook.com";
@@ -871,7 +857,7 @@ export default async function fetchFacebookItems(
);
if (err.status === 400 || err.status === 401 || err.status === 403) {
console.warn(
"This might indicate invalid or expired cookies. Please update ./cookies/facebook.json with fresh session cookies.",
"This might indicate invalid or expired cookies. Update FACEBOOK_COOKIE with a fresh raw Cookie header string.",
);
}
return [];
@@ -914,11 +900,8 @@ export default async function fetchFacebookItems(
*/
export async function fetchFacebookItem(
itemId: string,
cookiesSource?: string,
_cookiePath?: string,
): Promise<FacebookListingDetails | null> {
// Load Facebook cookies - required for Facebook Marketplace access
const cookies = await ensureFacebookCookies(cookiesSource, _cookiePath);
const cookies = await ensureFacebookCookies();
// Format cookies for HTTP header
const cookiesHeader = formatCookiesForHeader(cookies, "www.facebook.com");
@@ -956,10 +939,7 @@ export async function fetchFacebookItem(
case 401:
case 403:
console.warn(
"Authentication error: Invalid or expired cookies. Please update ./cookies/facebook.json with fresh session cookies.",
);
console.warn(
"Try logging out and back into Facebook, then export fresh cookies.",
"Authentication error: Invalid or expired cookies. Update FACEBOOK_COOKIE with a fresh raw Cookie header string.",
);
break;
case 404:

View File

@@ -139,6 +139,10 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
});
test("should handle authentication errors", async () => {
const originalWarn = console.warn;
const warnMock = mock(() => {});
console.warn = warnMock;
global.fetch = mock(() =>
Promise.resolve({
ok: false,
@@ -150,8 +154,15 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
}),
);
const result = await fetchFacebookItem("123", mockCookies);
try {
const result = await fetchFacebookItem("123");
expect(result).toBeNull();
expect(warnMock).toHaveBeenCalledWith(
"Authentication error: Invalid or expired cookies. Update FACEBOOK_COOKIE with a fresh raw Cookie header string.",
);
} finally {
console.warn = originalWarn;
}
});
test("should handle item not found", async () => {
@@ -166,7 +177,7 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
}),
);
const result = await fetchFacebookItem("nonexistent", mockCookies);
const result = await fetchFacebookItem("nonexistent");
expect(result).toBeNull();
});
@@ -226,7 +237,7 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
});
});
const _result = await fetchFacebookItem("123", mockCookies);
const _result = await fetchFacebookItem("123");
expect(attempts).toBe(2);
// Should eventually succeed after retry
});
@@ -274,7 +285,7 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
}),
);
const result = await fetchFacebookItem("456", mockCookies);
const result = await fetchFacebookItem("456");
expect(result?.listingStatus).toBe("SOLD");
});
@@ -326,7 +337,7 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
}),
);
const result = await fetchFacebookItem("789", mockCookies);
const result = await fetchFacebookItem("789");
expect(result).not.toBeNull();
expect(result?.title).toBe("Working Item");
expect(result?.listingPrice?.amountFormatted).toBe("$299.00");
@@ -345,7 +356,7 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
}),
);
const result = await fetchFacebookItem("error", mockCookies);
const result = await fetchFacebookItem("error");
expect(result).toBeNull();
});
});