File size: 16,853 Bytes
e79fbb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
from config import settings
import pymongo
from urllib.parse import urlparse
def EatEasy_Mongo_Data_add(url):

    client = pymongo.MongoClient(settings.MONGO_URL)

    def restro_details(url,location_area,location_name):
        
        db = client.Restaurants_in_dubai
        collection = db.Restaurant_details

        parsed_url = urlparse(url)
        platform_name = parsed_url.netloc.split('.')[1].capitalize()
        restaurant_name = parsed_url.path.strip('/').split('/')[-1]
        restaurant_details = {
            'url' : url,
            'platform_name' : platform_name,
            "restaurant_name" : restaurant_name.strip(),
            "location_area" : location_area ,
            "location_name" : location_name
          }
        result = collection.insert_one(restaurant_details)
        print("Inserted document IDs:", result.inserted_id)
    

    def main_json(url_input):
        import requests
        import json
        from bs4 import BeautifulSoup
        from urllib.parse import urlparse

        payload1 = {'restId': '17902'}
        files = []

        headers2 = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': 'eateasy-ae-website=lai3mvcb9hd99nnivbt0pn68ibfjsd6g'
        }

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }

        

        parsed_url = urlparse(url_input)
        restaurant_code = parsed_url.path.strip('/').split('/')[-1]

        url = "https://www.eateasy.ae/dubai/food/getFilteredMenus"
        data = {
            "restCode": restaurant_code
        }

        response = requests.post(url_input, data=data)

        menu_items_list = []
        category_name_list = []
        j = 0

        if response.status_code == 200:
            soup = BeautifulSoup(response.text, 'html.parser')
            menu_item_boxes = soup.find_all('div', class_='menu-item-box')  # Find all divs with class 'menu-item-box'
            location_area = soup.find('div', class_='location-area').text
            input_element = soup.find('input', {'id': 'menuDeliveryLocationNewModalInput'})

            # Get the value of the value attribute
            location_name = input_element.get('value')
            # location_name = soup.find('div', class_='location').text   

            for i,item_box in enumerate(menu_item_boxes):  # Iterate over each menu item box
                img = item_box.find('img')  # Find the img tag within the div
                if img:  # Check if img tag exists
                    image_url = img.get('data-image')  # Get the value of 'data-image' attribute
                else:
                    image_url = None  # If image URL is not found

                menu_item_details = item_box.find('div', class_='menu-item-details')  # Find menu-item-details div
                if menu_item_details:
                    category = menu_item_details.find('p', class_='type').text.strip()
                    name = menu_item_details.find('h5', class_='menu-food-title').text.strip()
                    description = menu_item_details.find('p', itemprop='description').text.strip()
                    price = menu_item_details.find('div', class_='menu-item-price').text.strip()
                else:
                    # If menu-item-details not found, set defaults
                    category = ""
                    name = ""
                    description = ""
                    price = ""

                menu_list_options = item_box.find('a', class_='menu-list-options')  # Find the menu-list-options anchor tag
                if menu_list_options:
                    item_id = menu_list_options.get('value')  # Get the value attribute
                else:
                    item_id = None
                
                if category not in category_name_list:
                    category_name_list.append(category)
                    j = j+1
                    Category_position = j

                else:
                    Category_position = j

                menu_item = {
                    "Category": category,
                    "Category_position": Category_position,
                    "Item_name": name,
                    "Item_position": i,
                    "Image": image_url,
                    "Description": description,
                    "Price": price,
                    "ID": item_id,
                    "Option_groups": []
                
                }

                if item_id is not None:
                    url1 = f"https://www.eateasy.ae/dubai/order/add_to_cart_v1/{item_id}/1/"
                    response = requests.request("POST", url1, headers=headers2, data=payload1, files=files)
                    if response.status_code == 200:
                        try:
                            json_data = response.json()
                            option_group_data = {}

                            if 'arrResult' in json_data and 'arrFoodChoice' in json_data['arrResult']:
                                for option_group in json_data['arrResult']['arrFoodChoice']:
                                    group_name = option_group['choice_name']
                                    min_quantity = option_group['mandatory']
                                    max_quantity = max((option_group['max_choice']), "1")

                                    option_group_item = next((x for x in menu_item['Option_groups'] if x['Option_group_name'] == group_name), None)
                                    if option_group_item:
                                        option_group_item['Option_group_names'].append({
                                            "Option_name": option_group['name'],
                                            "Option_price": option_group['price']
                                        })
                                    else:
                                        menu_item['Option_groups'].append({
                                            "Option_group_name": group_name,
                                            "Min": min_quantity,
                                            "Max": max_quantity,
                                            "Option_group_names": [{
                                                "Option_name": option_group['name'],
                                                "Option_price": option_group['price']
                                            }]
                                        })
                            
                        except json.JSONDecodeError:
                            print("JSON decoding error. Response content may not be in valid JSON format.")
                    else:
                        print(f"Failed to get data for item with value {item_id}. Status code: {response.status_code}")
                if not menu_item['Option_groups']:  
                    menu_item["Has_choice"] = False
                
                    
                else:
                    menu_item["Has_choice"] = True
                menu_items_list.append(menu_item)
        else:
            print(f"Failed to get menu items. Status code: {response.status_code}")

        # Convert the list of menu items into JSON
        # with open(f'{restaurant_code}.json', 'w') as json_file:
        #     json.dump(menu_items_list, json_file, indent=4)

        # print(f"Menu items saved to {restaurant_code}.json file.")

        # main_excel_file(url_input)
        return menu_items_list,location_area,location_name
    

    def extract_category(items,url):
        db = client.Restaurants_in_dubai
        collection = db.Category
        json_data = items
        restro_collection = db['Restaurant_details']
      

        def item_extract_category(json,name):
            items_list = []
            for item in json:
                if item['Category'] == name:
                    item_info = {
                            "id": item["ID"],
                            "name": item["Item_name"]
                        }
                    items_list.append(item_info)

            return items_list
            
            
        categories_info = []
        existing_categories = set()
        for item in json_data:
            name = item['Category']
            if name not in existing_categories:
                # category_position = 1
                # category_isActive = True
                items = item_extract_category(json_data, name)

                restro = restro_collection.find_one({'url': url})
                if restro:
                        restro_id = restro['_id']
                            
                restro_ref_id = restro_id

                category_info = {
                    'category_name': name,
                    'items': items,
                    'restro_ref_id' : restro_ref_id
                }
                categories_info.append(category_info)
                existing_categories.add(name)
        result = collection.insert_many(categories_info)
        print("Inserted Category document IDs:", result.inserted_ids)

    def extract_item(items,url):
        db = client.Restaurants_in_dubai
        collection = db.Items
        json_data = items
        category_collection = db['Category']
        restro_collection = db['Restaurant_details']

        items_info = []
        
        for item in json_data:
            id = item['ID']
            name = item['Item_name']
            description = item['Description']
            price = item['Price']
            img_url= item['Image']
            category_name = item['Category']
            item_position = item['Item_position']
            has_choices = item['Has_choice']
            if has_choices == True:
                modifires = []
                for option_group in item.get('Option_groups', []):
                    modifires.append(option_group['Option_group_name'])
            else:
                modifires = "None"
            
            restro = restro_collection.find_one({'url': url})
            if restro:
                        restro_id = restro['_id']
                            
            restro_ref_id = restro_id

            category = category_collection.find_one(
                  {
                  'category_name': category_name,
                  'restro_ref_id': restro_ref_id
                  })
            if category:
                category_id = category['_id']
            
            ref_id = category_id


            item_info = {
                'item_id': id,
                'name': name,
                'description': description,
                'amount': price,
                'image': img_url,
                'category_name':category_name,
                'modifires':modifires,
                'ref_id_category' : ref_id,
                'restro_ref_id' : restro_ref_id
                
            }
            items_info.append(item_info)
        result = collection.insert_many(items_info)
        print("Inserted Items document IDs:", result.inserted_ids)


    def extract_option_group(items,url):
        db = client.Restaurants_in_dubai
        collection = db.OptionGroup
        option_group_info = []
        existing_categories = []
        option_group_names_list = []
        restro_collection = db['Restaurant_details']
        
        for item in items:
            for option_group in item.get('Option_groups', []):
                flag = 1
                restro = restro_collection.find_one({'url': url})
                if restro:
                    restro_id = restro['_id']
                            
                restro_ref_id = restro_id

                
                option_group_name = option_group["Option_group_name"]
                min_quantity = option_group["Min"]
                max_quantity = option_group["Max"]
                option_names = []
                option_names_trial_all = []

                for option in option_group.get("Option_group_names", []):
                    option_name = option["Option_name"]
                    option_price = option["Option_price"]
                    option_names.append(option_name)
                    option_names_trial = {
                                'Option_name': option_name,
                                'Option_price': option_price              
                                }
                    option_names_trial_all.append(option_names_trial)
                item_id = []
                for item in items:
                    for option_group in item.get('Option_groups', []):
                        option_group_name2 = option_group["Option_group_name"]
                        option_group_names1 = option_group.get('Option_group_names',[])
                        if(option_group_name2 == option_group_name and (sorted(option_group_names1, key=lambda x: x['Option_name'])) ==  sorted(option_names_trial_all, key=lambda x: x['Option_name']) ):
                                item_id.append(item['ID'])
                    
                option_group_information = {
                    "option_group_name" : option_group_name,
                    "min_quantity" : min_quantity,
                    "max_quantity" : max_quantity,
                    "option_names"  : option_names,
                    "item_id" : item_id,
                    "restro_ref_id" : restro_ref_id
                }
                option_group_check ={
                    "option_group_name" : option_group_name,
                     "option_names"  : option_names
                    }
                for category in existing_categories:
                    if (category['option_group_name'] == option_group_check['option_group_name'] and
                        sorted(category['option_names']) == sorted(option_group_check['option_names'])):
                        flag = 0
                if flag==1:
                    option_group_info.append(option_group_information)
                
                existing_categories.append(option_group_check)
        result = collection.insert_many(option_group_info)
        print("Inserted Option_Groups document IDs:", result.inserted_ids)
 
    def extract_option_group_names(items,url):
        db = client.Restaurants_in_dubai
        collection = db.OptionName
        option_group_collection = db['OptionGroup']
        json_data = items
        option_names = []
        option_names_list = []
        restro_collection = db['Restaurant_details']

    
        for item in json_data:
            has_choices = item['Has_choice']
            if has_choices == True:
                flag = 1
                for option_group in item.get('Option_groups', []):
                    option_group_name2 = option_group["Option_group_name"]
                    for option in option_group.get("Option_group_names", []):
                        restro = restro_collection.find_one({'url': url})
                        if restro:
                            restro_id = restro['_id']
                            
                        restro_ref_id = restro_id
                        option_name = option["Option_name"]
                        option_price = option["Option_price"]

                        query = {
                           
                            'restro_ref_id': restro_id,
                            'option_names': option_name  
                        }

                        matching_documents = option_group_collection.find(query)
                        matching_ids = [doc['_id'] for doc in matching_documents]
                        # if option_group_name_true:
                        #         option_group_id = option_group_name_true['_id']
                        # restro = restro_collection.find_one({'url': url})
                        option_group_name = {
                                "option_name" : option_name,
                                "option_price" : option_price,  
                                "ref_option_group_id" : matching_ids,
                                "restro_ref_id" : restro_ref_id
                            }
                        if (option_name in option_names_list ):
                                flag = 0
                        if flag==1:
                            option_names.append(option_group_name)
                        option_names_list.append(option_name)

        result = collection.insert_many(option_names)
        print("Inserted Options_name document IDs:", result.inserted_ids)

    items,location_area,location_name = main_json(url)
    restro_details(url,location_area,location_name)
    extract_category(items,url)
    extract_item(items,url)
    extract_option_group(items,url)
    extract_option_group_names(items,url)
    return True