News Process Flow

Identify the Country Name

First step to get the news is Identify the Country name from the Human Raw Speech, to do this getCountryName flow is triggered.

getCountryName uses pycountry package to get the list of countries and thereafter check this list against the rawspeech,

  • If there's a perfect match then country name gets identified

  • In case no country is identified then country name is defaulted to human's default_country parameter.

Country Name is defaulted to default_country param to answer questions "Aryan, Can you get the news?" wherein no country is provided.

i=0
while i<len(pycountry.countries):
    if subStrCheck(rawspeech,(list(pycountry.countries)[i].name)):
        countryname = list(pycountry.countries)[i].name
        break
    i = i+1
    if i == len(pycountry.countries):
        countryname = default_country 

pycountry packages provides the ISO databases for the standards for Countries, Languages, Deleted Countries, Subdivisions of Countries, Currencies, Scripts.

Identify the Country Code

Once Country Name is Identified, Aryan needs to get the Country Code, Example: "US" being country Code for United States, While "IN" being for India. This is needed to pass the country code for the requested news country in the News API Call.

getCountryCode flow does this work for Aryan, Attributes for a country are identified and thereafter alpha_2 attribute is used to get the countrycode.

countriesattributes = pycountry.countries.get(name=countryname)
countrycode = countriesattributes.alpha_2 

Get News For the Country Code

API calls are made to NewsApi along with the country code and Aryan's API key to Search the News. Response is captured in JSON format. Below is the snippet which does this for Aryan and Arya.

News Api is a simple, easy-to-use REST API that returns JSON search results for current and historic news articles published by over 80,000 worldwide sources.

newsurl = ('https://newsapi.org/v2/top-headlines?country=' + countrycode + '&apiKey=' + newsapi_apikey)
response = requests.get(newsurl)
news = json.loads(response.text)

Parse the News Title and Description

Once Aryan has the news dump in JSON format, Next step is to parse the JSON to get the title and description of the news article.

Once captured, Top 5 News Headlines are provided to the human after sleep of 2 sec between each news.

    i=1
    for new in news['articles']:

        if i ==1:
            PrintText(aifriend, "Title: " + str(new['title']))
            SpeakText(aifriend,str(new['title']))
        else:
            PrintText(aifriend, "Next News is Title: " + str(new['title']))
            SpeakText(aifriend,"Next News is " + str(new['title']))

        PrintText(aifriend,"Description: " + str(new['description']))
        SpeakText(aifriend,str(new['description']))

        i = i+1

        if i==5:
            break

        time.sleep(2)

Last updated