Skip to content

Commit

Permalink
Merge pull request #2495 from jupyter-naas/2494-news-api-get-top-head…
Browse files Browse the repository at this point in the history
…lines

feat: News API - Get top headlines
  • Loading branch information
FlorentLvr authored Feb 7, 2024
2 parents 7a4b493 + 7c6a51c commit b2a169c
Show file tree
Hide file tree
Showing 9 changed files with 2,070 additions and 91 deletions.
Binary file added .github/assets/logos/News API.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed .github/assets/logos/Newsapi.png
Binary file not shown.
49 changes: 35 additions & 14 deletions Newsapi/Newsapi_Get_data.ipynb → News API/News_API_Get_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# Newsapi - Get data\n",
"# News API - Get data\n",
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Newsapi/Newsapi_Get_data.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=Newsapi+-+Get+data:+Error+short+description\">Bug report</a>"
]
},
Expand Down Expand Up @@ -55,7 +55,7 @@
"tags": []
},
"source": [
"**Last update:** 2023-04-12 (Created: 2021-02-28)"
"**Last update:** 2024-02-07 (Created: 2021-02-28)"
]
},
{
Expand All @@ -68,7 +68,7 @@
]
},
"source": [
"**Description:** This notebook provides a guide to using the Newsapi service to access and retrieve data."
"**Description:** This notebook provides a guide to access and retrieve data from News API."
]
},
{
Expand Down Expand Up @@ -108,38 +108,60 @@
},
{
"cell_type": "markdown",
"id": "model_cell",
"id": "6e51e3e8-4cdb-4fcc-8a91-8eb310550778",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
"### Setup variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acoustic-result",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"query = \"TSLA\"\n",
"fields = [\"image\", \"title\"]"
]
},
{
"cell_type": "markdown",
"id": "6e51e3e8-4cdb-4fcc-8a91-8eb310550778",
"id": "model_cell",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Fill in the informations you want to have"
"## Model"
]
},
{
"cell_type": "markdown",
"id": "13126c00-a741-46ef-baf1-91007f1fb92b",
"metadata": {},
"source": [
"### Get data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acoustic-result",
"id": "fa7b6134-b8cb-44e5-8dc4-be7ad1ba8088",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"fields = [\"image\", \"title\"]\n",
"COMPANY = \"TSLA\""
"df = newsapi.connect().get(query, fields=fields)\n",
"df"
]
},
{
Expand Down Expand Up @@ -167,14 +189,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fa7b6134-b8cb-44e5-8dc4-be7ad1ba8088",
"id": "2940c391-b3a8-4bc6-a966-36280e022e74",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"newsapi.get(COMPANY, fields=fields)"
"df"
]
}
],
Expand Down Expand Up @@ -209,4 +230,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
257 changes: 257 additions & 0 deletions News API/News_API_Get_everything.ipynb
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
}
Loading

0 comments on commit b2a169c

Please sign in to comment.