gdeltdoc Python Client

repository·main·Indexed 42 days ago

https://github.com/alex9smith/gdelt-doc-api

A Python client for the GDELT 2.0 Doc API used to fetch news coverage data for small-scale analysis. It provides the GdeltDoc class for article and timeline searches, a Filters object for defining search criteria, and helper functions like near() and repeat() for advanced text filtering. Supports retrieving data as pandas DataFrames with modes including timelinevol, timelinevolraw, timelinelang, timelinesourcecountry, and timelinetone.

Tokens
1.6K
Snippets
5
Records
7
Agent score
3%

What's inside gdeltdoc

  1. Configure search queries using Filters

    main

    All search queries are constructed using a gdeltdoc.Filters object. You must provide either a date range (start_date and end_date) or a timespan.

    Note on Dates: start_date and end_date can be strings in YYYY-MM-DD format or UTC datetime objects. Using datetime allows for second-level granularity. The API officially supports the most recent 3 months of articles; older requests are not guaranteed to return data.

    from gdeltdoc import Filters, near, repeat
    
    f = Filters(
        start_date = "2020-05-01",
        end_date = "2020-05-02",
        num_records = 250,
        keyword = "climate change",
        domain = ["bbc.co.uk", "nytimes.com"],
        country = ["UK", "US"],
        theme = "GENERAL_HEALTH",
        near = near(10, "airline", "carbon"),
        repeat = repeat(5, "planet")
    )
  2. Basic usage of GdeltDoc for article and timeline searches

    main

    To use the client, instantiate GdeltDoc and create a Filters object to define your search criteria. You can then use article_search(filters) to get a list of articles or timeline_search(mode, filters) to get temporal data.

    from gdeltdoc import GdeltDoc, Filters
    
    f = Filters(
        keyword = "climate change",
        start_date = "2020-05-10",
        end_date = "2020-05-11"
    )
    
    gd = GdeltDoc()
    
    # Search for articles matching the filters
    articles = gd.article_search(f)
    
    # Get a timeline of the number of articles matching the filters
    timeline = gd.timeline_search("timelinevol", f)
  3. Perform a timeline search with different modes

    main

    The timeline_search(mode, filters) method allows you to retrieve temporal data using one of five available modes:

    • timelinevol: A timeline of the volume of news coverage as a percentage of total GDELT monitored articles.
    • timelinevolraw: Similar to timelinevol, but returns the actual number of articles and a total instead of a percentage.
    • timelinelang: Breaks down the total articles by published language. Each language is a separate column in the resulting DataFrame.
    • timelinesourcecountry: Breaks down the total articles by the country of publication. Each country is a separate column in the resulting DataFrame.
    • timelinetone: A timeline of the average tone of the news coverage (refer to GDELT documentation for tone metric details).
  4. Reference: Advanced text filters (near and repeat)

    main

    Use these helper functions to construct complex text requirements within a Filters object.

    • near(distance, word1, word2, ...): Finds articles where words are within a certain distance of each other.
    • multi_near(list_of_tuples, method="AND"): Allows multiple proximity restrictions.
    • repeat(count, word): Finds articles where a word is repeated at least count times.
    • multi_repeat(list_of_tuples, method="AND"): Allows multiple repetition restrictions.
    # Examples of near and repeat usage
    near_filter = near(5, "airline", "climate")
    
    multi_near_filter = multi_near([
        (5, "airline", "crisis"), 
        (10, "airline", "climate", "change")
    ], method="AND")
    
    repeat_filter = repeat(3, "environment")
    
    multi_repeat_filter = multi_repeat([("airline"), ("airport")], "AND")
  5. Reference: Filters configuration options

    main

    The following options can be passed to the Filters constructor. For keyword, domain, domain_exact, country, language, and theme, you can pass a single string or a list of strings (lists are treated as a boolean OR).

    start_date         - Start date (YYYY-MM-DD or datetime object).
    end_date           - End date (YYYY-MM-DD or datetime object).
    timespan           - Relative timespan (must match API formats).
    num_records        - Number of records to return (up to 250, used in article list mode).
    keyword            - Exact phrase to find in article text.
    domain             - Domain match (not exact, e.g., 'cnn.com' matches subdomains).
    domain_exact       - Requires an exact domain match.
    country            - FIPS 2-letter country code (or list).
    language           - ISO 639 language code (or list).
    theme              - GDELT GKG Theme.
    near               - Use near() to find words close to each other.
    repeat             - Use repeat() to find words repeated N times.
    tone               - Search by tone threshold (e.g., ">5" or "<-5").
    tone_absolute      - Search by high/low emotion regardless of positive/negative sign.