Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Retrieving YouTube Playlist Videos Based on Date Range with Youtube API v3 - No Data Returned

$
0
0

I'm trying to retrieve a list of videos from a YouTube playlist based on a date range using the Google YouTube Data API v3 in Python. However, the video_list variable is not getting any data even though the if statement that checks if a video falls within the date range is being executed. Here's my

code:

import requestsfrom dateutil.parser import isoparsedef get_playlist(start_date, end_date, max_results=50):    URL = f'{INSTANCE}/playlistItems'    params = {'part': 'snippet','key': 'api_key','playlistId': PLAYLIST_ID,'maxResults': max_results,'fields': 'items(snippet(publishedAt,resourceId/videoId)),nextPageToken',    }    page_token = ''    video_list = []    start_date = isoparse(start_date)    end_date = isoparse(end_date)    while True:        params['pageToken'] = page_token        response = requests.get(URL, params=params)        if response.status_code != 200:            print('Error:', response.text)            return []        data = response.json()        for item in data['items']:            snippet = item['snippet']            video_date = isoparse(snippet['publishedAt'])            if start_date <= video_date <= end_date:                video_id = snippet.get('resourceId', {}).get('videoId', None)                published_at = snippet.get('publishedAt', None)                video_data = {'id': video_id,'publishedAt': published_at,                }                video_list.append(video_data)        print('video_list', video_list)  # This is not get any data        if 'nextPageToken' not in data:            break        page_token = data['nextPageToken']    return video_listdate_strt_str = '2024-02-27T00:00:00Z'date_end_str = '2024-02-24T00:00:00Z'results = get_playlist(    start_date=date_strt_str,    end_date=date_end_str,)

The video_list variable is empty even though the if statement is being executed. I've added a print statement to check the value of video_list inside the while loop, but it's not getting any data. How can I retrieve a list of videos from the YouTube playlist based on the specified date range?


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>