Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Example: List Videos for Channel #54

Merged
merged 5 commits into from
Jul 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions examples/list-channel-videos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import random
import time

from innertube import InnerTube


def delay():
# A random duration between 2s and 5s
secs = random.randint(2000, 5000) / 1000

time.sleep(secs)


def list_videos(channel_id=None, /, *, continuation=None):
# Client for YouTube (Web)
client = InnerTube("WEB", "2.20230728.00.00")

# If this is the first video listing, browse the "Videos" page
if continuation is None:
# Fetch the browse data for the channel
channel_data = client.browse(channel_id)

# Extract the tab renderer for the "Videos" tab of the channel
videos_tab_renderer = channel_data["contents"][
"twoColumnBrowseResultsRenderer"
]["tabs"][1]["tabRenderer"]

# Make sure this tab is the "Videos" tab
assert videos_tab_renderer["title"] == "Videos"

# Extract the browse params for the "Videos" tab of the channel
videos_params = videos_tab_renderer["endpoint"]["browseEndpoint"]["params"]

# Wait a bit so that Google doesn't suspect us of being a bot
delay()

# Fetch the browse data for the channel's videos
videos_data = client.browse(channel_id, params=videos_params)

# Extract the contents list
contents = videos_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][1][
"tabRenderer"
]["content"]["richGridRenderer"]["contents"]
else:
# Fetch more videos by using the continuation token
continued_videos_data = client.browse(continuation=continuation)

contents = continued_videos_data["onResponseReceivedActions"][0][
"appendContinuationItemsAction"
]["continuationItems"]

# Extract the rich video items and the continuation item
*rich_items, continuation_item = contents

# Loop through each video and log out its details
for rich_item in rich_items:
video_renderer = rich_item["richItemRenderer"]["content"]["videoRenderer"]

video_id = video_renderer["videoId"]
video_title = video_renderer["title"]["runs"][0]["text"]

print(f"[{video_id}] {video_title}")

# Extract the continuation token
continuation_token = continuation_item["continuationItemRenderer"][
"continuationEndpoint"
]["continuationCommand"]["token"]

return continuation_token


channel_browse_id = "UCXuqSBlHAE6Xw-yeJA0Tunw" # Linus Tech Tips

# List the initial videos
continuation = list_videos(channel_browse_id)

# Wait a bit so that Google doesn't suspect us of being a bot
delay()

# Print an empty line to demarcate the initial items from the continuation
print()

# List the videos from the first continuation
list_videos(continuation=continuation)
Loading