Tutorial

How to Build a Trend Detection Agent Using YouTube and Google

Build a Python agent that monitors YouTube and Google search trends simultaneously using the Scavio API. Detect emerging topics before they go mainstream.

Trend detection involves identifying topics that are gaining rapid search and engagement momentum before they reach mainstream awareness. Combining Google search result velocity with YouTube video upload rates and view counts gives a multi-signal view of emerging trends. This tutorial builds a trend detection agent that queries both platforms via the Scavio API, scores topics by signal strength, and outputs a ranked list of trending subjects for a given niche.

Prerequisites

  • Python 3.10 or higher
  • requests library installed
  • A Scavio API key
  • Basic understanding of data normalization and scoring

Walkthrough

Step 1: Define topic seeds

Start with a set of broad seed terms in your niche. The agent will expand these into related trend queries.

Python
SEEDS = ["generative ai", "open source llm", "ai agents 2026"]
NICHE = "artificial intelligence"

Step 2: Collect Google search result counts

For each seed, fetch Google SERP and note the number of organic results and their recency signals.

Python
def google_signal(topic: str) -> int:
    data = search_google(f"{topic} 2026")
    results = data.get("organic_results", [])
    # Count results mentioning the current year as recency signal
    recent = sum(1 for r in results if "2026" in r.get("title", "") + r.get("snippet", ""))
    return recent

Step 3: Collect YouTube video metrics

Search YouTube for each seed topic using the Scavio search endpoint and count recent video uploads.

Python
def youtube_signal(topic: str) -> int:
    response = requests.post(
        "https://api.scavio.dev/api/v1/search",
        headers={"x-api-key": API_KEY},
        json={"platform": "youtube", "query": topic}
    )
    videos = response.json().get("videos", [])
    return len([v for v in videos if "2026" in v.get("published_at", "")])

Step 4: Score and rank trends

Combine Google and YouTube signals into a composite score and rank topics.

Python
def rank_trends(seeds: list[str]) -> list[dict]:
    scored = []
    for seed in seeds:
        g = google_signal(seed)
        y = youtube_signal(seed)
        scored.append({"topic": seed, "google": g, "youtube": y, "score": g + y * 2})
    return sorted(scored, key=lambda x: x["score"], reverse=True)

Python Example

Python
import os
import requests

API_KEY = os.environ.get("SCAVIO_API_KEY", "your_scavio_api_key")
ENDPOINT = "https://api.scavio.dev/api/v1/search"
SEEDS = ["generative ai", "open source llm", "ai agents", "model distillation"]

def search_google(q: str) -> dict:
    r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
                      json={"query": q, "country_code": "us"})
    r.raise_for_status()
    return r.json()

def search_youtube(q: str) -> list:
    r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
                      json={"platform": "youtube", "query": q})
    r.raise_for_status()
    return r.json().get("videos", [])

def score(seed: str) -> dict:
    g_results = search_google(f"{seed} 2026").get("organic_results", [])
    y_results = search_youtube(seed)
    g_score = sum(1 for r in g_results if "2026" in r.get("title", ""))
    y_score = len(y_results)
    return {"topic": seed, "score": g_score + y_score * 2}

if __name__ == "__main__":
    scores = sorted([score(s) for s in SEEDS], key=lambda x: x["score"], reverse=True)
    for s in scores:
        print(f"{s['topic']}: {s['score']}")

JavaScript Example

JavaScript
const API_KEY = process.env.SCAVIO_API_KEY || "your_scavio_api_key";
const ENDPOINT = "https://api.scavio.dev/api/v1/search";
const SEEDS = ["generative ai", "open source llm", "ai agents", "model distillation"];

async function searchGoogle(q) {
  const res = await fetch(ENDPOINT, {
    method: "POST",
    headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
    body: JSON.stringify({ query: q, country_code: "us" })
  });
  return res.json();
}

async function scoreTopic(seed) {
  const data = await searchGoogle(`${seed} 2026`);
  const organic = data.organic_results || [];
  const gScore = organic.filter(r => (r.title + " " + (r.snippet || "")).includes("2026")).length;
  return { topic: seed, score: gScore };
}

async function main() {
  const scores = await Promise.all(SEEDS.map(scoreTopic));
  scores.sort((a, b) => b.score - a.score).forEach(s => console.log(`${s.topic}: ${s.score}`));
}
main().catch(console.error);

Expected Output

JSON
ai agents: 8
generative ai: 7
open source llm: 5
model distillation: 3

Top trend: ai agents (score: 8)

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Python 3.10 or higher. requests library installed. A Scavio API key. Basic understanding of data normalization and scoring. A Scavio API key gives you 500 free credits per month.

Yes. The free tier includes 500 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

Build a Python agent that monitors YouTube and Google search trends simultaneously using the Scavio API. Detect emerging topics before they go mainstream.