Skip to content

Commit

Permalink
patching news with comments (#2530)
Browse files Browse the repository at this point in the history
adds functionality to keep runnings news until data is received. stops after 60 tries
  • Loading branch information
simmonsj330 authored Sep 13, 2022
1 parent e3b4ff5 commit 43dd40a
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions openbb_terminal/common/feedparser_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import feedparser
import pandas as pd
from openbb_terminal.rich_config import console


def get_news(
Expand All @@ -23,25 +24,42 @@ def get_news(
articles : dict
term to search on the news articles
"""
if term:
if sources:
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")}'
)
have_data = False
console.print("[yellow]Fetching data. Please be patient\n[/yellow]")
limit = 0
while not have_data:
if term:
if sources:
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={term}&when:24h&hl=en-US&gl=US&ceid=US:en"
)
else:
data = feedparser.parse(
f"https://news.google.com/rss/search?q={term}&when:24h&hl=en-US&gl=US&ceid=US:en"
)
else:
if sources:
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"
)
else:
data = feedparser.parse(
"https://news.google.com/rss/search?q=when:24h&hl=en-US&gl=US&ceid=US:en"
)
if sources:
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"
)
else:
data = feedparser.parse(
"https://news.google.com/rss/search?q=when:24h&hl=en-US&gl=US&ceid=US:en"
)

if data.status == 200: # Checking 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 data.status != 200: # If data request failed
console.print("[red]Status code not 200. Unable to retrieve data\n[/red]")
break

return pd.DataFrame(data.entries, columns=["title", "link", "published"])

0 comments on commit 43dd40a

Please sign in to comment.