5

It seems that sometime in the past 2 or 3 weeks, the Playlist class seems to have stopped working for me. I've tried the following code snippet adapted from their GitHub:

from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")

print(len(playlist.video_urls))
for url in playlist.video_urls:
    print(url)

I've tried multiple public playlists but they all produced an empty list object. This code was working about 3 weeks ago. Also, I am running Python 3.7.6 and the latest version of PyTube3 (9.6.4).

Is there something that I'm doing wrong?

Maelstrom
  • 958
  • 3
  • 13
  • I have since raised it on [their GitHub](https://github.com/get-pytube/pytube3/issues/89). Hopefully someone answers soon. If not, I'll start digging in the source to see if I can find anything. – Maelstrom Jul 02 '20 at 06:08

1 Answers1

6

I looked into the source a bit and it seemed that it would load the html and then perform a regex search for /watch/v=... URLs. The regex expression used was href=\"(/watch\?v=[\w-]*) but it couldn't find any matches since YouTube must have updated their html. They now send the watch URLs in a JSON object. So we should look for that instead.

Here is something that works:

from pytube import Playlist
import re

playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")

print(len(playlist.video_urls))
for url in playlist.video_urls:
    print(url)

Hope this is useful until the next patch.

Maelstrom
  • 958
  • 3
  • 13
  • 1
    nice, did you submit a PR? – mitchus Jul 09 '20 at 17:20
  • 1
    @mitchus yeah, my first one but I assume once I submit it, I don't need to do anything. I assume I just have to wait for it to be accepted. – Maelstrom Jul 10 '20 at 06:33
  • No, this isn't working me. I'm not getting any links and also no error message. – bobsmith76 Aug 17 '20 at 16:26
  • 1
    @bobsmith76, if you're running the same Python and PyTube version as me and you have the same link, I have no idea what could be wrong. This same setup was working for me yesterday. – Maelstrom Aug 17 '20 at 16:29
  • btw, what version of python and pytube are you using? – bobsmith76 Aug 18 '20 at 04:10
  • @bobsmith76 In the post I stated I was using Python 3.7.6 and the latest version of PyTube3 (9.6.4). – Maelstrom Aug 18 '20 at 07:40
  • 1
    Got it working. I was using Python 3.8, that was the problem. – bobsmith76 Aug 19 '20 at 15:34
  • @bobsmith76 That's really weird. I thought 3.8 adds backwards compatible changes. If you have the time, you could add it as an issue on [their GitHub](https://github.com/nficano/pytube/issues/new/choose) to let them know it's a problem. – Maelstrom Aug 20 '20 at 07:26
  • pytube no longer has an official maintainer so issues are no longer getting resolved in a timely fashion. – bobsmith76 Aug 30 '20 at 02:45
  • @bobsmith76 I think PRs are still being merged but they're not being pushed to PyPi. So the github build should be passing while the pip one is not. [Here](https://github.com/nficano/pytube/pull/687) is a more professional merged PR that fixes the problem. The project is just not updated on pip but you can pull from github. – Maelstrom Aug 30 '20 at 10:06
  • 1
    I haven't yet gotten to that stage with Github where I'm able to pull directly from it. – bobsmith76 Aug 30 '20 at 15:20
  • If you are using PyCharm or VS code, it's pretty simple. Just Google "Github with PyCharm tutorial" and pick whichever video suits you. This allows you to update it with a few clicks. Otherwise, go directly to the repository on your browser, click Code -> download ZIP. Then extract it and put the folder in the project you're working in. – Maelstrom Aug 31 '20 at 15:34
  • @bobsmith76 you could just use this in your terminal-> pip3 install git+url_of_github_repository – Satya Vinay Dec 06 '20 at 19:04
  • @Maelstrom I just installed pytube from github and used, but still it didn't work. – Satya Vinay Dec 06 '20 at 19:07
  • @SatyaVinay did you try [this repo](https://github.com/nficano/pytube)? If that doesn't work then it is a different problem that you have. This issue has been patched long ago. – Maelstrom Dec 07 '20 at 14:52