This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdd.py
73 lines (64 loc) · 2.63 KB
/
dd.py
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
from urllib.parse import urlparse
from datetime import date
from time import sleep
from selenium.webdriver import Firefox
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.options import Options
import requests
class DoorDashOrder(Firefox):
def __init__(self):
options = Options()
options.headless = True
super().__init__(options=options)
self.logged_in = False
self.order_url = None
# NOTE: Wait up to one minute for DOM elements to load.
self.implicitly_wait(60)
def login(self, username: str, password: str) -> None:
try:
self.get("https://www.doordash.com/accounts/login")
self.find_element_by_xpath("//input[@id='email']").send_keys(username)
self.find_element_by_xpath("//input[@id='id_password']").send_keys(password)
self.find_element_by_xpath("//button[@type='submit']").click()
# TODO(jgaeb): Change to 60s for production.
sleep(10)
if self.current_url == "https://www.doordash.com/":
self.logged_in = True
else:
raise LoginError
except NoSuchElementException as e:
raise LoginError(f"{e!r}")
def group_order(self, restaurant_url: str) -> None:
if self.logged_in:
url = urlparse(restaurant_url)
if (url.netloc != 'www.doordash.com' or
not url.path.startswith("/store/") or
not requests.get(restaurant_url).ok):
raise NavigationError(restaurant_url)
self.get(restaurant_url)
try:
self.find_element_by_xpath(
"//button[@data-anchor-id='CreateGroupCartModalButton']"
).click()
self.find_element_by_xpath(
"//button//span[div='$15']"
).click()
submit_button = self.find_element_by_xpath(
"//button[@data-anchor-id='CreateGroupCartButton']"
).click()
return self.find_element_by_xpath(
"//input[@data-anchor-id='GroupCartLinkTextField']"
).get_attribute("value"), self.title.split(" Delivery")[0]
except:
raise GroupOrderFailedError(f"{e!r}")
else:
raise NotLoggedInError
################################## EXCEPTIONS ##################################
class LoginError(Exception):
pass
class NavigationError(Exception):
pass
class NotLoggedInError(Exception):
pass
class GroupOrderFailedError(Exception):
pass