Skip to content

Commit

Permalink
Merge branches 'feat/amazon' and 'master' of https://github.com/Hari-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hari-Nagarajan committed Sep 25, 2020
3 parents 9020bbb + e8c4c12 + 260075b commit 2fd7acc
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ valid, the notification handler will send you an sms when it carts or purchases
#### Discord
To enable Discord notifications, first get your wehbook url. Use the directions [here](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) to get the webhook url.
Make a copy of the `discord_config.template_json` file and name it `discord_config.json` and place the webhook url here.
Optionally a [user id](https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-) can be added to ping someone (like yourself).
```
{
"webhook_url": "Discord webhook url here"
"webhook_url": "Discord webhook url here",
"user_id": "Optional user id to ping here"
}
```

Expand Down
3 changes: 2 additions & 1 deletion discord_config.template_json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"webhook_url": "Discord webhook url here"
"webhook_url": "Discord webhook url here",
"user_id": "Optional user id to ping here"
}
29 changes: 17 additions & 12 deletions notifications/notifications.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from concurrent.futures import ThreadPoolExecutor

from notifications.providers.audio import AudioHandler
from notifications.providers.discord import DiscordHandler
from notifications.providers.pavlok import PavlokHandler
Expand Down Expand Up @@ -35,15 +37,18 @@ def get_enabled_handlers(self):
return enabled_handlers

def send_notification(self, message):
if self.audio_handler.enabled:
self.audio_handler.play()
if self.twilio_handler.enabled:
self.twilio_handler.send(message)
if self.discord_handler.enabled:
self.discord_handler.send(message)
if self.telegram_handler.enabled:
self.telegram_handler.send(message)
if self.slack_handler.enabled:
self.slack_handler.send(message)
if self.pavlok_handler.enabled:
self.pavlok_handler.zap()
with ThreadPoolExecutor(
max_workers=len(self.get_enabled_handlers())
) as executor:
if self.audio_handler.enabled:
executor.submit(self.audio_handler.play)
if self.twilio_handler.enabled:
executor.submit(self.twilio_handler.send, message)
if self.discord_handler.enabled:
executor.submit(self.discord_handler.send, message)
if self.telegram_handler.enabled:
executor.submit(self.telegram_handler.send, message)
if self.slack_handler.enabled:
executor.submit(self.slack_handler.send, message)
if self.pavlok_handler.enabled:
executor.submit(self.pavlok_handler.zap)
8 changes: 7 additions & 1 deletion notifications/providers/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ def __init__(self):
self.config = json.load(json_file)
if self.config["webhook_url"]:
self.webhook_url = self.config["webhook_url"]
self.user_id = self.config.get("user_id", "N/A")
self.enabled = True
else:
log.debug("No Discord creds found.")

def send(self, message_body):
try:
web_hook = DiscordWebhook(url=self.webhook_url, content=message_body)
message = (
f"<@{self.user_id}> {message_body}"
if self.user_id.isdigit()
else message_body
)
web_hook = DiscordWebhook(url=self.webhook_url, content=message)
response = web_hook.execute()
log.info(f"Discord hook status: {response.status_code}")
except Exception as e:
Expand Down
13 changes: 5 additions & 8 deletions notifications/providers/telegram.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from os import path
from urllib.parse import quote

import requests

Expand All @@ -21,22 +22,18 @@ def __init__(self):
if self.config["BOT_TOKEN"] and self.config["BOT_CHAT_ID"]:
self.bot_token = self.config["BOT_TOKEN"]
self.bot_chat_id = self.config["BOT_CHAT_ID"]
if not isinstance(self.bot_chat_id, list):
self.bot_chat_id = [self.bot_chat_id]
self.enabled = True
else:
log.debug("No Telegram config found.")

def send(self, message_body):
try:
if type(self.bot_chat_id) is list:
for chat_id in self.bot_chat_id:
requests.get(
f"https://api.telegram.org/bot{self.bot_token}/sendMessage?"
f"chat_id={chat_id}&parse_mode=Markdown&text={message_body}"
)
else:
for chat_id in self.bot_chat_id:
requests.get(
f"https://api.telegram.org/bot{self.bot_token}/sendMessage?"
f"chat_id={self.bot_chat_id}&parse_mode=Markdown&text={message_body}"
f"chat_id={chat_id}&text={quote(message_body)}"
)
except Exception as e:
log.error(e)
Expand Down
2 changes: 1 addition & 1 deletion stores/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, headless=False):
else:
log.info("Lets log in.")
selenium_utils.button_click_using_xpath(self.driver, '//*[@id="nav-link-accountList"]/div/span')
selenium_utils.wait_for_page(self.driver, "Amazon Sign In")
selenium_utils.wait_for_any_title(self.driver, SIGN_IN_TITLES)
self.login()
log.info("Waiting 15 seconds.")
time.sleep(15) # We can remove this once I get more info on the phone verification page.
Expand Down
10 changes: 5 additions & 5 deletions stores/nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ def run_items(self):
def buy(self, product_id):
pass
try:
log.info(
f"Stock Check {product_id} at {self.interval} second intervals."
)
log.info(f"Stock Check {product_id} at {self.interval} second intervals.")
while not self.is_in_stock(product_id):
self.attempt = self.attempt + 1
time_delta = str(datetime.now() - self.started_at).split(".")[0]
Expand Down Expand Up @@ -296,7 +294,9 @@ def get_cart_url(self, product_id):
return True, response_json["location"]
else:
log.error(response.text)
log.error(f"Add to cart failed with {response.status_code}. This is likely an error with nvidia's API.")
log.error(
f"Add to cart failed with {response.status_code}. This is likely an error with nvidia's API."
)
return False, ""

def get_session_token(self):
Expand All @@ -314,4 +314,4 @@ def get_session_token(self):
return False, ""
return True, response_json["session_token"]
else:
log.debug(f"Get Session Token: {response.status_code}")
log.debug(f"Get Session Token: {response.status_code}")

0 comments on commit 2fd7acc

Please sign in to comment.