Count the videos in a Youtube Playlist

12

1

Challenge

Given a Youtube playlist, output the number of videos in it.

I/O

The playlist will be given as an URL; for example, https://www.youtube.com/playlist?list=PL_3jnZNPYn7qUatgDYBS5NShkfXoehSRC (this is a playlist a friend sent me to try to get me to listen to K-Pop because she's obsessed with it and I don't like it :P)

The link to a playlist is of the form https://www.youtube.com/playlist?list=<id>. The input is the URL, not the ID.

The output is to be a single integer indicating how many videos are in this playlist. You can either output the total number of videos or the number of non-deleted videos, whichever one is more golfy for you.

Test Cases

https://www.youtube.com/playlist?list=PL_3jnZNPYn7qUatgDYBS5NShkfXoehSRC -> 12
https://www.youtube.com/playlist?list=PLIEbITAtGBebWGyBZQlkiMXwt30WqG9Bd -> 114 OR 1

Feel free to add more test cases.

HyperNeutrino

Posted 2017-05-25T14:03:38.753

Reputation: 26 575

6Hats off for finding the sneakiest way of asking people to make you a library to count videos in a YouTube playlist. – Stevoisiak – 2017-05-25T17:47:07.807

2I just resisted the urge to add a rickroll playlist. Or the "Dank Memes" playlist. – CAD97 – 2017-05-25T20:29:08.490

Off topic: I'd be more interested in a way list all videos a youtube channel has to offer. The source code doesnt seem to include this naturally. – phil294 – 2017-06-13T11:24:38.110

@Blauhirn That's an interesting idea. Maybe you could make a challenge about that. – HyperNeutrino – 2017-06-13T12:00:33.067

Answers

9

05AB1E, 22 bytes

Assumes that the URL is passed without the https://.

Code:

.w’®Ã-ƒ¢’¡`"<li>"¡2è#¬

Uses the 05AB1E encoding. Cannot be tried online, because of internet access.

Explanation:

.w                       # Receive implicit input and read all data from the url
  ’®Ã-ƒ¢’¡               # Split on the string "header-details"
          `              # Flatten once which leaves the last element on top of the stack
           "<li>"¡       # Split on the string "<li>"
                  2è     # Get the third element
                    #    # Split on spaces
                     ¬   # Retrieve the first element 

This is what I get when running in the command line:

enter image description here

Adnan

Posted 2017-05-25T14:03:38.753

Reputation: 41 965

1What sort of command line is that? I haven't seen it before. – Okx – 2017-05-25T14:47:17.380

2

@Okx It's cmder

– Adnan – 2017-05-25T14:50:11.490

How does it work? – Stevoisiak – 2017-05-25T17:52:18.500

@StevenVascellaro Thanks for the reminder, I have added an explanation. – Adnan – 2017-05-25T17:56:43.333

6

Scala 2.12, 138 bytes

print("""<li>.*?</li><li>(\d+).*?</li>""".r.findFirstMatchIn(scala.io.Source.fromURL(args(0),"utf-8").getLines.mkString("")).get.group(1))

Save it to something.scala and run by scala something.scala {url}.

enter image description here

Aria Ax

Posted 2017-05-25T14:03:38.753

Reputation: 321

6

JS (ES6), 68 bytes

x=>fetch(x).then(x=>x.text()).then(x=>alert(x.match(/(\d+) vi/)[1]))

Paste this in to your console on youtube.com to try:

(x=>fetch(x).then(x=>x.text()).then(x=>alert(x.match(/(\d+) vi/)[1])))(prompt("Enter the URL"))

user69783

Posted 2017-05-25T14:03:38.753

Reputation:

6

MATL, 15 bytes

Thanks to Hyper Neutrino for finding a mistake, now corrected.

Xi'wner">\n'XXn

The input is a string enclosed with single quotes. The output is the number of undeleted videos.

Here's an example run with the two test cases. As of June 13, 2017 the number of undeleted videos in the playlists are 12 and 1 respectively.

enter image description here

How it works

Xi          % Read string containing URL and get contents as a string
'wner">\n'  % Push this string
XX          % Regexp matching. Gives a cell array with the matched strings
n           % Number of elements. Implicitly display

Luis Mendo

Posted 2017-05-25T14:03:38.753

Reputation: 87 464

Ha, clever regex! – rubik – 2017-05-25T16:51:09.880

What are you using to capture the screen? – Matt – 2017-05-25T17:08:35.753

@Matt LICEcap for Windows

– Luis Mendo – 2017-05-25T17:13:24.240

Question: What if I create a playlist called "wner"? Will it still work? – HyperNeutrino – 2017-06-13T03:01:08.930

@HyperNeutrino Good point. I have corrected it. BTW, the number of videos in the first list has changed – Luis Mendo – 2017-06-13T09:10:48.753

Nice fix. And yes, thanks for pointing that out. That would make sense; that's my friend's K-Pop playlist which she makes her friends listen to (including me >.<) and she keeps adding to it :P – HyperNeutrino – 2017-06-13T12:01:20.073

@HyperNeutrino Well, in this case it looks like she removed one track :-) – Luis Mendo – 2017-06-13T13:38:38.970

Oh? Hm, that's weird. I'll check and maybe ask her right now. – HyperNeutrino – 2017-06-13T14:17:35.140

4

bash + curl + gnu grep, 32 bytes

Finally giving Code Golf a go:

curl -s $1|grep -Po \\d+(?= vid)
  • Use curl -s to download without any extra output.
  • Pipe the output to grep (perl regex) which will output only the match
    • Match the first number that proceeds " vid"

This is golfed from how I would do this practically.

Sudo Bash

Posted 2017-05-25T14:03:38.753

Reputation: 61

Nice first submission! – HyperNeutrino – 2017-05-25T19:41:36.373

3

PowerShell, 55 Bytes

"$(iwr "$args")"-match'(?s)(\d+) vid'>$null;$matches[1]

Browse to the site. Convert the object to a string which is the html source. Parse out the string that has a number preceding "vid" with regex.

Logic works with both test cases.

Matt

Posted 2017-05-25T14:03:38.753

Reputation: 1 075

0

Haskell 161 bytes

import Network.Curl.Download
import qualified Data.ByteString.Char8 as B
z=do u<-getLine;(Right b)<-openURI u;putStrLn$show$length$B.findSubstrings(B.pack"<tr")b

Davide Spataro

Posted 2017-05-25T14:03:38.753

Reputation: 211