import requests
import json


def getWbDistrictID(st_id, lookout_district):
    """
    Function to get district id from state id and district name

    Parameters
    ----------
    st_id : String
        ID associated with a state.
    lookout_district : String
        District name for which we need the district id.

    Returns
    -------
    district_id : String
        ID associated with a district.

    """
    
    district_id = ''
    url = "https://cdn-api.co-vin.in/api/v2/admin/location/districts/{st}".format(st = st_id)
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36' }
    try:
        response = requests.get(url,headers=headers)
        parsed_response = json.loads(response.text)
        district_length = len(parsed_response['districts'])
        for idx in range(district_length):
            if((parsed_response['districts'][idx]['district_name']).lower().replace(" ", "") == lookout_district.lower().replace(" ", "")):
                district_id = parsed_response['districts'][idx]['district_id']
        return (district_id)
    except Exception as e:
        print(e)