Skip to content

Instantly share code, notes, and snippets.

@ueaner
Forked from agungjk/[slug].js
Created April 14, 2022 19:49
Show Gist options
  • Save ueaner/0045653fb0d9663551386274f6448de6 to your computer and use it in GitHub Desktop.
Save ueaner/0045653fb0d9663551386274f6448de6 to your computer and use it in GitHub Desktop.

Revisions

  1. Agung Jati Kusumo revised this gist Sep 14, 2020. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions [slug].js
    Original file line number Diff line number Diff line change
    @@ -11,17 +11,6 @@ export default async (req, res) => {
    return;
    }

    const fs = require("fs");
    const dirTmp = '/tmp/.local-chromium';
    if (!fs.existsSync(dirTmp)){
    fs.mkdirSync(dirTmp);
    }

    const dirCache = '/tmp/.chromium-cache';
    if (!fs.existsSync(dirCache)){
    fs.mkdirSync(dirCache);
    }

    const browser = await puppeteer.launch(
    process.env.NODE_ENV === 'production'
    ? {
  2. Agung Jati Kusumo renamed this gist Sep 14, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Agung Jati Kusumo renamed this gist Sep 14, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Agung Jati Kusumo created this gist Sep 14, 2020.
    58 changes: 58 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    const puppeteer = require('puppeteer-core');
    const cheerio = require('cheerio');
    const chrome = require('chrome-aws-lambda');

    export default async (req, res) => {
    const slug = req?.query?.slug;
    if (!slug) {
    res.statusCode = 200
    res.setHeader('Content-Type', 'application/json')
    res.end(JSON.stringify({ id: null }))
    return;
    }

    const fs = require("fs");
    const dirTmp = '/tmp/.local-chromium';
    if (!fs.existsSync(dirTmp)){
    fs.mkdirSync(dirTmp);
    }

    const dirCache = '/tmp/.chromium-cache';
    if (!fs.existsSync(dirCache)){
    fs.mkdirSync(dirCache);
    }

    const browser = await puppeteer.launch(
    process.env.NODE_ENV === 'production'
    ? {
    args: chrome.args,
    executablePath: await chrome.executablePath,
    headless: chrome.headless,
    }
    : {}
    );

    const page = await browser.newPage();
    page.setUserAgent('Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/28.2725; U; ru) Presto/2.8.119 Version/11.10');
    await page.goto(`https://m.youtube.com/${slug}/videos`);

    let content = await page.content();
    var $ = cheerio.load(content);
    $.prototype.exists = function (selector) {
    return this.find(selector).length > 0;
    }

    let id = null;
    const isLive = $('body').exists('[data-style="LIVE"]');
    if (isLive) {
    const url = $('ytm-compact-video-renderer .compact-media-item-image').attr('href');
    const arr = url.split('?v=');
    id = arr[1];
    }

    await browser.close();

    res.statusCode = 200
    res.setHeader('Content-Type', 'application/json')
    res.end(JSON.stringify({ id }))
    }