nurlresolver
Version:
url resolver for node
62 lines • 3.04 kB
JavaScript
import { BaseUrlResolver } from "../BaseResolver.js";
import { CookieJar } from 'tough-cookie';
import { parseGoogleFileId } from "../utils/helper.js";
export class GDriveResolver extends BaseUrlResolver {
googleDriveId;
constructor() {
super({
domains: [/https?:\/\/(drive|docs|drive\.usercontent)\.google\.com/],
speedRank: 99
});
this.googleDriveId = '';
}
async resolveInner(_urlToResolve) {
const links = [];
const googleDriveId = parseGoogleFileId(_urlToResolve);
if (googleDriveId) {
const normalizeDriveUrl = `https://drive.google.com/uc?id=${googleDriveId}&export=download`;
this.googleDriveId = googleDriveId;
const cookieJar = new CookieJar();
const response = await this.gotInstance(normalizeDriveUrl, {
cookieJar,
followRedirect: false
});
const title = this.scrapeInnerText(response.body, 'span.uc-name-size a');
const downloadlink1 = this.scrapeLinkHref(response.body, 'a#uc-download-link');
const driveCookie = cookieJar.getCookiesSync(normalizeDriveUrl).find(x => x.domain === 'drive.google.com');
//DEV NOTE: While using cookieJar for got, somehow the generated link in the end is not streamable.
//So, that is the reason for this custom cookie implementation.
if (driveCookie) {
const nextRequestCookies = `${driveCookie.key}=${driveCookie.value}`;
const reqMediaConfirm = await this.gotInstance('https://drive.google.com' + downloadlink1, {
headers: {
cookie: nextRequestCookies
},
followRedirect: false
});
const link = reqMediaConfirm.headers.location;
const result = { link, title, isPlayable: true };
links.push(result);
}
}
return links;
}
async fillMetaInfo(resolveMediaItem) {
// const headerswithrange = resolveMediaItem.headers || {};
// headerswithrange['Range'] = 'bytes=0-0';
// const rangeresponse = await this.gotInstance(resolveMediaItem.link, {
// headers: headerswithrange
// });
const gdriveid = this.googleDriveId;
//google doesn't support HTTP HEAD, so another way to find the size and other meta infor is here.
const result = await this.gotInstance(`https://content.googleapis.com/drive/v2beta/files/${gdriveid}?fields=fileSize%2CmodifiedDate%2CmimeType&supportsTeamDrives=true&key=AIzaSyC1eQ1xj69IdTMeii5r7brs3R90eck-m7k`, {
headers: {
"X-Origin": "https://drive.google.com",
},
}).json();
resolveMediaItem.size = result.fileSize;
resolveMediaItem.lastModified = result.modifiedDate.toString();
resolveMediaItem.contentType = result.mimeType;
}
}
//# sourceMappingURL=gdrive.js.map