news.get_headlines - how to include column for RICS

Hi
I am using Refinitiv Data library to collect news articles in Python, which is working fine.
Any idea of how I can return the list of RICS that the article relates to, just like News Monitor does in Eikon Desktop. I've included a snapshot of what I need below.
Here is my current code:
reg_df = pd.DataFrame(columns=['timestamp_API', 'RIC', 'headline', 'story', 'storyid'])
headlines = rd.news.get_headlines('R:TSLA.O',
start='2023-09-01T09:00:00',
end='2023-11-08T23:59:00', #API is 1hr behind the UK
count = 100)
headlines = headlines.reset_index()
temp_df = pd.DataFrame(columns=['timestamp_API', 'headline', 'story', 'storyid'])
for index, row in headlines.iterrows():
newsText = rd.news.get_story(row['storyId'], format=rd.news.Format.TEXT) #get the news story
temp_df.loc[index] = [row['versionCreated'], row['headline'], newsText, row['storyId']]
time.sleep(1)
company_news_df = pd.concat([reg_df, temp_df], ignore_index=True)
display(company_news_df)
A snapshot of what I'm after in the dataframe is attached.
Any help greatly appreciated.
Thanks
Best Answer
-
@di.ti Thanks for your question - so to do this you would need to use the RD Library - which is our latest library providing access to EIkon/Workspace services as well as newer services on the Data Platform. You can use both RD services as well as the Eikon Data API from the same library. First you need to install the library:
pip install refinitiv-data
once this is installed you can:
import refinitiv.data as rd
# import refinitiv.data.eikon as ek #you can also use the Eikon Data API from here
from refinitiv.data.content import news
from IPython.display import HTML
import pandas as pdrd.open_session()
# ek.set_app_key('Your_AppKey_Here')dNow = datetime.now().date()
maxenddate = dNow - timedelta(days=7) #upto months=15
compNews = pd.DataFrame()
riclist = ['VOD.L','HD','MSFT.O'] # can also use Peers, Customers, Suppliers, Monitor, Portfolio to build universe
for ric in riclist:
try:
cHeadlines = rd.news.get_headlines("R:" + ric + " AND Language:LEN", start= str(dNow), end = str(maxenddate), count = 10)
cHeadlines['cRIC'] = ric
if len(compNews):
compNews = pd.concat([compNews,cHeadlines])
else:
compNews = cHeadlines
except Exception:
pass
compNewsNow you need to use the delivery layer of the RD Library to make an endpoint request to the news story endpoint to access additional metadata such as story text, topic codes etc. We will get all the topic codes and then extract the PermIDs that were tagged for this article.
# For each news headline get story text and topic codes
baseurl = "/data/news/v1/stories/"
fullcodelist = pd.DataFrame()
compNews['storyText'] = str()
compNews['q_codes'] = str()
compNews['sentiment'] = str()
compNews['cos_mentioned'] = str()
for i, uri in enumerate(compNews['storyId']):
rics=[]
request_definition = rd.delivery.endpoint_request.Definition(
url = baseurl + uri,
method = rd.delivery.endpoint_request.RequestMethod.GET
)
response = request_definition.get_data()
time.sleep(0.1)
rawr = response.data.raw
#print(rawr)
if 'newsItem' in rawr.keys():
compNews['storyText'][i] = rawr['newsItem']['contentSet']['inlineData']['$']
topics = rawr['newsItem']['contentMeta']['subject']
compNews['q_codes'][i] = [d['_qcode'] for d in topics]
compNews['cos_mentioned'][i] = [x for x in compNews['q_codes'][i] if x.startswith("P:")]
compNewsSo in the column compNew['cos_mentioned'] we have a list of PermIDs that were mentioned in the article. You want to use PermIDs as these will include private companies that may not have a RIC. You can then pass the PermIDs to other functions such as rd.get_data() to get additional reference and fundamental information. I hope this can help.
0
Answers
-
You can also use the refinitiv library to go through the dataframe with story ids to get the RIC codes:
from refinitiv.data.content import news
storyid = 'urn:newsml:reuters.com:20231110:nRTV8tfcw0:4'
story_data = news.story.Definition(storyid).get_data()
rics = [i['_qcode'][2:] for i in story_data._raw['newsItem']['assert'] if i['_qcode'].startswith("R:")]0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛