Skip to content

specialprocedures/napi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

napi - NewsAPI.ai CLI Tool

A command-line interface for downloading data from NewsAPI.ai. It currently only supports the article/getArticles endpoint.

You can spec your query by hard, but the easiest approach is in conjunction with NewsAPI.ai's sandbox: simply save the REST output to a file and run the cli against it to download your data.

NewsAPI.ai sandbox

Installation

From Source

git clone https://github.com/specialprocedures/napi.git
cd napi
pip install -e .

Usage

Basic Usage

napi pull query.json output.json --api_key YOUR_API_KEY

API key management

You can provide your API key to napi in several ways. The easiest is to use the query as is from the sandbox, which should include your key in the apiKey field.

If you'd like to avoid keeping your key in source control, you have several options.

The tool looks for API keys in the following order:

  1. In the source json
  2. --api_key command line argument
  3. NEWSAPI_API_KEY environment variable
  4. NEWSAPI_API_KEY in a .env file

Using Environment Variables

Set your API key as an environment variable:

export NEWSAPI_API_KEY=your_api_key_here
napi pull query.json output.json

Using .env File

Create a .env file in your working directory:

NEWSAPI_API_KEY=your_api_key_here

Then run:

napi pull query.json output.json

Query File Format

The query file should be in JSON format as generated by the NewsAPI sandbox. Example:

{
    "query": {
        "$query": {
            "$and": [
                {
                    "conceptUri": "http://en.wikipedia.org/wiki/Gaza"
                },
                {
                    "dateStart": "2023-08-06",
                    "dateEnd": "2025-10-07",
                    "lang": "eng"
                }
            ]
        }
    },
    "resultType": "articles",
    "articlesSortBy": "date"
}

Development

Setting up for Development

git clone https://github.com/yourusername/napi.git
cd napi
pip install -e .

Running Tests

python -m pytest tests/

Statement on the use of AI

I've used some variation on this very simple code in numerous projects over the past few years. Given how frequently I need to download from NewsAPI, I figured I'd wrap it up in a cli and publish it for others to use.

The initial core functions, i.e., those retrieving the request response, were written by me. I then used Copilot (and Claude Sonnet 4) to:

  • Wrap the functions for cli use with argparse
  • Bootstrap tests, docs and project scaffolding
  • Start getting things ready for publication to PyPI (forthcoming)

License

MIT License - see LICENSE file for details.

About

A command-line interface for downloading data from NewsAPI.ai.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages