Youtube collector with API key

To collect all video links from a YouTube channel, you can use the YouTube Data API, which allows you to interact with YouTube data. Below is an example of a Python program that retrieves all video links from a given YouTube channel.
Before you start, make sure you have the following:
A Google account to create a project in the Google Developers Console.
Enable the YouTube Data API for your project.
Obtain an API key.
Once you have your API key, you can use the following Python code:
import requests
def get_channel_videos(channel_id, api_key):
base_url = "https://www.googleapis.com/youtube/v3"
# Get the uploads playlist ID
playlist_url = f"{base_url}/channels?part=contentDetails&id={channel_id}&key={api_key}"
response = requests.get(playlist_url)
response_data = response.json()
if 'items' not in response_data or len(response_data['items']) == 0:
print("Channel not found or no content details available.")
return []
uploads_playlist_id = response_data['items'][0]['contentDetails']['relatedPlaylists']['uploads']
# Get videos from the uploads playlist
videos = []
next_page_token = None
while True:
playlist_url = f"{base_url}/playlistItems?part=snippet&playlistId={uploads_playlist_id}&maxResults=50&key={api_key}"
if next_page_token:
playlist_url += f"&pageToken={next_page_token}"
response = requests.get(playlist_url)
response_data = response.json()
for item in response_data.get('items', []):
video_id = item['snippet']['resourceId']['videoId']
video_url = f"https://www.youtube.com/watch?v={video_id}"
videos.append(video_url)
next_page_token = response_data.get('nextPageToken')
if not next_page_token:
break
return videos
if __name__ == "__main__":
# Replace 'YOUR_API_KEY' with your actual API key
API_KEY = 'YOUR_API_KEY'
# Replace 'CHANNEL_ID' with the actual channel ID
CHANNEL_ID = 'CHANNEL_ID'
video_links = get_channel_videos(CHANNEL_ID, API_KEY)
for link in video_links:
print(link)
Instructions:
Replace
'YOUR_API_KEY'with your actual YouTube Data API key.Replace
'CHANNEL_ID'with the actual channel ID from which you want to collect video links.
Running the Program:
- Install the required library if you haven't already:
pip install requests
- Run the script, and it will print all video links from the specified YouTube channel.
Note:
- Be aware of the API quotas and limits set by YouTube.



