Back to use cases

    Use case / Analysts / Python 3 / Published

    Twitter Trends by City: See What Is Trending Where with Python

    City Trends Pulse is a small Python script that answers "what is trending on Twitter where, right now". For a list of places it pulls the current X trends, fetches the top posts for the leading trends, and writes a snapshot of which trends are shared across places and which are local.

    The problem

    Trend lists are easy to see one place at a time, but comparing several cities or countries is manual work. A trend name alone also says little: you need the top posts to know what it is about and whether those posts are fresh and on topic.

    What the app does

    • Takes a list of places by WOEID (worldwide, countries, cities).
    • Pulls up to 30 current trends per place.
    • Fetches top posts from the last day for the leading trends and keeps the 3 most engaged.
    • Pulls replies for the top post of each place.
    • Shows shared vs local trends, relevance and freshness checks.
    • Writes a Markdown snapshot, CSVs and a heatmap chart.

    How it works

    1. Trends. GET /twitter/trends per place, through the desearch-py SDK.
    2. Top posts. GET /twitter with sort=Top and a start date of yesterday for each trend.
    3. Replies. GET /twitter/replies/post for the top post per place.
    4. Compare. Overlap between places, relevance of each post to its trend, post age and language mix.
    5. Write the snapshot, CSVs, chart and a cost log from the X-Desearch-Cost-Usd header.

    The code

    import asyncio, os
    from desearch_py import Desearch
    
    async def main():
        async with Desearch(api_key=os.environ["DESEARCH_API_KEY"]) as d:
            trends = await d.x_trends(woeid=2459115, count=30, include_metadata=True)   # New York
            print(trends)
    
    asyncio.run(main())
    

    The folder has one script, trends.py, a sample report in sample-output.md and the chart from that run.

    Example output

    From a run on 2026-10-04 across worldwide, the US, the UK, New York, London and Istanbul:

    CheckResult
    Trends in 3 or more places9
    Trends only in Istanbul29 of 29
    Trends only in the UK0 of 30 (same list as London)
    Kept posts that mention their trend98%
    Median age of kept posts4.2 hours

    Run it yourself

    git clone https://github.com/Desearch-ai/desearch-py-examples && cd desearch-py-examples/city-trends-pulse
    pip install -r requirements.txt
    export DESEARCH_API_KEY=...   # from console.desearch.ai
    python trends.py
    

    Useful flags: --top (trends per place to search posts for, default 4), --posts (posts per trend, default 20), --no-replies, --from-raw FILE (re-run the analysis with no API calls). Edit LOCATIONS in trends.py to change places.

    What it costs

    One run with the defaults on 2026-10-04 made 32 calls, took about 100 seconds and cost $0.0975, summed from the X-Desearch-Cost-Usd headers. See pricing.

    Ideas to extend it

    • Run it hourly and keep a history of when a trend reaches each city.
    • Alert when a trend in your niche appears in a city you care about.
    • Add more cities for a regional newsroom view.

    Build your own example.

    Give your agents access to web and social search. Get an API key to start building, or talk to us about your integration.

    Read docs