import requests class MANDI_PRICE: def __init__(self): # Base URL to get the Mandi Price details self.base_url = "https://enam.gov.in/web/Ajax_ctrl/trade_data_list" # self.base_url = "https://enam.gov.in/web/dashboard/trade-data" def get_mandi_price(self, state_name, apmc_name, commodity_name, from_date, to_date ): # Prepare the payload for POST request payload = f"language=en&stateName={state_name}&apmcName={apmc_name}&commodityName={commodity_name}&fromDate={from_date}&toDate={to_date}" headers = { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8", "Referer": "https://enam.gov.in/web/dashboard/trade-data", "Accept": "application/json, text/javascript, */*; q=0.01", } response = requests.post( self.base_url, json=payload, headers=headers, ) return response.json() def get_mandi_states( self ): base_url = 'https://enam.gov.in/web/ajax_ctrl/states_name' headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', } response = requests.get( base_url, headers=headers, ) response = response.json() response = response.get('data', []) # state_names = [i for i in map(lambda x: x.get('state_name', ''), response)] state_names_ids = {} for state in response: if state.get('state_name', ''): state_names_ids[state.get('state_name')] = state.get('state_id', '') return state_names_ids def get_mandi_apmcs( self, state_name ): base_url = 'https://enam.gov.in/web/Ajax_ctrl/apmc_list' headers = { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8", "Referer": "https://enam.gov.in/web/dashboard/trade-data", "Accept": "application/json, text/javascript, */*; q=0.01", } payload = f'state_id={self.get_mandi_states().get(state_name)}' response = requests.post( base_url, data=payload, headers=headers ) response = response.json() response = response.get('data', []) # apmc_names = [i for i in map(lambda x: x.get('apmc_name', ''), response)] apmc_names_ids = {} for apmc in response: if apmc.get('apmc_name', ''): apmc_names_ids[apmc.get('apmc_name')] = apmc.get('apmc_id', '') return list(apmc_names_ids.keys()) def get_mandi_commodity( self, state_name, apmc_name, from_date, to_date ): base_url = 'https://enam.gov.in/web/Ajax_ctrl/commodity_list' headers = { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8", "Accept": "application/json, text/javascript, */*; q=0.01", "Referer": "https://enam.gov.in/web/dashboard/trade-data" } payload = f'language=en&stateName={state_name}&apmcName={apmc_name}&fromDate={from_date}&toDate={to_date}' response = requests.post( base_url, data=payload, headers=headers ) response = response.json() commodity_names_ids = response.get('data', []) commodity_list = [i for i in map(lambda x: x.get('commodity', ''), commodity_names_ids)] return commodity_list