Analyze news with game theory via curl and OpenAI
Fetches recent news articles and pipes them into an LLM to extract actors, events, and perform a game-theory analysis.
Setup
- → export NEWS_API_KEY='your_newsapi_key'
- → export OPENAI_API_KEY='your_openai_key'
- → apt-get install jq
Cost per run
~$0.01 per run
The one-liner
$ curl -s "https://newsapi.org/v2/everything?q=OPEC&pageSize=3&apiKey=$NEWS_API_KEY" \
| jq -r '.articles[] | "- \(.title): \(.description)"' \
| jq -Rs '{model: "gpt-4o", messages: [{role: "user", content: ("Identify actors, events, and provide a game-theory analysis for these news items:\n" + .)}]}' \
| curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d @- \
| jq -r '.choices[0].message.content'What each stage does
- [01] curl
curl -s "https://newsapi.org/v2/everything?q=OPEC&pageSize=3&apiKey=$NEWS_API_KE…Fetches the top 3 latest news articles about the specified topic (e.g., OPEC) using the NewsAPI. - [02] jq
jq -r '.articles[] | "- \(.title): \(.description)"'Extracts the titles and descriptions of the articles, formatting them as a raw text bulleted list. - [03] jq
jq -Rs '{model: "gpt-4o", messages: [{role: "user", content: ("Identify actors, …Reads the raw text list and wraps it into a JSON payload formatted for the OpenAI API, prepending the analysis prompt. - [04] curl
curl -s https://api.openai.com/v1/chat/completions -H "Content-Type: application…Sends the constructed JSON payload to OpenAI via a POST request, reading the body from standard input. - [05] jq
jq -r '.choices[0].message.content'Parses the OpenAI JSON response to extract and print just the raw markdown analysis text.
Expected output (sample)
Actors: OPEC+ members (Saudi Arabia, Russia), US Shale Producers. Events: OPEC+ extends production cuts into Q4. Game-Theory Analysis: - Saudi Arabia (Cooperate): Maintains cuts to keep prices high, risking market share loss. - Russia (Defect): Incentivized to secretly overproduce to fund war efforts. - Equilibrium: Fragile cartel stability; likely breakdown if US production surges.
Caveats & tips
- Footgun: The `jq -Rs` command slurp-reads the entire input into a single string; if the news payload is too large, it may exceed the LLM context window.
- Cost/Permission: Requires active API keys for both NewsAPI and OpenAI, incurring minor usage costs per execution.