pythonlistgoogle-apiyoutubeinsight

Google API youtube channel list limit result=5 (https://www.googleapis.com/youtube/v3/search)


I'm developing a python script that executes some check on yt channels. I need to retreive channel_id full list of video, or at least the last 30-40 videos.

I'm using the API https://www.googleapis.com/youtube/v3/search with the following params:

However it always shows me only 5 results as the FULL set of result. There is no pagination. Even testing the API from the google console it returns me the same. Additionally, if I call consecutvely the API I get one time 4 results, one time 5, one time 3... seems to be randomic.

Maybe there is somewhere some limitation. Any suggestions?

Thanks

I expected it to retourn the number of reslts of maxResults


Solution

  • Code correction

    As suggested in comments: probably you should use the playlistItems.list method only costs 1 quota unit per call, compared to 100 units for the search.list method.

    get_uploads_playlist_id:

    import requests
    
    def get_uploads_playlist_id(channel_id, api_key):
        url = f"https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={channel_id}&key={api_key}"
        print(f"Getting uploads playlist ID with URL: {url}")
        
        try:
            response = requests.get(url)
            data = response.json()
            
            if 'error' in data:
                print(f"Error: {data['error']['message']}")
                return None
            
            print(f"Channel API response: {data}")
            
            if 'items' in data and len(data['items']) > 0:
                uploads_playlist_id = data['items'][0]['contentDetails']['relatedPlaylists']['uploads']
                print(f"Found uploads playlist ID: {uploads_playlist_id}")
                return uploads_playlist_id
            else:
                print(f"No channel found with ID: {channel_id}")
                return None
                
        except Exception as e:
            print(f"An error occurred: {e}")
            return None
    
    
    if __name__ == "__main__":
        API_KEY = "#####"
        CHANNEL_ID = "UCupvZG-5ko_eiXAupbDfxWw"
        
        uploads_playlist_id = get_uploads_playlist_id(CHANNEL_ID, API_KEY)
        print(f"Uploads playlist ID: {uploads_playlist_id}")
    

    output#

    Getting uploads playlist ID with URL: 
    https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=UCupvZG-5ko_eiXAupbDfxWw&key=[API_KEY_HIDDEN]
    
    Channel API response: 
    {
        "kind": "youtube#channelListResponse",
        "etag": "IbXPspjj5fQ4E6OF2FGfdSAqO6Q",
        "pageInfo": {
            "totalResults": 1,
            "resultsPerPage": 5
        },
        "items": [
            {
                "kind": "youtube#channel",
                "etag": "G8xQgHvyOJZrWSekOjayPj-5Cbc",
                "id": "UCupvZG-5ko_eiXAupbDfxWw",
                "contentDetails": {
                    "relatedPlaylists": {
                        "likes": "",
                        "uploads": "UUupvZG-5ko_eiXAupbDfxWw"
                    }
                }
            }
        ]
    }
    
    Found uploads playlist ID: UUupvZG-5ko_eiXAupbDfxWw
    Uploads playlist ID: UUupvZG-5ko_eiXAupbDfxWw
    

    use this uploads playlist ID with the get_videos_from_playlist function to fetch all videos from the channel.

    import requests
    
    def get_videos_from_playlist(playlist_id, api_key):
        if not playlist_id:
            print("No playlist ID provided")
            return []
            
        videos = []
        next_page_token = None
        
        print(f"Fetching videos from playlist ID: {playlist_id}")
        
        while True:
            url = f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={playlist_id}&key={api_key}"
            if next_page_token:
                url += f"&pageToken={next_page_token}"
            
            print(f"Making API request with URL: {url}")
            
            try:
                response = requests.get(url)
                data = response.json()
                
                if 'error' in data:
                    print(f"Error: {data['error']['message']}")
                    break
                
                items = data.get('items', [])
                print(f"Retrieved page with {len(items)} videos")
                
                for item in items:
                    video_data = {
                        'video_id': item['snippet']['resourceId']['videoId'],
                        'title': item['snippet']['title'],
                        'published_at': item['snippet']['publishedAt'],
                    }
                    videos.append(video_data)
                
                next_page_token = data.get('nextPageToken')
                if not next_page_token:
                    print("No more pages to fetch")
                    break
                else:
                    print(f"Next page token: {next_page_token}")
                    
            except Exception as e:
                print(f"An error occurred: {e}")
                break
        
        return videos
    
    
    if __name__ == "__main__":
        API_KEY = "###"  
        PLAYLIST_ID = "UUupvZG-5ko_eiXAupbDfxWw"  
        
        videos = get_videos_from_playlist(PLAYLIST_ID, API_KEY)
        
        print(f"\nTotal videos fetched: {len(videos)}")
        
        if videos:
            for i, video in enumerate(videos[:5]):
                print(f"\nVideo {i+1}:")
                print(f"  ID: {video['video_id']}")
                print(f"  Title: {video['title']}")
                print(f"  Published: {video['published_at']}")
        else:
            print("\nNo videos were fetched.")
    
    output#
    
    Fetching videos from playlist ID: UUupvZG-5ko_eiXAupbDfxWw Making API request with URL: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUupvZG-5ko_eiXAupbDfxWw&key=
    
    [API_KEY_HIDDEN] Retrieved page with 50 videos Next page token: EAAaHlBUOkNESWlFRUUyTTBFME16WTROekEwTmpoQ1JrWQ Making API request with URL: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUupvZG-5ko_eiXAupbDfxWw&key=[API_KEY_HIDDEN]&pageToken=EAAaHlBUOkNESWlFRUUyTTBFME16WTROekEwTmpoQ1JrWQ 
    
    Retrieved page with 50 videos Next page token: EAAaHlBUOkNHUWlFRGMzUTBKRE9URTVNa1ZHUWpGR1Fqaw Making API request with URL: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUupvZG-5ko_eiXAupbDfxWw&key=[API_KEY_HIDDEN]&pageToken=EAAaHlBUOkNHUWlFRGMzUTBKRE9URTVNa1ZHUWpGR1Fqaw 
    
    Retrieved page with 50 videos Next page token: EAAaH1BUOkNKWUJJaEF3UmpkRFJVWTJSVUpHTVRFeFFUSkQ
    
    ---------