how can we get news directly on codebook?

how can we get news directly on codebook? can it be done without the api key

i am using currently

df = ek.get_news_headlines('Topic:COVID AND Language:LEN AND Source:RTRS', date_from='2021-04-01T09:00:00',date_to='2021-04-02T09:00:00',count=50)

df.head()

is it possible to do it directly on codebook? if so function?

Best Answer

  • @akshmita so Codebook uses a default app key - you obviously have to be logged into Eikon in order to launch codebook.

    import refinitiv.dataplatform.eikon as ek
    from IPython.display import HTML

    ek.set_app_key('DEFAULT_CODE_BOOK_APP_KEY')

    then use your API call:

    df = ek.get_news_headlines('Topic:COVID AND Language:LEN AND Source:RTRS', date_from='2021-04-01T09:00:00',date_to='2021-04-02T09:00:00',count=50)
    df.head()

    and as expected it works fine:

    Screenshot 2021-09-21 at 10.49.59.png

Answers

  • thanks @jason.ramchandani

    I have another issue: eikon news through api should be available for last 15 months,

    however i am getting from june 2020 (last 12 months)

    any way to sort the issue?


  • @akshmita So the limit of news headlines per API call is 100 (in your case you limit it at 50) - to get more headlines than this you will have to iterate using date as a parameter. Please check the generalised example below:

    import pandas as pd
    import datetime
    from datetime import datetime
    import dateutil.relativedelta
    now = datetime.now()
    maxenddate = now - dateutil.relativedelta.relativedelta(months=15) #months,days
    print(now, maxenddate)
    newsdf = pd.DataFrame()
    startdf=now
    while startdf >= maxenddate:
    try:
    df1 = ek.get_news_headlines('Topic:COVID AND Language:LEN AND Source:RTRS', date_to = startdf, count=100)
    startdf = df1['versionCreated'].min().replace(second=0,microsecond=0,tzinfo=None).strftime('%Y/%m/%d %H:%M')
    startdf = datetime.strptime(startdf,'%Y/%m/%d %H:%M')
    if len(newsdf):
    newsdf = pd.concat([newsdf, df1], axis=0)
    else:
    newsdf = df1
    except Exception:
    break

    newsdf.info()

    This will give you 15 months worth of news for a query (obviously you can just replace my query with whatever query you want). I trust this can help.

  • @jason.ramchandani thanks for the code,

    i tried the code, but getting 0 results, may i doing something wrong?

    import datetime

    from datetime import datetime

    import dateutil.relativedelta

    now = datetime.now()

    maxenddate = now - dateutil.relativedelta.relativedelta(months=15) #months,days

    print(now, maxenddate)

    output:

    2021-09-23 11:03:46.402525 2020-06-23 11:03:46.402525



    newsdf = pd.DataFrame()

    startdf=now

    while startdf >= maxenddate:

    try:

    df1 = ek.get_news_headlines('Topic:COVID AND Language:LEN AND Source:RTRS', date_to = startdf, count=100)

    startdf = df1['versionCreated'].min().replace(second=0,microsecond=0,tzinfo=None).strftime('%Y/%m/%d %H:%M')

    startdf = datetime.strptime(startdf,'%Y/%m/%d %H:%M')

    if len(df1):

    newsdf = pd.concat([newsdf, df1], axis=0)

    else:

    newsdf = df1

    except Exception:

    break

    newsdf.info()


    output:

    <class 'pandas.core.frame.DataFrame'> Index: 0 entries Empty DataFrame
  • @akshmita apols I found 2 small errors in the code I pasted - one missing import and if len(df1): should be if len(newsdf): - I have altered the code above and it works fine. You might want to test it by not running the full 15 months of news - as this will take some time - maybe change the following line:

    maxenddate = now - dateutil.relativedelta.relativedelta(months=15) #months,days

    to

    maxenddate = now - dateutil.relativedelta.relativedelta(days=3) #months,days

    I hope this can help.