-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.ts
32 lines (30 loc) · 1.23 KB
/
fetch.ts
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
const PREFIX = 'https://m.facebook.com/photo/view_full_size/?fbid=';
/**
* Obtain the redirect URL for a given picture FBID.
* @param fbid FBID of the picture to follow
* @param headers headers to send in the request
*/
export async function fetchRedirect(fbid: string, headers: Headers): Promise<string | null> {
const response = await fetchFacebook(PREFIX + fbid, headers);
const body = await response.text();
const url = /(?<=url=).*?(?=")/.exec(body);
// For an invalid FBID, Facebook returns a page with a url=https://mbasic.facebook.com/home.php parameter somewhere
if (url === null || url[0] === 'https://mbasic.facebook.com/home.php') {
console.warn(`Cannot fetch URL for picture with FBID ${fbid}. Try accessing ${PREFIX}${fbid} in your browser and check if you can see the picture.`);
return null;
}
return url[0].replace(/&/g, '&');
}
/**
* Make a request to Facebook, passing the necessary headers along.
* @param url URL to retrieve
* @param headers headers to send
* @returns promise with the response to the request
*/
export async function fetchFacebook(url: string, headers: Headers): Promise<Response> {
return await fetch(url, {
method: 'GET',
credentials: 'include',
headers,
});
}