GithubHelp home page GithubHelp logo

Comments (1)

solcabz avatar solcabz commented on June 4, 2024

image

const express = require('express');
const ytdl = require('ytdl-core');

const app = express();

app.get('/', async (req, res) => {
  res.sendFile('index.html', { root: './' });
});

app.get('/videoInfo', async (req, res) => {
  const { videoUrl } = req.query;

  if (!videoUrl) {
    return res.status(400).json({ error: 'Please provide a video URL.' });
  }

  try {
    const info = await ytdl.getInfo(videoUrl);
    console.log('Video Info:', info); // Log the entire 'info' object to the console

    const title = info.videoDetails.title;
    const uploadDate = info.videoDetails.uploadDate;
    const views = info.videoDetails.viewCount;
    const duration = parseInt(info.videoDetails.lengthSeconds) / 60;
    
    // Fetching likes and dislikes with default values of 0
    const likes = info.videoDetails && info.videoDetails.likes ;
    const dislikes = info.videoDetails && info.videoDetails.dislikes ? parseInt(info.videoDetails.dislikes) : 0;
    
    const separateVideoCounter = info.videoDetails.isLiveContent ? 'Live Stream' : 'Single Video';

    const videoInfo = {
      title,
      uploadDate,
      views,
      duration: duration.toFixed(2),
      likes,
      dislikes, // Adding dislikes to the videoInfo object
      separateVideoCounter,
    };

    res.json(videoInfo);
  } catch (error) {
    res.status(500).json({ error: 'Could not fetch video information.' });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

from node-ytdl-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.