Skip to main content

Media Cloud API Client Library

Project description

MediaCloud Python API Client

This is a python client for accessing the MediaCloud API v4. This allows you to perform cross-platform searches and also browse our collection/source/feed directory.

pylint pytest GitHub license

Usage

First sign up for an API key. Then

pip install mediacloud

Check CHANGELOG.md for a detailed history of changes.

Examples

Take a look at the test in the mediacloud/test/ module for more detailed examples.

Count Stories Matching a Query

import mediacloud.api
US_NATIONAL_COLLECTION = 34412234
mc_search = mediacloud.api.SearchApi(YOUR_MC_API_KEY)
all_stories = []
pagination_token = None
more_stories = True
while more_stories:
    page, pagination_token = mc_search.story_list('robots', start_date= , end_date= collection_ids=[US_NATIONAL_COLLECTION])
    all_stories += page
    more_stories = pagination_token is not None
print(f"Retrived {len(all_stories)} matching stories")

Page Through Stories Matching a Query

import mediacloud.api
INDIA_NATIONAL_COLLECTION = 34412118
mc_search = mediacloud.api.SearchApi(YOUR_MC_API_KEY)
all_stories = []
pagination_token = None
more_stories = True
while more_stories:
    page, pagination_token = mc_search.story_list('modi AND biden', collection_ids=[INDIA_NATIONAL_COLLECTION],
                                                  pagination_token=pagination_token)
    all_stories += page
    more_stories = pagination_token is not None
print(f"Retrived {len(all_stories)} matching stories")

Fetch all Sources in a Collection

import mediacloud.api
INDIA_NATIONAL_COLLECTION = 34412118
SOURCES_PER_PAGE = 100  # the number of sources retrieved per page
mc_directory = mediacloud.api.DirectoryApi(YOUR_MC_API_KEY)
sources = []
offset = 0   # offset for paging through
while True:
    # grab a page of sources in the collection
    response = mc_directory.source_list(collection_id=INDIA_NATIONAL_COLLECTION, limit=SOURCES_PER_PAGE, offset=offset)
    # add it to our running list of all the sources in the collection
    sources += response['results']
    # if there is no next page then we're done so bail out
    if response['next'] is None:
        break
    # otherwise setup to fetch the next page of sources
    offset += len(response['results'])
print("India National Collection has {} sources".format(len(sources)))

Development

If you are interested in adding code to this module, first clone the GitHub repository.

Installing

  • flit install
  • pre-commit install

Testing

pytest

Distributing a New Version

  1. Run pytest to make sure all the test pass
  2. Update the version number in pyproject.toml
  3. Make a brief note in the CHANGELOG.md about what changes
  4. Commit changes, and tag commit with version number
  5. Push to main

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mediacloud-4.6.0.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mediacloud-4.6.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file mediacloud-4.6.0.tar.gz.

File metadata

  • Download URL: mediacloud-4.6.0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mediacloud-4.6.0.tar.gz
Algorithm Hash digest
SHA256 041e4a7eefdb0c9baab13b0ad38cd87e4f151d627d21a753d483479e601017da
MD5 b4fccb6ccd9d25d8c4c48aab5e4e2b12
BLAKE2b-256 a3d759440ce53911955bb152f0a21947841e3cfc086eeca95e975db496aa9354

See more details on using hashes here.

Provenance

The following attestation bundles were made for mediacloud-4.6.0.tar.gz:

Publisher: publish-to-pypi.yml on mediacloud/api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mediacloud-4.6.0-py3-none-any.whl.

File metadata

  • Download URL: mediacloud-4.6.0-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mediacloud-4.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25888935f9cf5ff7a94046373f97005d4869fe2056c9768a8a8d5ddf70ef31c3
MD5 29798d18634d305763d654a07864a827
BLAKE2b-256 e7c9018a3dc859193b8155345502ecb12ea26b549b0c4e600897d3f421ea5f7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mediacloud-4.6.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on mediacloud/api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page