Skip to content
24 changes: 15 additions & 9 deletions src/lfx/src/lfx/components/youtube/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,7 @@ def get_video_comments(self) -> DataFrame:
else:
request = None

# Convert to DataFrame
comments_df = pd.DataFrame(comments_data)

# Add video metadata
comments_df["video_id"] = video_id
comments_df["video_url"] = self.video_url

# Sort columns for better organization
# Define column order
column_order = [
"video_id",
"video_url",
Expand All @@ -217,7 +210,20 @@ def get_video_comments(self) -> DataFrame:
if self.include_metrics:
column_order.extend(["like_count", "reply_count"])

comments_df = comments_df[column_order]
# Handle empty comments case
if not comments_data:
# Create empty DataFrame with proper columns
comments_df = pd.DataFrame(columns=column_order)
else:
# Convert to DataFrame
comments_df = pd.DataFrame(comments_data)

# Add video metadata
comments_df["video_id"] = video_id
comments_df["video_url"] = self.video_url

# Reorder columns
comments_df = comments_df[column_order]

return DataFrame(comments_df)

Expand Down
Loading