-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2495 from jupyter-naas/2494-news-api-get-top-head…
…lines feat: News API - Get top headlines
- Loading branch information
Showing
9 changed files
with
2,070 additions
and
91 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "818af7de-41cd-43f9-bc73-af7729d3b4f5", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"8%\" alt=\"Naas.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Naas.png\" style=\"border-radius: 15%\">" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "65160f8a-b1a8-415a-9cc9-0077ea095b53", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# News API - Get everything" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2a6da0ab-ee71-4cb3-8f47-b2cd863a81d8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #newsapi #python #data #news #api #geteverything" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f90373a2-60ac-4162-9e25-8fe56e01877b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "516700b6-3c5c-4b31-952f-02683d5b77b6", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2024-02-07 (Created: 2024-02-07)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "44660500-8650-452c-ad9e-7190aa72240e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook demonstrates how to use the News API to get everything. It shows how to set up the API, make requests, and display the results." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "95f22bc5-89e3-40a4-83ef-71731ed6e024", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [News API Documentation](https://newsapi.org/docs/client-libraries/python)\n", | ||
"- [Python Requests Library](https://docs.python-requests.org/en/latest/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fc02875d-8425-43b9-9fc8-8bc5b93d4413", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5fba9f56-4ed2-44f7-acd5-7f40356cd9a2", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0c61468b-5a5b-48ea-862d-497cd8eea189", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import naas\n", | ||
"import requests\n", | ||
"from datetime import datetime, date, timedelta" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e52eff03-6982-4aa4-a9ed-c7f3d9a2182b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `api_key`: Your News API key. You can get it from [here](https://newsapi.org/register).\n", | ||
"- `q`: Query to perform" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f89d8c91-5bbf-4907-9b69-1de10648f423", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"api_key = naas.secret.get(\"NEWS_API_API_KEY\")\n", | ||
"q = \"bitcoin\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f4770e76-1059-4be6-8af2-18eda5b47fc7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c31403d9-4a33-4dba-b74b-cb6f2c5d9876", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Get News" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b94a4993-093f-40a8-b556-f9f04d564899", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def fetch_everything(api_key, q, from_param=None, to=None):\n", | ||
" # Params\n", | ||
" to = datetime.now().isoformat()\n", | ||
" today = date.today()\n", | ||
" from_param = today - timedelta(days=7)\n", | ||
" \n", | ||
" # Request\n", | ||
" url = f\"https://newsapi.org/v2/everything?q={q}&from_param={from_param}&to={to}\"\n", | ||
" headers = {\"Authorization\": f\"Bearer {api_key}\"}\n", | ||
" res = requests.get(url, headers=headers)\n", | ||
" return res.json()\n", | ||
"\n", | ||
"res_json = fetch_everything(api_key, q)\n", | ||
"res_json" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1d85e923-bc1a-4d58-a6f7-b2780171bef8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1e7f3d1e-bd16-488e-b065-ceb99d48af9e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "130062e5-1b5f-4650-92a2-825eb6ad5615", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"res_json.get(\"articles\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.