Skip to content

Commit

Permalink
Merge pull request #406 from Hari-Nagarajan/development
Browse files Browse the repository at this point in the history
Merging all the things for 0.4.4
  • Loading branch information
Worst Mod Confirmed authored Dec 9, 2020
2 parents 09a333b + 538f396 commit 84441cf
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 593 deletions.
30 changes: 16 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
dist/
*.code-workspace
*.html
*.pkl
*.png
*.py[cod]
*_config.json
*_credentials.json
.github
.idea/
build/
Pipfile.lock
geckodriver.log
.profile
.profile-amz*
*_config.json
*_credentials.json
*.log*
__pycache__/
*.py[cod]
*.pkl
.vscode/
*.png
*.html
/config/*_credentials.json
/config/apprise.conf
/config/apprise_config.json
screenshots/*.png
Pipfile.lock
__pycache__/
build/
dist/
geckodriver.log
html_saves/*.html
*.code-workspace
screenshots/*.png
logs/*.log*
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ verify_ssl = true
pyinstaller = "*"

[packages]
requests = "*"
requests = "==2.24.0"
click = "*"
selenium = "*"
chromedriver-py = "==87.0.4280.20"
Expand All @@ -29,6 +29,8 @@ coloredlogs = "*"
apprise = "*"
price-parser = "*"
pypresence = "==4.0.0"
pywin32 = {version = "*", sys_platform = "== 'win32'"}

stdiomask = "*"

[requires]
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ Commands:
--test : This will not finish the checkout
--delay : modify default delay between page refreshes (3 seconds), use --delay=x, where is is time in seconds (accepts decimals)
--checkshipping : Bot will consider shipping + sales price in reserve check. Without this flag, only free shipping items will be considered
--detailed : Take more screenshots. !!!!!! This could cause you to miss checkouts !!!!!!
--detailed : Take more screenshots.
--used : Show used items in search listings
--random-delay : Set delay to a random interval
--single-shot : Quit after 1 successful purchase
--no-screenshots : Do not take screenshots
--disable-presence : Disable Discord Rich Presence functionallity
--no-screenshots : Disable screenshots from being taken
```

Make a copy of `amazon_config.template_json` and rename to `amazon_config.json`:
Expand Down Expand Up @@ -161,28 +162,27 @@ python app.py bestbuy --sku 6429440


### Notifications
Notifications are now handled by apprise. Apprise lets you send notifications to a large number of supported notification services.
Notifications are now handled by Apprise. Apprise lets you send notifications to a large number of supported notification services.
Check https://github.com/caronc/apprise/wiki for a detailed list.

To enable Apprise notifications, make a copy of `apprise_config.template_json` in the `config` directory and name it `apprise_config.json`.
Then add apprise formatted urls for your desired notification services as json blobs.
To enable Apprise notifications, make a copy of `apprise.conf_template` in the `config` directory and name it
`apprise.conf`. Then add apprise formatted urls for your desired notification services as simple text entries
in the config file. Any recognized notification services will be reported on app start.

Apprise Example Config:
```
# Hash Tags denote comment lines and blank lines are allowed
# Discord (https://github.com/caronc/apprise/wiki/Notify_discord)
https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
# Telegram
tgram://{bot_token}/{chat_id}/
# Slack (https://github.com/caronc/apprise/wiki/Notify_slack)
https://hooks.slack.com/services/{tokenA}/{tokenB}/{tokenC}
Apprise Example blobs:
```json
[
{
"url": "tgram://{bot_token}/{chat_id}"
},
{
"url": "twilio://{AccountSID}:{AuthToken}@{FromPhoneNo}/{PhoneNo}"
},
{
"url": "slack://{OAuthToken}/#{channel}"
},
{
"url": "{COPY AND PASTE DISCORD WEBHOOK HERE}"
}
]
```

#### Pavlok
Expand Down
27 changes: 15 additions & 12 deletions cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

import click

from cli.utils import QuestionaryOption
from notifications.notifications import NotificationHandler, TIME_FORMAT
from stores.amazon import Amazon
from stores.bestbuy import BestBuyHandler
from stores.nvidia import NvidiaBuyer, GPU_DISPLAY_NAMES, CURRENCY_LOCALE_MAP
from utils import selenium_utils
from utils.logger import log
from utils.discord_presence import start_presence
from utils.version import check_version

notification_handler = NotificationHandler()

try:
check_version()
except Exception as e:
log.error(e)


def handler(signal, frame):
log.info("Caught the stop, exiting.")
Expand Down Expand Up @@ -100,6 +103,11 @@ def main():
is_flag=True,
help="Take NO screenshots, do not bother asking for help if you use this... Screenshots are the best tool we have for troubleshooting",
)
@click.option(
"--disable-presence",
is_flag=True,
help="Disable Discord Rich Presence functionallity",
)
@notify_on_crash
def amazon(
no_image,
Expand All @@ -112,6 +120,7 @@ def amazon(
random_delay,
single_shot,
no_screenshots,
disable_presence,
):
if no_image:
selenium_utils.no_amazon_image()
Expand All @@ -127,6 +136,7 @@ def amazon(
used=used,
single_shot=single_shot,
no_screenshots=no_screenshots,
disable_presence=disable_presence,
)
amzn_obj.run(delay=delay, test=test)

Expand All @@ -144,23 +154,16 @@ def bestbuy(sku, headless):

@click.command()
def test_notifications():
enabled_handlers = ", ".join(notification_handler.get_enabled_handlers())
enabled_handlers = ", ".join(notification_handler.enabled_handlers)
time = datetime.now().strftime(TIME_FORMAT)
notification_handler.send_notification(
f"Beep boop. This is a test notification from Nvidia bot. Sent {time}."
f"Beep boop. This is a test notification from FairGame. Sent {time}."
)
log.info(f"A notification was sent to the following handlers: {enabled_handlers}")


signal(SIGINT, handler)

try:
status = "Spinning up"
start_presence(status)
except:
pass

# main.add_command(nvidia)
main.add_command(amazon)
main.add_command(bestbuy)
main.add_command(test_notifications)
37 changes: 37 additions & 0 deletions config/apprise.conf_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copy this file to a new file named apprise.conf in the same directory and edit accordingly
#
# Apprise TEXT based configuration file for simplicity.
#
# Specification can be found here:
# https://github.com/caronc/apprise/wiki/config_text
#
# Notification Providers configurations can be found here:
# https://github.com/caronc/apprise/wiki

# Example Configurations:

# Discord (https://github.com/caronc/apprise/wiki/Notify_discord)
#
# https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
# OR
# discord://{WebhookID}/{WebhookToken}/


# Telegram

# tgram://{bot_token}/{chat_id}/


# Slack (https://github.com/caronc/apprise/wiki/Notify_slack)

# https://hooks.slack.com/services/{tokenA}/{tokenB}/{tokenC}
# OR
# slack://{tokenA}/{tokenB}/{tokenC}


# Local OS Notification - NOTE: May require local installation of apprise (pip install apprise)
# qt://
# gnome://
# kde://
# macosx://
# windows://
14 changes: 0 additions & 14 deletions config/apprise_config.template_json

This file was deleted.

120 changes: 14 additions & 106 deletions notifications/notifications.py
Original file line number Diff line number Diff line change
@@ -1,134 +1,42 @@
import json
import queue
import threading
from concurrent.futures import ThreadPoolExecutor
from os import path

import apprise

from notifications.providers.audio import AudioHandler
from notifications.providers.discord import DiscordHandler
from notifications.providers.join import JoinHandler
from notifications.providers.slack import SlackHandler
from notifications.providers.telegram import TelegramHandler
from notifications.providers.twilio import TwilioHandler
from utils.logger import log

TIME_FORMAT = "%Y-%m-%d @ %H:%M:%S"


APPRISE_CONFIG_PATH = "config/apprise_config.json"
APPRISE_CONFIG_PATH = "config/apprise.conf"


class NotificationHandler:
def __init__(self):
log.info("Initializing Apprise handler")
self.apb = apprise.Apprise()
enabled_handlers = []

def __init__(self):
if path.exists(APPRISE_CONFIG_PATH):
with open(APPRISE_CONFIG_PATH) as json_file:
configs = json.load(json_file)
for config in configs:
self.apb.add(config["url"])
log.info(f"Initializing Apprise handler using: {APPRISE_CONFIG_PATH}")
self.apb = apprise.Apprise()
config = apprise.AppriseConfig()
config.add(APPRISE_CONFIG_PATH)
# Get the service names from the config, not the Apprise instance when reading from config file
for server in config.servers():
log.info(f"Found {server.service_name} configuration")
self.enabled_handlers.append(server.service_name)
self.apb.add(config)
self.queue = queue.Queue()
self.start_worker()
self.enabled = True
else:
self.enabled = False
log.debug("No Apprise config found.")

log.info("Initializing other notification handlers")
self.audio_handler = AudioHandler()
self.twilio_handler = TwilioHandler() # Deprecate soon
self.discord_handler = DiscordHandler() # Deprecate soon
self.join_handler = JoinHandler() # Deprecate soon
self.telegram_handler = TelegramHandler() # Deprecate soon
self.slack_handler = SlackHandler() # Deprecate soon

deprecation_message = "The standalone {notification} handler will be deprecated soon, please delete your {notification}_config.json and add the equivalent apprise url: '{apprise_url}' to 'config/apprise_config.json'"
if self.slack_handler.enabled:
slack_apprise_url = self.slack_handler.generate_apprise_url()
log.warning(
deprecation_message.format(
notification="slack", apprise_url=slack_apprise_url
)
)
self.apb.add(slack_apprise_url)

if self.twilio_handler.enabled:
twilio_apprise_url = self.twilio_handler.generate_apprise_url()
log.warning(
deprecation_message.format(
notification="twilio", apprise_url=twilio_apprise_url
)
)
self.apb.add(twilio_apprise_url)

if self.telegram_handler.enabled:
telegram_apprise_url = self.telegram_handler.generate_apprise_url()
log.warning(
deprecation_message.format(
notification="telegram", apprise_url=telegram_apprise_url
)
)
self.apb.add(telegram_apprise_url)

if self.join_handler.enabled:
join_apprise_url = self.join_handler.generate_apprise_url()
log.warning(
deprecation_message.format(
notification="join", apprise_url=join_apprise_url
)
)
self.apb.add(join_apprise_url)

if self.discord_handler.enabled:
discord_apprise_url = self.discord_handler.generate_apprise_url()
log.warning(
deprecation_message.format(
notification="discord", apprise_url=discord_apprise_url
)
)
self.apb.add(discord_apprise_url)

enabled_handlers = self.get_enabled_handlers()
log.info(f"Enabled Handlers: {enabled_handlers}")
if len(enabled_handlers) > 0:
self.executor = ThreadPoolExecutor(max_workers=len(enabled_handlers))

def get_enabled_handlers(self):
enabled_handlers = []
if self.audio_handler.enabled:
enabled_handlers.append("Audio")
if self.twilio_handler.enabled:
enabled_handlers.append("Twilio")
if self.discord_handler.enabled:
enabled_handlers.append("Discord")
if self.join_handler.enabled:
enabled_handlers.append("Join")
if self.telegram_handler.enabled:
enabled_handlers.append("Telegram")
if self.slack_handler.enabled:
enabled_handlers.append("Slack")
return enabled_handlers
log.info(f"No Apprise config found at {APPRISE_CONFIG_PATH}.")
log.info(f"For notifications, see {APPRISE_CONFIG_PATH}_template")

def send_notification(self, message, ss_name=[], **kwargs):
if self.enabled:
self.queue.put((message, ss_name))

if self.audio_handler.enabled:
self.executor.submit(self.audio_handler.play, **kwargs)
if self.twilio_handler.enabled:
self.executor.submit(self.twilio_handler.send, message)
if self.discord_handler.enabled:
self.executor.submit(self.discord_handler.send, message)
if self.join_handler.enabled:
self.executor.submit(self.join_handler.send, message)
if self.telegram_handler.enabled:
self.executor.submit(self.telegram_handler.send, message)
if self.slack_handler.enabled:
self.executor.submit(self.slack_handler.send, message)

def message_sender(self):
while True:
message, ss_name = self.queue.get()
Expand Down
Empty file.
Loading

0 comments on commit 84441cf

Please sign in to comment.