Created
February 25, 2022 03:35
-
-
Save savithruml/87d90fb62b3e92665b2e39b83a1ab91c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Author: SAVITHRU LOKANATH | |
# Script to hunt masala dose [https://en.wikipedia.org/wiki/Masala_dosa] | |
# Slots open Thursday's at 07:00 PM PT, usually OOS in less than 3min | |
# https://suryadarshinicom.wixsite.com/sdhome | |
# Run this in a scheduler | |
# $ crontab -l | |
# 00 17 * * THU /usr/local/bin/python3 /opt/get_dose.py --wait | |
# 30 18 * * THU /usr/local/bin/python3 /opt/get_dose.py --cant-wait | |
# 59 18 * * THU /usr/local/bin/python3 /opt/get_dose.py --lets-do-it | |
# Butter fingers won't help, quickest wins! Good luck guru! | |
from sys import argv | |
from time import sleep, time | |
from bs4 import BeautifulSoup | |
from requests import get | |
from selenium import webdriver | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import WebDriverWait | |
LAPTOP_USERNAME = 'savi' | |
URL = 'https://suryadarshinicom.wixsite.com/sdhome' | |
WHATSAPP_GROUP = 'Masala Dose' | |
CHROME_OPTS = { | |
'profile_path': f'/Users/{LAPTOP_USERNAME}/Library/ApplicationSupport/Google/Chrome/Profile 2', | |
'exec_path': '/usr/local/bin/chromedriver' | |
} | |
class Dose(object): | |
def __init__(self, url: str, channel: str, chrome_opts: dict): | |
self.url = url | |
self.channel = channel | |
self.domain = 'square.site' | |
self.whatsapp_url = 'https://web.whatsapp.com/' | |
self.chrome_userprofile_path = chrome_opts.get('profile_path') | |
self.chrome_exec_path = chrome_opts.get('exec_path') | |
self.chrome_options = webdriver.ChromeOptions() | |
self.chrome_service = Service(self.chrome_exec_path) | |
self.chrome_options.add_argument(f'user-data-dir={self.chrome_userprofile_path}') | |
self.xpath_search_user = '//*[@id="side"]/div[1]/div/label/div/div[2]' | |
self.xpath_msg_box = '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]' | |
def remind(self, again: bool = False): | |
msg = '*### ATTN DOSE SEEKERS ###*\n\nHello, this is your ```Chutney``` bot!\n\nI will post the link to order Suryadarshini dose at exactly ```07:00 PM``` PT (2 hrs from now) today.\n\n*PROTIP:*\nFor the fastest checkout experience, save your credit card in your browser or pay using Square cash app at the payment page\n\nStay tuned,\n```Chutney``` bot' | |
if again: | |
msg = 'The wait is almost over, 30 more minutes to get the link\n\nUffff,\n```Chutney``` bot <3' | |
self._send_message(message=msg) | |
def get_link(self, interval: int = 1): | |
start = time() | |
search = True | |
print(f'Watch interval set to {interval}s') | |
while search: | |
response = get(self.url) | |
soup = BeautifulSoup(response.text, 'html.parser') | |
for link in soup.find_all('a'): | |
href = link.get('href') | |
end = time() | |
if self.domain in href: | |
search = False | |
break | |
if not search: | |
break | |
current = time() | |
print(f'Still looking for chutney..since {"%.2f"%(current-start)}s') | |
sleep(interval) | |
print(f'Chutney found..after {"%.2f"%(end-start)}s') | |
msg = f'The link has arrived!!!\n\n*DIRECT URL (Click this):*\n{href}\n\n*SITE URL (Backup):*\n{self.url}\n\nYours truly,\n```Chutney``` bot' | |
self._send_message(message=msg) | |
def _send_message(self, message: str, interval: int = 1, timeout: int = 300): | |
try: | |
chrome_driver = webdriver.Chrome(service=self.chrome_service, options=self.chrome_options) | |
chrome_driver.get(self.whatsapp_url) | |
wait = WebDriverWait(chrome_driver, timeout) | |
group_title = wait.until(EC.presence_of_element_located((By.XPATH, self.xpath_search_user))) | |
group_title.clear() | |
group_title.send_keys(self.channel) | |
group_title.send_keys(Keys.ENTER) | |
input_box = chrome_driver.find_element(By.XPATH, self.xpath_msg_box) | |
sleep(interval) | |
message = message.replace('\n', (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)) | |
input_box.send_keys(message + Keys.ENTER) | |
print(f'Message sent to {self.channel}') | |
sleep(interval) | |
except Exception as e: | |
print(e) | |
chrome_driver.close() | |
x = Dose(url=URL, channel=WHATSAPP_GROUP, chrome_opts=CHROME_OPTS) | |
arg = argv[1] | |
if len(argv) > 1: | |
if arg == '--wait': | |
x.remind() | |
if arg == '--cant-wait': | |
x.remind(again=True) | |
if arg == '--lets-do-it': | |
x.get_link() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment