refactor: remove facebook cookie overrides
This commit is contained in:
@@ -25,7 +25,6 @@ const FACEBOOK_COOKIE_CONFIG: CookieConfig = {
|
|||||||
name: "Facebook",
|
name: "Facebook",
|
||||||
domain: ".facebook.com",
|
domain: ".facebook.com",
|
||||||
envVar: "FACEBOOK_COOKIE",
|
envVar: "FACEBOOK_COOKIE",
|
||||||
filePath: "./cookies/facebook.json",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
interface FacebookAdNode {
|
interface FacebookAdNode {
|
||||||
@@ -211,20 +210,10 @@ export function parseFacebookCookieString(cookieString: string): Cookie[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load Facebook cookies with priority: URL param > ENV var > file
|
* Load Facebook cookies from FACEBOOK_COOKIE
|
||||||
* @param cookiesSource - Optional cookie JSON string from URL parameter (highest priority)
|
|
||||||
* @param _cookiePath - Deprecated, uses default path from config
|
|
||||||
*/
|
*/
|
||||||
export async function ensureFacebookCookies(
|
export async function ensureFacebookCookies(): Promise<Cookie[]> {
|
||||||
cookiesSource?: string,
|
return ensureCookies(FACEBOOK_COOKIE_CONFIG);
|
||||||
_cookiePath?: string,
|
|
||||||
): Promise<Cookie[]> {
|
|
||||||
return ensureCookies(
|
|
||||||
_cookiePath
|
|
||||||
? { ...FACEBOOK_COOKIE_CONFIG, filePath: _cookiePath }
|
|
||||||
: FACEBOOK_COOKIE_CONFIG,
|
|
||||||
cookiesSource,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HttpError extends Error {
|
class HttpError extends Error {
|
||||||
@@ -825,11 +814,8 @@ export default async function fetchFacebookItems(
|
|||||||
REQUESTS_PER_SECOND = 1,
|
REQUESTS_PER_SECOND = 1,
|
||||||
LOCATION = "toronto",
|
LOCATION = "toronto",
|
||||||
MAX_ITEMS = 25,
|
MAX_ITEMS = 25,
|
||||||
cookiesSource?: string,
|
|
||||||
cookiePath?: string,
|
|
||||||
) {
|
) {
|
||||||
// Load Facebook cookies with priority: URL param > ENV var > file
|
const cookies = await ensureFacebookCookies();
|
||||||
const cookies = await ensureFacebookCookies(cookiesSource, cookiePath);
|
|
||||||
|
|
||||||
// Format cookies for HTTP header
|
// Format cookies for HTTP header
|
||||||
const domain = "www.facebook.com";
|
const domain = "www.facebook.com";
|
||||||
@@ -871,7 +857,7 @@ export default async function fetchFacebookItems(
|
|||||||
);
|
);
|
||||||
if (err.status === 400 || err.status === 401 || err.status === 403) {
|
if (err.status === 400 || err.status === 401 || err.status === 403) {
|
||||||
console.warn(
|
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 [];
|
return [];
|
||||||
@@ -914,11 +900,8 @@ export default async function fetchFacebookItems(
|
|||||||
*/
|
*/
|
||||||
export async function fetchFacebookItem(
|
export async function fetchFacebookItem(
|
||||||
itemId: string,
|
itemId: string,
|
||||||
cookiesSource?: string,
|
|
||||||
_cookiePath?: string,
|
|
||||||
): Promise<FacebookListingDetails | null> {
|
): Promise<FacebookListingDetails | null> {
|
||||||
// Load Facebook cookies - required for Facebook Marketplace access
|
const cookies = await ensureFacebookCookies();
|
||||||
const cookies = await ensureFacebookCookies(cookiesSource, _cookiePath);
|
|
||||||
|
|
||||||
// Format cookies for HTTP header
|
// Format cookies for HTTP header
|
||||||
const cookiesHeader = formatCookiesForHeader(cookies, "www.facebook.com");
|
const cookiesHeader = formatCookiesForHeader(cookies, "www.facebook.com");
|
||||||
@@ -956,10 +939,7 @@ export async function fetchFacebookItem(
|
|||||||
case 401:
|
case 401:
|
||||||
case 403:
|
case 403:
|
||||||
console.warn(
|
console.warn(
|
||||||
"Authentication error: Invalid or expired cookies. Please update ./cookies/facebook.json with fresh session cookies.",
|
"Authentication error: Invalid or expired cookies. Update FACEBOOK_COOKIE with a fresh raw Cookie header string.",
|
||||||
);
|
|
||||||
console.warn(
|
|
||||||
"Try logging out and back into Facebook, then export fresh cookies.",
|
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 404:
|
case 404:
|
||||||
|
|||||||
@@ -139,6 +139,10 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("should handle authentication errors", async () => {
|
test("should handle authentication errors", async () => {
|
||||||
|
const originalWarn = console.warn;
|
||||||
|
const warnMock = mock(() => {});
|
||||||
|
console.warn = warnMock;
|
||||||
|
|
||||||
global.fetch = mock(() =>
|
global.fetch = mock(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
ok: false,
|
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(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 () => {
|
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();
|
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);
|
expect(attempts).toBe(2);
|
||||||
// Should eventually succeed after retry
|
// 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");
|
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).not.toBeNull();
|
||||||
expect(result?.title).toBe("Working Item");
|
expect(result?.title).toBe("Working Item");
|
||||||
expect(result?.listingPrice?.amountFormatted).toBe("$299.00");
|
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();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user