import requests
import json


def westBengalStateID(state):
    """
    Function to get state id from state name derived by reverse engineering
    pincode

    Parameters
    ----------
    state : String
        State name.

    Returns
    -------
    state_id : String
        ID associated with that state name

    """
    
    state_id = ''
    url = "https://cdn-api.co-vin.in/api/v2/admin/location/states"
    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)
        state_length = len(parsed_response['states'])
        for idx in range(state_length):
            if((parsed_response['states'][idx]['state_name']).lower().replace(" ", "") == state.lower().replace(" ", "")):
                state_id = parsed_response['states'][idx]['state_id']
        return(state_id)
    except Exception as e:
        print(e)