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.

Thanksrefinitiv-ric-snapshot.png

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 pd
    rd.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
            
    compNews

    1699618335706.png

    Now 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:")]
                
    compNews

    1699618763311.png

    So 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.

Answers

  • Hi @jason.ramchandani01

    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:")]