Skip to content

Commit

Permalink
feat+ci: Added support for notifications via e-mail and Secrets Leaks…
Browse files Browse the repository at this point in the history
… for CI
  • Loading branch information
Vel-San committed Feb 15, 2024
1 parent 54d50c9 commit 3819f5c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/check_leaks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Secret Detection

on:
push:
branches:
- main

jobs:
scan-secrets:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Run TruffleHog OSS
uses: trufflesecurity/trufflehog@v3.67.6
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Configuration File Example](#configuration-file-example)
- [Notifications (E-mails)](#notifications-e-mails)
- [Outputs](#outputs)
- [Command-Line Interface](#command-line-interface)
- [Docker](#docker)
Expand Down Expand Up @@ -78,6 +79,18 @@ Alternatively, you can manually create the `configs/wbm_config.json` file with t
}
```

## Notifications (E-mails)

The bot will be able to send you e-mail notifications (from YOURSELF) once it applies for a flat.

To do so, you need to export `EMAIL_PASSWORD` to your environment variables. If this variable is not found, no e-mails will be sent.

**NOTE**: If your email has 2FA (MFA), you need to create an `App Password` from your Outlook online settings.

**NOTE**: Only `@outlook.com` emails are currently supported

> export EMAIL_PASSWORD=(password)
## Outputs

You will get outputs such as:
Expand Down Expand Up @@ -117,6 +130,9 @@ options:
### Run

If running for the first time, use `-it` to setup your config, if you already have the config ready in the correct directory, use `-d`

If you want to send emails as well to yourself as notifications, please add `-e "EMAIL_PASSWORD=<password>"` to the command

```bash
docker run -it \
-v /PATH_HERE/offline_viewings:/home/offline_viewings \
Expand Down
3 changes: 3 additions & 0 deletions wbmbot_v2/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
now = dt.datetime.now()
now = now.strftime("%Y-%m-%d_%H-%M")

# Retrieve email and password from environment variables
email_password = os.environ.get("EMAIL_PASSWORD")

# WBM Config File Name
wbm_config_name = f"{os.getcwd()}/configs/wbm_config.json"

Expand Down
54 changes: 54 additions & 0 deletions wbmbot_v2/helpers/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import yagmail
from helpers import constants
from logger import wbm_logger

__appname__ = os.path.splitext(os.path.basename(__file__))[0]
color_me = wbm_logger.ColoredLogger(__appname__)
LOG = color_me.create_logger()


def send_email_notification(
email: str, subject: str, body: str, attachment: str = None
):
"""Send email notification with optional attachment.
Args:
email (str): Sender's email address.
subject (str): Email subject.
body (str): Email body.
attachment (str, optional): Path to the attachment file. Defaults to None.
"""

if not constants.email_password:
LOG.warning(
color_me.yellow(
f"E-mail password not found in the ENV variables! I will not be able to send you e-mails."
)
)
return

try:
# Initialize yagmail SMTP connection
yag = yagmail.SMTP(
email,
constants.email_password,
smtp_starttls=True,
smtp_ssl=False,
smtp_skip_login=False,
host="smtp-mail.outlook.com",
port=587,
)

# Send email with attachment if provided
if attachment:
yag.send(to=email, subject=subject, contents=body, attachments=attachment)
else:
yag.send(to=email, subject=subject, contents=body)

LOG.info(color_me.green(f"Email notification sent successfully to '{email}'"))
except Exception as e:
LOG.error(
color_me.red(f"Failed to send email notification to '{email}' | {str(e)}")
)
19 changes: 14 additions & 5 deletions wbmbot_v2/helpers/webDriverOperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time

from handlers import flat
from helpers import constants
from helpers import constants, notifications
from httpsWrapper import httpPageDownloader as hpd
from logger import wbm_logger
from selenium.common.exceptions import (NoSuchElementException,
Expand Down Expand Up @@ -85,9 +85,10 @@ def download_expose_as_pdf(web_driver, flat_name: str):
# Log the href attribute of the found button
download_link = download_button.get_attribute("href")

hpd.download_pdf_file(
pdf_path = hpd.download_pdf_file(
download_link, f"{constants.offline_apartment_path}{constants.now}"
)
return pdf_path


def continue_btn(web_driver, flat_element):
Expand Down Expand Up @@ -338,17 +339,25 @@ def apply_to_flat(web_driver, flat_element, flat_title, user_profile, email):
"""Apply to the flat using the provided email."""

# Find and click "Ansehen" button on current flat
continue_btn(web_driver, flat_element)
flat_link = continue_btn(web_driver, flat_element)

# Fill out application form on current flat using info stored in user object
fill_form(web_driver, user_profile, email)

# Download as PDF
download_expose_as_pdf(web_driver, flat_title)
pdf_path = download_expose_as_pdf(web_driver, flat_title)

# Submit form
web_driver.find_element(By.XPATH, "//button[@type='submit']").click()

# Send e-mail
notifications.send_email_notification(
email,
f"[Applied] {flat_title}",
f"Appartment Link: {flat_link}\n\nYour Profile: {user_profile}",
pdf_path,
)


def process_flats(
web_driver,
Expand Down Expand Up @@ -412,7 +421,7 @@ def process_flats(
else:
LOG.info(
color_me.cyan(
f"Applying to flat: {flat_obj.title} for {email}"
f"Applying to flat: {flat_obj.title} for '{email}'"
)
)
apply_to_flat(
Expand Down
2 changes: 1 addition & 1 deletion wbmbot_v2/httpsWrapper/httpPageDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import warnings

import requests
from pywebcopy import save_webpage
from utility import io_operations

Expand Down Expand Up @@ -63,5 +62,6 @@ def download_pdf_file(url: str, local_dir: str) -> None:
if chunk:
pdf_file.write(chunk)

return file_path
except requests.exceptions.RequestException as e:
None
1 change: 1 addition & 0 deletions wbmbot_v2/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ colorama==0.4.6
pywebcopy==7.0.2
selenium==4.17.2
webdriver_manager==4.0.1
yagmail==0.15.293

0 comments on commit 3819f5c

Please sign in to comment.