11
\$\begingroup\$

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.

\$\endgroup\$
4
  • 7
    \$\begingroup\$ Hats off for finding the sneakiest way of asking people to make you a library to count videos in a YouTube playlist. \$\endgroup\$
    – Stevoisiak
    Commented May 25, 2017 at 17:47
  • 2
    \$\begingroup\$ I just resisted the urge to add a rickroll playlist. Or the "Dank Memes" playlist. \$\endgroup\$
    – CAD97
    Commented May 25, 2017 at 20:29
  • \$\begingroup\$ 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. \$\endgroup\$
    – phil294
    Commented Jun 13, 2017 at 11:24
  • \$\begingroup\$ @Blauhirn That's an interesting idea. Maybe you could make a challenge about that. \$\endgroup\$
    – hyperneutrino
    Commented Jun 13, 2017 at 12:00

7 Answers 7

8
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What sort of command line is that? I haven't seen it before. \$\endgroup\$
    – Okx
    Commented May 25, 2017 at 14:47
  • 2
    \$\begingroup\$ @Okx It's cmder \$\endgroup\$
    – Adnan
    Commented May 25, 2017 at 14:50
  • \$\begingroup\$ How does it work? \$\endgroup\$
    – Stevoisiak
    Commented May 25, 2017 at 17:52
  • \$\begingroup\$ @StevenVascellaro Thanks for the reminder, I have added an explanation. \$\endgroup\$
    – Adnan
    Commented May 25, 2017 at 17:56
6
\$\begingroup\$

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
\$\endgroup\$
8
  • \$\begingroup\$ Ha, clever regex! \$\endgroup\$
    – rubik
    Commented May 25, 2017 at 16:51
  • \$\begingroup\$ What are you using to capture the screen? \$\endgroup\$
    – Matt
    Commented May 25, 2017 at 17:08
  • \$\begingroup\$ @Matt LICEcap for Windows \$\endgroup\$
    – Luis Mendo
    Commented May 25, 2017 at 17:13
  • \$\begingroup\$ Question: What if I create a playlist called "wner"? Will it still work? \$\endgroup\$
    – hyperneutrino
    Commented Jun 13, 2017 at 3:01
  • \$\begingroup\$ @HyperNeutrino Good point. I have corrected it. BTW, the number of videos in the first list has changed \$\endgroup\$
    – Luis Mendo
    Commented Jun 13, 2017 at 9:10
4
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Nice first submission! \$\endgroup\$
    – hyperneutrino
    Commented May 25, 2017 at 19:41
4
\$\begingroup\$

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"))
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.