-
-
Notifications
You must be signed in to change notification settings - Fork 83
Fix KeyError in YouTube search by correcting JSON response parsing #401
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
base: master
Are you sure you want to change the base?
Conversation
Resolved a KeyError: 1 issue in the search_yt function that occurred when attempting to access page[1] in the JSON response from YouTube's search API. The original code assumed the response was a list or dict with a top-level key '1', but the actual structure starts directly with a 'response' key. Updated the code to remove the unnecessary [1] index and correctly navigate the nested structure starting from page["response"]["contents"]. This ensures the video list is parsed accurately from the API response. Tested with the query "Channa mere ya" and confirmed the fix works as expected.
Reviewer's Guide by SourceryThis pull request fixes a KeyError in the Sequence diagram for YouTube search API interactionsequenceDiagram
participant User
participant Bot
participant YouTube API
User->>Bot: Sends a YouTube search query
Bot->>YouTube API: Makes a request to the YouTube API
activate YouTube API
YouTube API-->>Bot: Returns JSON response
deactivate YouTube API
Bot->>Bot: Parses JSON response (corrected)
Bot-->>User: Returns list of videos
Updated class diagram for YouTube API response parsingclassDiagram
class YouTubeResponse {
+response: Response
}
class Response {
+contents: Contents
}
class Contents {
+twoColumnSearchResultsRenderer: TwoColumnSearchResultsRenderer
}
class TwoColumnSearchResultsRenderer {
+primaryContents: PrimaryContents
}
class PrimaryContents {
+sectionListRenderer: SectionListRenderer
}
class SectionListRenderer {
+contents: list[Content]
}
class Content {
+itemSectionRenderer: ItemSectionRenderer
}
class ItemSectionRenderer {
+contents: list[Video]
}
class Video {
+videoRenderer: VideoRenderer
}
class VideoRenderer {
+videoId: str
+title: str
+thumbnail: str
}
YouTubeResponse -- Response : contains
Response -- Contents : contains
Contents -- TwoColumnSearchResultsRenderer : contains
TwoColumnSearchResultsRenderer -- PrimaryContents : contains
PrimaryContents -- SectionListRenderer : contains
SectionListRenderer -- Content : contains
Content -- ItemSectionRenderer : contains
ItemSectionRenderer -- Video : contains
Video -- VideoRenderer : contains
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @MukundSinghRajput - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a more descriptive variable name than
page
to improve readability.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Resolved a KeyError: 1 issue in the search_yt function that occurred when attempting to access page[1] in the JSON response from YouTube's search API. The original code assumed the response was a list or dict with a top-level key '1', but the actual structure starts directly with a 'response' key. Updated the code to remove the unnecessary [1] index and correctly navigate the nested structure starting from page["response"]["contents"]. This ensures the video list is parsed accurately from the API response. Tested with the query "Channa mere ya" and confirmed the fix works as expected.
Summary by Sourcery
Bug Fixes: