# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: RevData39
#     language: python
#     name: rd39
# ---

# ----
# <img src="../../../files/refinitiv.png" width="20%" style="vertical-align: top;">
#
# # Data Library for Python
#
# ----

# ## Content - Search, Lookup and View Metadata
# This notebook demonstrates how to utilise the Search API using the Refinitiv Data Platform OR via Refinitiv Eikon/Workspace.
#
# For more details on the Search API refer to the following:
#
# * The article, [Building Search into your Application Workflow](https://developers.refinitiv.com/en/article-catalog/article/building-search-into-your-application-workflow), provides an overview of the capabilities of the Search service including tips and tricks and many useful techniques.
#
# * Search API documentation: [Search Reference Guide](https://apidocs.refinitiv.com/Apps/ApiDocs#/details/L2Rpc2NvdmVyeS9zZWFyY2gvdjE=/Lw==/POST/README)
#

# ## Set the location of the configuration file
# For ease of use, you can set various initialization parameters of the RD Library in the **_refinitiv-data.config.json_** configuration file - as described in the Quick Start -> Sessions example.
#
# ### One config file for the tutorials
# As these tutorial Notebooks are categorised into sub-folders and to avoid the need for multiple config files, we will use the _RD_LIB_CONFIG_PATH_ environment variable to point to a single instance of the config file in the top-level ***Configuration*** folder.
#
# Before proceeding, please **ensure you have entered your credentials** into the config file in the ***Configuration*** folder.

import os
os.environ["RD_LIB_CONFIG_PATH"] = "../../../Configuration"

from refinitiv.data.content import search
import refinitiv.data as rd
import pandas as pd
import json

# ## Open the default session
#
# To open the default session ensure you have a '*refinitiv-data.config.json*' in the ***Configuration*** directory, populated with your credentials and specified a 'default' session in the config file    
#

# + jupyter={"outputs_hidden": false} pycharm={"name": "#%%\n"}
rd.open_session()
# -

# #### General Search

response = search.Definition("IBM Bonds").get_data()
response.data.df

# #### Specify number of rows to fetch

response = search.Definition("IBM Bonds", top=100).get_data()
response.data.df

# #### Filter Expressions

response = search.Definition(
    view=search.SearchViews.GOV_CORP_INSTRUMENTS,
    select="ISIN,RIC,IssueDate,Currency,FaceIssuedTotal,CouponRate,MaturityDate", 
    filter="IssuerTicker eq 'IBM' and IsActive eq true and AssetStatus ne 'MAT'"
).get_data()
response.data.df

# #### Properties - Metadata

# +
response = search.metadata.Definition(
    view = search.SearchViews.GOV_CORP_INSTRUMENTS  # Required parameterc
).get_data()

# for Pandas Display purpose only
pd.set_option("display.max_columns", None) 
pd.set_option("display.max_rows", 10)  # Just show 10 rows
pd.set_option("display.max_colwidth", 1)  

response.data.df
# -

# #### Navigators

response = search.Definition(
    top=10,
    navigators="RCSTRBC2012Leaf"
).get_data()


# #### Response Data Structure

# + tags=[]
# Depending on the content type, the response can contain raw data and/or Dataframe format
# Let's check what this particular query returns.
if response.data.raw:
    display(response.data.raw)
else:
    print("Response does not contain raw data")    
# Check if the response contains a Dataframe too?
if (not response.data.df.empty):
    display(response.data.df)
else:
    print("\nResponse does not contain DataFrame")
# -

# As you will see the above response contained both raw data and a Dataframe   
#
# #### Search with Navigator option

# +
response = search.Definition(
    view=search.SearchViews.COMMODITY_QUOTES,
    query="cheese",
    navigators="ExchangeName"
).get_data()

response.data.df
# -

response.data.raw["Navigators"]['ExchangeName']

# +
response = search.Definition(
    view=search.SearchViews.GOV_CORP_INSTRUMENTS,
    top=0,
    navigators="Currency(buckets:10,desc:sum_FaceOutstandingUSD,calc:max_CouponRate)"
).get_data()

response.data.raw
# -

# #### Sub-navigator

# +
response = search.Definition(
    view=search.SearchViews.PEOPLE,
    top=0,
    navigators="FirstName(buckets:3,sub:LastName(buckets:2))"
).get_data()

response.data.raw["Navigators"]["FirstName"]["Buckets"]
# -

# #### Search with order_by option

# +
response = search.Definition(
    view=search.SearchViews.PEOPLE,
    query="ceo",
    order_by="YearOfBirth desc,LastName,FirstName",
    select="YearOfBirth,DocumentTitle"
).get_data()

response.data.df
# -

# #### Search with group_by option

# +
response = search.Definition(
    view=search.SearchViews.INDICATOR_QUOTES,
    query="rate",
    group_by="CentralBankName",
    group_count=2,
    select="CentralBankName,DocumentTitle,RIC"
).get_data()

response.data.df
# -

# #### Search For People

# +
response = search.Definition(
    query="cfo",
    view=search.SearchViews.PEOPLE
).get_data()

response.data.df
# -

# ### Lookup

# +
response = search.lookup.Definition(
    view=search.SearchViews.INSTRUMENTS,                               # Required parameter
    scope="RIC",                                                       # Required parameter
    terms="MSFT.O,AAPL.O,GOOG.O,IBM.N,KBANK.BK,SCC.BK",                # Required parameter
    select="BusinessEntity,DocumentTitle,CUSIP,SEDOL",                 # Required parameter
).get_data()

response.data.df

# +
response = search.lookup.Definition(
    view=search.SearchViews.SEARCH_ALL,                              # Required parameter
    scope="RIC",                                                     # Required parameter
    terms="A,B,NOSUCHRIC,C,D",                                       # Required parameter
    select="BusinessEntity,DocumentTitle",                           # Required parameter
).get_data()

response.data.df
# -

# ## Close the session

rd.close_session()



StarcoderdataJupyterScriptsDedupFiltered