GithubHelp home page GithubHelp logo

codedotjs / youtube-playlist Goto Github PK

View Code? Open in Web Editor NEW
86.0 8.0 19.0 401 KB

:snowflake: Extract links, ids, and names from a youtube playlist

License: MIT License

JavaScript 100.00%
nodejs npm-package youtube youtube-api scraper api links urls ids names

youtube-playlist's Introduction



Extract links, ids, durations and names from a youtube playlist


Install

$ npm install --save youtube-playlist

Usage

  • urls
const ytlist = require('youtube-playlist');

const url = 'https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704';

ytlist(url, 'url').then(res => {
  console.log(res);
  /* Object
  { data:
   { playlist:
      [ 'https://youtube.com/watch?v=bgU7FeiWKzc',
        'https://youtube.com/watch?v=3PUVr8jFMGg',
        'https://youtube.com/watch?v=3pXVHRT-amw',
        'https://youtube.com/watch?v=KOVc5o5kURE' ] } }
   */
});

// or

ytlist(url, 'url').then(res => {
  console.log(res.data.playlist);
  /* Array
  [ 'https://youtube.com/watch?v=bgU7FeiWKzc',
  'https://youtube.com/watch?v=3PUVr8jFMGg',
  'https://youtube.com/watch?v=3pXVHRT-amw',
  'https://youtube.com/watch?v=KOVc5o5kURE' ]
   */
});
  • names
ytlist(url, 'name').then(res => {
  console.log(res);
  /*
  { data:
   { playlist:
      [ 'Singleton Design Pattern - Beau teaches JavaScript',
        'Observer Design Pattern - Beau teaches JavaScript',
        'Module Design Pattern - Beau teaches JavaScript',
        'Mediator Design Pattern - Beau teaches JavaScript' ] } }
   */
});
  • ids
ytlist(url, 'id').then(res => {
  console.log(res);
  // => { data: { playlist: [ 'bgU7FeiWKzc', '3PUVr8jFMGg', '3pXVHRT-amw', 'KOVc5o5kURE' ] } }
})
  • durations
ytlist(url, 'duration').then(res => {
  console.log(res);
  // => { data: { playlist: [ 291, 237, 164, 309 ] } }
})
  • multiple details
ytlist(url).then(res => {
  console.log(res.data);
  // = [{}]
});

// or

ytlist(url, ['id', 'name', 'url']).then(res => {
  console.log(res.data);
  /* Array
  [ { id: 'bgU7FeiWKzc',
    name: 'Singleton Design Pattern - Beau teaches JavaScript',
    url: 'https://youtube.com/watch?v=bgU7FeiWKzc',
    isPrivate: false },
  { id: '3PUVr8jFMGg',
    name: 'Observer Design Pattern - Beau teaches JavaScript',
    url: 'https://youtube.com/watch?v=3PUVr8jFMGg',
    isPrivate: false },
  { id: '3pXVHRT-amw',
    name: 'Module Design Pattern - Beau teaches JavaScript',
    url: 'https://youtube.com/watch?v=3pXVHRT-amw',
    isPrivate: false },
  { id: 'KOVc5o5kURE',
    name: 'Mediator Design Pattern - Beau teaches JavaScript',
    url: 'https://youtube.com/watch?v=KOVc5o5kURE',
    isPrivate: false } ]
   */
});

Notice: In multiple details - another prop will be added. isPrivate will be true when the video is private (for not loggedin user).

API

ytlist(url, opts)

opts

  • id : returns only ids of all the videos present in a playlist

  • url : returns only urls of all the videos present in a playlist

  • name : return only name of the videos present in a playlist

  • duration : return only duration (in seconds) of the videos present in a playlist

  • Passing opts either as url or an array of options ['id', 'name', 'url', 'duration'] returns all the details.

Type of

  • url : string

  • opts : string or array

NOTE

  • This api already supports url-redirection, so you are free to use the shortened url.

  • For data extraction, you can either choose -

    • playlist url
    • url of the content from playlist #1

Related

  • Pufetch : The best youtube playlist url scrapper and exporter!

License

MIT © Rishi Giri

youtube-playlist's People

Contributors

codedotjs avatar lefuturiste avatar moshfeu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

youtube-playlist's Issues

Limited to One Playlist

If I try to use this module for multiple playlist, it causes an error. I even tried calling the module in 2 different const, but it just causes the same issues.

stuck on "Cross-Origin Request Blocked" warning

I'm trying to use this module in a Nuxt JS app and am facing this error while developing locally:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Some time the playlist isn't loading to data

ytlist(urls, ['name', 'url']).then(res => {
	playlist['name'] = res.data.name;
	playlist['list'] = res.data.playlist;
	cache['url'] , cache['name'] = "";
	var s = setInterval(() => {
		if (typeof playlist['list'] !== 'undefined' && playlist['list'].length > 0){
			clearInterval(s);
			for([k,v] of Object.entries(playlist['list'])){
				var titles = Buffer.from(v.name).toString('base64');
				cache['url']+=v.url+",";
				cache['name']+=titles+",";
			}
			cache['reg1'] = LZString.compress(cache['url']);
			cache['reg2'] = LZString.compress(cache['name']);
		}		
	}, 100);
});

input: urls = https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK
output: {name: '', playlist: Array(0)}

Does it require an API key?

I don't see anywhere that this library requires a Youtube API key or a client ID, so I want to ask if it require one of those and where to use them?

If it doesn't require them, then is there a limit for the usage or limit for number of users?

No longer works

I have finally started to get back into coding a found out this no longer works.

const commando = require('discord.js-commando');
const ytlist = require('youtube-playlist');
var fs = require('fs');
var path = require('path');
const { exec } = require("child_process");

class PlaylistChannelCommand extends commando.Command {
    constructor(client) {
        super(client, {
            name: 'playlist',
            group: 'music',
            memberName: 'playlist',
            description: 'Adds playlist to the random music bot',
            args: [
                {
                    key: 'link',
                    prompt: 'What playlist do you want to add?',
                    type: 'string'
                }
            ]
        });
    }

    async run(message, { link }) {

        function restart() {
            exec("pm2 restart music", (error, stdout, stderr) => {
                if (error) {
                    console.log(`error: ${error.message}`);
                }
                if (stderr) {
                    console.log(`stderr: ${stderr}`);
                }
                console.log(`stdout: ${stdout}`);
            });
        }

        var pathToFile = path.resolve(__dirname, "../../queue.txt");

        ytlist(link, 'url').then(res => {
            let pls = res.data.playlist.toString().split(',').join('\n');
            fs.appendFile(pathToFile, "\n" + pls, 'utf8',
                function (err) {
                    if (err) throw err;
                    console.log("Data is appended to file successfully.");
                    restart();
                    message.channel.send("Success, " + res.data.playlist.length + ' songs added!');
                });
        });
    }
}

module.exports = PlaylistChannelCommand;

Running this command adds no urls. I am a little more comfortable coding and would like to try and help, however, I don't know how much help I could be.

Sorry man Doesn't seem like it's working anymore

Hi, I tried running your example code on npm through RunKit and that wouldn't even work, it just returns an empty Array/list with no data in it. I have got it to give me the names a few times, but it's a fluke and I didn't even have the name tag in the function.

Be able to get playlist information from a video that is attached to a playlist

It would be helpful if this plugin would be also capable to fetch playlist information from a URL where a video is already selected like:
https://www.youtube.com/watch?v=bgU7FeiWKzc&list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704&index=1
instead of the normal:
https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704

Limit to 100 videos

If the Youtube playlist contains more than 100 videos, ytlist returns only 100.

Add playlist title

At the moment, you cannot get the title of the playlist from the provided URL. Would be nice to be able to its title as well.

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.