JuanBindez / pytubefix

Python3 library for downloading YouTube Videos.
http://pytubefix.rtfd.io/
MIT License
674 stars 95 forks source link

Channel length throwing exception #132

Closed awesome-bhatti closed 3 months ago

awesome-bhatti commented 3 months ago

Describe the bug I noticed getting channel length is throwing exception. it seems to be a KeyError.

Traceback (most recent call last):
  File "/Users/xxxx/Documents/xxxx/test.py", line 49, in <module>
    print(c.length)
          ^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pytubefix/contrib/channel.py", line 467, in length
    return self.initial_data['header']['c4TabbedHeaderRenderer']['videosCountText']['runs'][0]['text']
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'c4TabbedHeaderRenderer'

To Reproduce Steps to reproduce the behavior:

c = Channel('https://www.youtube.com/channel/UCNJ1Ymd5yFuUPtn21xtRbbw')
print(c.length)

Expected behavior it should return the number of videos of the channel channel.length can have this implementation as one of the options. This will return 4.4k if there are 4400 videos and so on. it will return a string.

for metadata_row in self.initial_data["header"]["pageHeaderRenderer"]["content"]["pageHeaderViewModel"]["metadata"]["contentMetadataViewModel"]["metadataRows"]:
    for metadata_part in metadata_row["metadataParts"]:
        if "text" in metadata_part and "content" in metadata_part["text"]:
            if str(metadata_part["text"]["content"]).endswith("videos"):
                return str(metadata_part["text"]["content"]).split(" videos")[0]

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

awesome-bhatti commented 3 months ago

One observation. We can still get the number of videos by iterating over video_urls and keeping track of length. But that only gives the video count and does not include shorts count or live stream count I think.

If we implement above suggested method, we will get complete count.

JuanBindez commented 3 months ago

try: pip install pytubefix==6.5.3a1