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.
2
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:")]2
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛