Building an AI-Powered YouTube Digest with Python, Gemini, and the YouTube API

Stop drowning in content. Build a smart assistant that finds, summarizes, and emails you exactly what you need to watch.

We’ve all been there. You open YouTube to watch one specific video on a topic you want to learn about, and two hours later you emerge from a rabbit hole of unrelated content, your original goal long forgotten. The sheer volume of information available today is both a blessing and a curse.

What if you could tame this firehose of content? What if you could build a personal content curator that automatically scours YouTube for videos on your topics of interest, uses a powerful AI to write clean, concise summaries, and delivers a polished digest directly to your inbox every day?

That’s exactly what this project is about. In this article, I’ll walk you through how to build a fully automated, AI-powered YouTube newsletter using Python. We’ll leverage the precision of the YouTube Data API to find the videos and the creative intelligence of Google’s Gemini API to make our results smart, readable, and engaging.

This isn’t just a data-scraping script; it’s a blueprint for creating your own intelligent information system.


The Architecture: A 30,000-Foot View

Before diving into the code, let’s understand the workflow. Our script operates in a clear, five-step pipeline, where the output of one step becomes the input for the next. This modular design makes it easy to understand and modify.

Here is the high-level architecture of our system:

Code
    [Your Keywords] -> [YouTube API] -> [Gemini API for Summaries] -> [Gemini API for Email] -> [SMTP Delivery]
  
  1. Keyword-Driven Search: The process begins with a simple, hardcoded list of keywords in our Python script (e.g., 'latest AI advancements', 'python project ideas'). This is the seed of our curation.

  2. Fetch & Filter (YouTube Data API): For each keyword, the script calls the YouTube Data API. It acts like a programmatic search, fetching a list of the most relevant videos, along with their raw data like titles, links, and often messy, lengthy descriptions.

  3. Analyze & Summarize (Gemini API - Goodie #1): This is where the magic begins. Instead of using the raw YouTube description, we send each video's title and description to the Gemini API. We prompt it to act as an expert content curator, asking it to write a clean, concise, one-paragraph summary. This transforms raw data into valuable, readable information.

  4. Assemble & Beautify (Gemini API - Goodies #2 & #3): With our list of summarized videos, we call the Gemini API again. This time, we ask it to perform two creative tasks:

    • Generate an engaging introduction for our email newsletter.

    • Craft a catchy, relevant subject line.
      This ensures the final product feels personal and professionally written, not like a robotic data dump.

  5. Deliver (SMTP): Finally, the script assembles all these pieces—the subject, the intro, and the list of summarized videos—into a beautifully formatted HTML email. It then uses Python's built-in smtplib to log in to an email server (like Gmail) and send the digest to a predefined list of recipients.


Diving Deeper: The Code Explained

Now, let's break down how the Python code accomplishes each of these steps. We'll use pseudo-code and simplified snippets to illustrate the core logic.

Part 1: Setup and Configuration

First, we define all our parameters at the top of the script. This includes our API keys, email credentials, and the keywords we want to search for. Storing these in one place makes the script easy to configure.

Pseudo-code:

Python
    # --- Configuration ---
GEMINI_API_KEY = "YOUR_GEMINI_API_KEY"
YOUTUBE_API_KEY = "YOUR_YOUTUBE_API_KEY"
SENDER_EMAIL = "your-email@gmail.com"
SENDER_PASSWORD = "your-gmail-app-password"
RECIPIENT_EMAILS = ["recipient1@example.com", "recipient2@example.com"]
KEYWORDS_TO_SEARCH = ["generative AI news", "woodworking projects"]
NUMBER_OF_VIDEOS = 5

# --- API Initialization ---
CONFIGURE Gemini client with GEMINI_API_KEY
BUILD YouTube client with YOUTUBE_API_KEY
  

Pro Tip: For a real-world application, avoid hardcoding keys. Use environment variables or a configuration file for better security.

Part 2: Fetching Videos with the YouTube API

This function is our data collector. It takes a keyword and programmatically searches YouTube, returning the raw data for the top videos.

Pseudo-code:

Python
    FUNCTION find_youtube_videos(keyword):
  // Connect to the YouTube Data API service
  youtube_service = build_youtube_client()

  // Execute a search query
  search_results = youtube_service.search(
    query=keyword,
    part="snippet", // We want the title, description, etc.
    type="video",
    max_results=NUMBER_OF_VIDEOS
  ).execute()

  RETURN search_results["items"]
  

Part 3: The AI Magic - Summarizing Content

This is the core value-add of our project. This function takes the raw title and description from YouTube and uses Gemini to generate a high-quality summary. The key is prompt engineering—telling the AI exactly what we want.

Pseudo-code:

Python
    FUNCTION summarize_with_gemini(title, raw_description):
  // We craft a very specific prompt to guide the AI
  prompt = """
  You are an expert content curator for an email newsletter.
  Based on the video's title and its raw description, write a clean,
  engaging, one-paragraph summary (2-3 sentences).
  Ignore all promotional text, social media links, or calls to subscribe.

  TITLE: "{title}"
  RAW DESCRIPTION: "{raw_description}"

  CONCISE SUMMARY:
  """

  // Send the prompt to the Gemini API
  gemini_response = gemini_model.generate_content(prompt)

  RETURN gemini_response.text
  

By giving the AI a role ("expert content curator") and clear negative constraints ("Ignore all promotional text"), we get a consistently clean and useful output.

Part 4: Assembling and Sending the Email

Finally, we need a function to build and send our email. This involves creating an HTML structure for a nicely formatted message and using Python's smtplib library to connect to an SMTP server (like Gmail's) and send it.

Pseudo-code:

Python
    FUNCTION send_email(subject, html_body, recipients):
  // Create an email message object
  message = create_mime_multipart_message()
  message["Subject"] = subject
  message["From"] = SENDER_EMAIL
  message["To"] = recipients

  // Attach the HTML content
  message.attach(html_body)

  // Connect to the SMTP server over SSL
  server = smtplib.SMTP_SSL("smtp.gmail.com", 465)

  // Log in with your credentials
  server.login(SENDER_EMAIL, SENDER_PASSWORD) // Use an App Password!

  // Send the mail and close the connection
  server.send_message(message)
  server.quit()
  

The Main Workflow

The main execution block of the script ties all these functions together in the correct order:

Python
    // 1. Loop through all keywords
FOR keyword IN KEYWORDS_TO_SEARCH:
  // 2. Fetch raw video data
  raw_videos = find_youtube_videos(keyword)

  // 3. For each video, get a Gemini summary
  FOR video IN raw_videos:
    clean_summary = summarize_with_gemini(video.title, video.description)
    STORE {title, link, clean_summary} in a curated list

// 4. Use Gemini to write the email intro and subject
email_intro = generate_email_intro_with_gemini(KEYWORDS_TO_SEARCH)
email_subject = generate_subject_line_with_gemini(KEYWORDS_TO_SEARCH)

// 5. Build the final HTML and send the email
html = build_html_body(email_intro, curated_list)
send_email(email_subject, html, RECIPIENT_EMAILS)```

***

## Conclusion and Your Next Steps

And there you have it. With a relatively short Python script, we’ve created a powerful, automated system that transforms the chaos of online content into a personalized, intelligent digest. We've successfully delegated the repetitive tasks of searching and filtering to the YouTube API and the cognitive task of summarizing and writing to the Gemini API.

This project saves time, ensures you stay updated on your key interests, and, most importantly, provides a fantastic starting point for more advanced applications.

**Where can you take this next?**

1.  **Full Automation**: Deploy this script to a cloud service like **Google Cloud Functions** or **AWS Lambda** and set it up on a cron trigger to run automatically every morning.
2.  **Web Interface**: Build a simple web front-end using **Flask** or **Django** that allows you to add or remove keywords and manage recipient email addresses without editing the code.
3.  **Persistence**: Store the IDs of the videos you’ve already sent in a database (like SQLite or Firestore) to ensure you never send the same video twice.
4.  **Deeper Personalization**: Experiment with more advanced Gemini prompts. For instance: *"Summarize this for a busy executive,"* or *"Explain this topic like I'm a beginner."*

The era of intelligent automation is here. By combining the power of APIs and Large Language Models, you have the tools to build systems that work for you, cutting through the noise and delivering pure, relevant value. Happy coding
  

Comments

Popular posts from this blog

AI Assistants: Your Secret Weapon for a More Productive Life?