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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# url = input("Enter Restro Url : ")
from urllib.parse import urlparse
import pymongo
from config import settings

def main(url,location,inside_location):

    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,
            "location_area" : location_area ,
            "location_name" : location_name
          }
        result = collection.insert_one(restaurant_details)
        print("Inserted document IDs:", result.inserted_id)

    def main_excel_file(url_input):
        import requests
        import json
        import pandas as pd
        from bs4 import BeautifulSoup
        from urllib.parse import urlparse
        import math

        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, 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'

            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:
                    type_ = 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
                    type_ = ""
                    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:
                    value = menu_list_options.get('value')  # Get the value attribute
                else:
                    value = None

                if type_ not in category_name_list:
                    category_name_list.append(type_)
                    j = j+1
                    Category_position = j

                else:
                    Category_position = j

                menu_item = {
                    "Category": type_,
                    "Category_position": Category_position,
                    "Item_name": name,
                    "Item_position": i, 
                    "Image": image_url,
                    "description": description,
                    "price": price,
                    "id": value,
                }

                menu_items_list.append(menu_item)  # Append menu item before the request
                if value is not None:
                    option_url = f"https://www.eateasy.ae/dubai/order/add_to_cart_v1/{value}/1/"
                    option_response = requests.post(option_url, headers=headers2, data=payload1)
                    if option_response.status_code == 200:
                        try:
                            json_data = json.loads(option_response.text)
                            extracted_data = []

                            if 'arrResult' in json_data and 'arrFoodChoice' in json_data['arrResult']:
                                for choice in json_data['arrResult']['arrFoodChoice']:
                                    extracted_data.append({
                                        'Option_group_name': choice['choice_name'],
                                        'Option_name': choice['name'],
                                        'Extra_price': choice['price'],
                                        'Min': choice.get('mandatory', 0),
                                        'Max': choice.get('max_choice', 1)
                                    })

                            grouped_data = {}
                            for choice in extracted_data:
                                group_name = choice['Option_group_name']
                                if group_name not in grouped_data:
                                    grouped_data[group_name] = {
                                        'Option_group_name': group_name,
                                        'Min': choice['Min'],
                                        'Max': max(choice['Max'], '1'),
                                    }
                                
                                num_options = sum(key.startswith('Option ') for key in grouped_data[group_name])
                                option_index = num_options + 1  # Index for the new option
                                grouped_data[group_name][f"Option {option_index} Name"] = choice['Option_name']
                                grouped_data[group_name][f"Option {option_index} Price"] = choice['Extra_price']
                                
                            for group_data in grouped_data.values():
                                menu_items_list.append(group_data)

                        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 {value}. Status code: {option_response.status_code}")

            df = pd.DataFrame(menu_items_list)

            # Ensure 'Max' column exists before renaming or shifting
            if 'Max' in df.columns:
                max_column_index = df.columns.get_loc('Max')
                for i in range(max_column_index + 1, len(df.columns)):
                    df.rename(columns={df.columns[i]: ''}, inplace=True)

                option_group_name_index = df.columns.get_loc('Option_group_name')
                for i in range(option_group_name_index, len(df.columns)):
                    df.iloc[:, i] = df.iloc[:, i].shift(-1)

            excel_file = f"{restaurant_code}_menu.xlsx"
            df.to_excel(excel_file, index=False)
            print(f"Menu items saved to {excel_file}")

        else:
            print(f"Failed to get menu items. Status code: {response.status_code}")

    

    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
            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 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 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 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 document IDs:", result.inserted_ids)

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


# main(url)