pythonerror-handlingyoutube-data-apiinterrupted-exception

Error from Youtube API Data V3 is interrupting my script even after using Try and Except


I'm trying to get comments from all videos from a channel using Youtube's API specifically: Youtube API Data V3. I'm getting an error which occurs when comments are disabled on a video and I'm trying to get my code to ignore it and continue with the other videos.

Error I'm getting:

<HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/commentThreads?part=id%2Csnippet%2Creplies&maxResults=5&order=relevance&videoId=4Tcsg6tU3ow&key=AIzaSyAPFiPIYO4oIRSyPTEUjPl7XGJYte-JkfI&alt=json returned "The video identified by the <code><a href="/youtube/v3/docs/commentThreads/list#videoId">videoId</a></code> parameter has disabled comments.". Details: "[{'message': 'The video identified by the <code><a href="/youtube/v3/docs/commentThreads/list#videoId">videoId</a></code> parameter has disabled comments.', 'domain': 'youtube.commentThread', 'reason': 'commentsDisabled', 'location': 'videoId', 'locationType': 'parameter'}]">

Which is expected however I am using the block of code in except to get the program to ignore this videoId however the program gets interrupted when running into this error. Furthermore I have run into cases where it will go to this videoId, print the error and ignore it, and then revisit the videoID and interrupt the script even though the videoId is now in processed_video_ids and shouldn't be run again.

Here is the snippet of code I believe is important in this case:

for video_id in videoMetadata:
        videoId = video_id['videoId']
        publish_date = dt.strptime(video_id['publish_date'], "%Y-%m-%dT%H:%M:%SZ")
        if videoId not in processed_video_ids:
            try:
                print(f"Processing comments for video ID: {videoId}")
                comment_threads(videoId, to_csv=True)

               # Block of Code for file moving Removed
                
            except requests.exceptions.HTTPError as e:
            # Check if the exception is due to a 403 error (Forbidden or Disabled Comments)
                if e.response.status_code == 403:
                    error_message = e.response.json()["error"]["errors"][0]['reason']
                    if error_message == 'commentsDisabled':
                        print("Comments are disabled for this video. Skipping this request.")
                        
                    else:
                        print(f"403 Forbidden: {error_message}. Skipping this request.")
                        
                    
                else:
                    # Handle other HTTP errors
                    print(f"HTTP error occurred: {e}")
                    

            except requests.exceptions.RequestException as e:
                # Handle other exceptions (e.g., network errors)
                print(f"Error making API request: {e}")
                

        processed_video_ids.add(videoId)

Additionally my script never seems to print the strings when running into the error which can lead me to believe me my script is wrong in accessing the error message provided by the API.


Solution

  • Found the solution. Google API has its own way of requesting errors so instead of:

    except requests.exceptions.HTTPError as e:
    

    it's:

    except googleapiclient.errors.HttpError as e:
    try:
        error_content = e.content.decode('utf-8')
        error_data = json.loads(error_content)
    

    and you can see check the error code with:

    error.resp.status == 'ERROR CODE HERE'