File size: 3,587 Bytes
5273d83
 
 
 
 
d2ef46d
5273d83
d2ef46d
5273d83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2ef46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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