-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicpin.php
141 lines (117 loc) · 5.35 KB
/
magicpin.php
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
<?php
include("database.php");
?>
<?php
require_once('C:/xampp/htdocs/drivertest/vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverSelect;
use Facebook\WebDriver\WebDriverWait;
use \Facebook\WebDriver\WebDriverKeys;
// Function to extract restaurant name from JSON data
function getRestaurantName($jsonData) {
return $jsonData['restaurantName'];
}
function getRestaurantLocation($jsonData) {
return $jsonData['restaurantLocation'];
}
// Function to extract food items and quantities from JSON data
function getFoodItemsAndQuantities($jsonData) {
return $jsonData['foodItemsAndQuantities'];
}
// Function to automate Magicpin cart process
function automateMagicpinCart($restaurantName,$restaurantLocation, $foodItemsAndQuantities) {
$host = 'http://localhost:4444/wd/hub'; // Selenium server URL
$capabilities = \Facebook\WebDriver\Remote\DesiredCapabilities::chrome();
// Set additional browser options
$options = new \Facebook\WebDriver\Chrome\ChromeOptions();
$options->addArguments(['--incognito', '--start-maximized']);
// Set the executable path of Chrome
$options->setBinary('C:/Program Files/Google/Chrome/Application/chrome.exe');
$capabilities->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options);
// Start the WebDriver session
$driver = RemoteWebDriver::create($host, $capabilities);
// Go to magicpin.com
$driver->get('https://magicpin.com/');
$wait = new WebDriverWait($driver, 10);
// Enter the location in the search bar
$locationInput = $driver->findElement(WebDriverBy::cssSelector('#city-popup-builder > form > div.searchLocation > input'));
$locationInput->sendKeys($restaurantLocation);
sleep(2);
$locationSelector = $wait->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector('#city-popup-builder > form > div.searchLocation > section > div > li:nth-child(1)')
));
$locationSelector->click();
$restaurantInput = $driver->findElement(WebDriverBy::cssSelector('#city-popup-builder > form > div.searchQuery > input'));
$restaurantInput->sendKeys($restaurantName);
$restaurantInput->sendKeys(WebDriverKeys::ENTER);
$resturantSelector = $wait->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector('#react-search-results > main > section > div.result-card-container > div:nth-child(1) > div.mx-info-holder > div.mx-info > h3 > a')
));
if ($resturantSelector) {
$resturantSelector->click();
}
else{
$resturantSelector = $wait->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector('#react-search-results > main > section > div > div > div.mx-info-holder > div.mx-info > h3 > a')
));
$resturantSelector->click();
}
$deliveryTab = $wait->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector('#nav-holder > nav > a.delivery-tab.rel-handled')
));
$deliveryTab->click();
sleep(8);
foreach ($foodItemsAndQuantities as $foodItem => $quantity) {
$foodItemInput = $driver->findElement(WebDriverBy::cssSelector('#searchBarCatalog > div > input'));
$foodItemInput->sendKeys($foodItem);
sleep(3);
$addButton = $driver->findElement(WebDriverBy::cssSelector('div > section > div.itemCountHolder > div > button'));
$addButton->click();
$foodItemElement = $wait->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector(' div > section > div.itemCountHolder > div > div > span')
)
);
$quantityText = $foodItemElement->getText();
$quantityText = (int)$quantityText;
$newQuantityText='';
while ($quantityText < (int)$quantity) {
$plusButton = $driver->findElement(WebDriverBy::cssSelector('div > section > div.itemCountHolder > div > div > img.countActions.add.err-handled'));
$plusButton->click();
$newQuantityText = $foodItemElement->getText();
$newQuantityText = (int)$quantityText;
sleep(2);
if ($quantityText === $newQuantityText) {
break; // Quantity not updated, break out of the loop
}
$quantityText = $newQuantityText;
}
$foodItemInput->clear();
}
// Retrieve the cart value
$cartValue = $driver->findElement(WebDriverBy::cssSelector('#merchant-order-summary-react > div > div > div.orderOptions > div > button > div.amountBlock > span:nth-child(1) > span.finalPrice'))->getText();
$phno=$_POST["phno"];
$order=$db->query("select max(orderid) from Cart where Sid in(select Sid from user where PhoneNo='$phno');");
$sql ="update cart set mval='$cartValue' where sid='$order');";
$db->query($sql);
// $driver->quit();
return $cartValue;
}
// Read JSON data from file
$jsonData = file_get_contents('swiggy_cart_data.json');
$jsonData = json_decode($jsonData, true);
// Extract restaurant name
$restaurantName = getRestaurantName($jsonData);
$restaurantLocation = getRestaurantLocation($jsonData);
// Extract food items and quantities
$foodItemsAndQuantities = getFoodItemsAndQuantities($jsonData);
// Automate Magicpin cart process and get cart value
$magicpinCartValue = automateMagicpinCart($restaurantName, $restaurantLocation, $foodItemsAndQuantities);
echo "Magicpin Cart Value: " . $magicpinCartValue;
?>