mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-06 01:12:27 +00:00
* Fixes * Update stocks_helper.py * update git-actions set-output to new format * Update stocks_helper.py * Update terminal_helper.py * removed LineAnnotateDrawer from qa_view * lint * few changes * updates * sdk auto gen modules done * Update stocks_helper.py * updates to changed imports, and remove first sdk_modules * Update generate_sdk.py * Update generate_sdk.py * pylint * revert stocks_helper * Update generate_sdk.py * Update sdk.py * Update generate_sdk.py * full auto generation, added sdk.py/controllers creation * missed enable forecasting * added running black in subprocess after sdk files generation completes * removed deleted sdk_arg_logger * comment out tests * property doc fix * clean up * Update generate_sdk.py * make trailmap classes useable for doc generation * Update generate_sdk.py * added lineon to trailmap class for linking to func in markdown * changed lineon to dict * added full_path to trailmap for linking in docs * updated portfolio * feat: initial files * feat: added meta head * feat: added funcdef * added func_def to trailmap attributes for markdown in docs, added missing type hints to covid functions * feat: added view and merged with jaun * Update generate_sdk.py * Update generate_sdk.py * Update generate_sdk.py * Update generate_sdk.py * init * fix returns * fix: random stuff * fix: random * fixed encoding issue on windows * fix: generate tabs * update * Update generate_sdk_markdown.py * Create .pydocstyle.ini * added type hint classes for views * fixes * alt, ba * alt-economy * Update finviz_compare_model.py * fixs * Update substack_model.py * Update generate_sdk.py * last of my section * porfolio * po * Update optimizer_model.py * fixing more things * few more * keys done * update * fixes * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * mypy forecast fix * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * fixes * forecast fixes * one more fix * Update coinbase_model.py * Update generate_sdk_markdown.py Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Co-authored-by: James Maslek <jmaslek11@gmail.com> Co-authored-by: jose-donato <zmcdonato@gmail.com> Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>
95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
""" Feedparser Model """
|
|
__docformat__ = "numpy"
|
|
|
|
import os
|
|
from typing import List
|
|
from urllib.parse import quote
|
|
import certifi
|
|
import feedparser
|
|
import pandas as pd
|
|
from openbb_terminal.rich_config import console
|
|
|
|
|
|
def get_news(
|
|
term: str = "", sources: str = "", sort: str = "published"
|
|
) -> pd.DataFrame:
|
|
"""Get news for a given term and source. [Source: Feedparser]
|
|
|
|
Parameters
|
|
----------
|
|
term : str
|
|
term to search on the news articles
|
|
sources: str
|
|
sources to exclusively show news from (separated by commas)
|
|
sort: str
|
|
the column to sort by
|
|
|
|
Returns
|
|
-------
|
|
articles: pd.DataFrame
|
|
term to search on the news articles
|
|
"""
|
|
# Necessary for installer so that it can locate the correct certificates for
|
|
# API calls and https
|
|
# https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error/73270162#73270162
|
|
os.environ["REQUESTS_CA_BUNDLE"] = certifi.where()
|
|
os.environ["SSL_CERT_FILE"] = certifi.where()
|
|
|
|
have_data = False
|
|
console.print("[yellow]Fetching data. Please be patient\n[/yellow]")
|
|
limit = 0
|
|
|
|
while not have_data:
|
|
if term:
|
|
term = quote(term)
|
|
data = feedparser.parse(
|
|
f"https://news.google.com/rss/search?q={term}&hl=en-US&gl=US&ceid=US:en&when:24h+allinurl"
|
|
f':{sources.replace(" ", "%20")}'
|
|
)
|
|
else:
|
|
data = feedparser.parse(
|
|
f'https://news.google.com/rss/search?q=when:24h+allinurl:{sources.replace(" ", "%20")}'
|
|
"&hl=en-US&gl=US&ceid=US:en"
|
|
)
|
|
|
|
if (
|
|
hasattr(data, "status") and data.status == 200
|
|
): # Checking if data has status attribute and if data request succeeded
|
|
if data.entries:
|
|
have_data = True
|
|
elif limit == 60: # Breaking if 60 successful requests return no data
|
|
console.print("[red]Timeout occurred. Please try again\n[/red]")
|
|
break
|
|
limit = limit + 1
|
|
|
|
elif hasattr(data, "status") and data.status != 200: # If data request failed
|
|
console.print("[red]Status code not 200. Unable to retrieve data\n[/red]")
|
|
break
|
|
else:
|
|
console.print("[red]Could not retrieve data\n[/red]")
|
|
break
|
|
|
|
# Filter based on data sources
|
|
if sources:
|
|
newdata: List = []
|
|
for entry in list(data.entries):
|
|
# check if sources specified
|
|
if "," in sources:
|
|
if entry["source"]["title"].lower().find(sources.lower()) != -1:
|
|
newdata.append(entry)
|
|
else:
|
|
for s in sources.split(","):
|
|
if entry["source"]["title"].lower().find(s.lower()) != -1:
|
|
newdata.append(entry)
|
|
|
|
if newdata:
|
|
df = pd.DataFrame(newdata, columns=["title", "link", "published"])
|
|
else:
|
|
return pd.DataFrame()
|
|
else:
|
|
df = pd.DataFrame(data.entries, columns=["title", "link", "published"])
|
|
df["published"] = pd.to_datetime(df["published"])
|
|
df = df.sort_values(by=[sort], ascending=False)
|
|
|
|
return df
|