alex9smith / gdelt-doc-api

A Python client for the GDELT 2.0 Doc API
MIT License
91 stars 20 forks source link

GDELT 2.0 Doc API Client

A Python client to fetch data from the GDELT 2.0 Doc API.

This allows for simpler, small-scale analysis of news coverage without having to deal with the complexities of downloading and managing the raw files from S3, or working with the BigQuery export.

Installation

gdeltdoc is on PyPi and is installed through pip:

pip install gdeltdoc

Use

The ArtList and Timeline* query modes are supported.

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)

Article List

The article list mode of the API generates a list of news articles that match the filters. The client returns this as a pandas DataFrame with columns url, url_mobile, title, seendate, socialimage, domain, language, sourcecountry.

Timeline Search

There are 5 available modes when making a timeline search:

Filters

The search query passed to the API is constructed from a gdeltdoc.Filters object.

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")
)

Filters for keyword, domain, domain_exact, country and theme can be passed either as a single string or as a list of strings. If a list is passed, the values in the list are wrappeed in a boolean OR.

You must pass either start_date and end_date, or timespan

Developing gdelt-doc-api

PRs & issues are very welcome!

Setup

It's recommended to use a virtual environment for development. Set one up with

python -m venv venv

and activate it (on Mac or Linux)

source venv/bin/activate

Then install the requirements

pip install -r requirements.txt

Tests for this package use unittest. Run them with

python -m unittest

If your PR adds a new feature or helper, please also add some tests

Publishing

There's a bit of automation set up to help publish a new version of the package to PyPI,

  1. Make sure the version string has been updated since the last release. This package follows semantic versioning.
  2. Create a new release in the Github UI, using the new version as the release name
  3. Watch as the publish.yml Github action builds the package and pushes it to PyPI