Skip to content

Commit

Permalink
Merge pull request #1 from stivenramireza/release/0.0.1
Browse files Browse the repository at this point in the history
Release 0.0.1
  • Loading branch information
stivenramireza authored Mar 7, 2021
2 parents c99ea26 + faf6eaa commit f288df7
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DRIVER_PATH=<DRIVER_PATH>
OUTPUT_PATH=<OUTPUT_PATH>
WEBSITE_URL=<WEBSITE_URL>
IDENTIFICATION=<IDENTIFICATION>
PASSWORD=<PASSWORD>
HEADQUARTER_NAME=<HEADQUARTER_NAME>
DESIRED_TIME=<DESIRED_TIME>
WHATSAPP_URL=<WHATSAPP_URL>
CHAT_NAME=<CHAT_NAME>
PERSON_NAME=<PERSON_NAME>
CHROME_PROFILE_PATH=<CHROME_PROFILE_PATH>
11 changes: 11 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DRIVER_PATH=<DRIVER_PATH>
OUTPUT_PATH=<OUTPUT_PATH>
WEBSITE_URL=<WEBSITE_URL>
IDENTIFICATION=<IDENTIFICATION>
PASSWORD=<PASSWORD>
HEADQUARTER_NAME=<HEADQUARTER_NAME>
DESIRED_TIME=<DESIRED_TIME>
WHATSAPP_URL=<WHATSAPP_URL>
CHAT_NAME=<CHAT_NAME>
PERSON_NAME=<PERSON_NAME>
CHROME_PROFILE_PATH=<CHROME_PROFILE_PATH>
49 changes: 49 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Upload Python Package

on:
push:
branches:
- main
tags:
- 'v*'

jobs:
release:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@main
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v0.0.1
release_name: v0.0.1
body: |
First version
draft: false
prerelease: false

deploy:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ Website bot to book an hour in the Smart Fit gym and send QR code via WhatsApp a
<p align="center">
<img src="https://blog.smartfit.com.mx/wp-content/uploads/2020/10/gimnasio-smart-fit-mi-plan.png">
</p>

## Environment variables

Here it's important to know the way to fill the .env files:

- **DRIVER_PATH**: It means the directory where the chromedriver is located in the computer.
- **OUTPUT_PATH**: Directory where the QR code will be saved temporary before sending to WhatsApp.
- **WEBSITE_URL**: In this case is https://www.smartfitreserva.com/login.
- **IDENTIFICATION**: That's the field of your identification number.
- **PASSWORD**: That's the password you registered in Smart Fit previously.
- **HEADQUARTER_NAME**: It's the name of the headquarter, e.g san ignacio.
- **DESIRED_TIME**: It is in 24 hour format, e.g I want to book at 3:00 PM, I must write 15:00.
- **WHATSAPP_URL**: In this case is https://web.whatsapp.com/.
- **CHAT_NAME**: It's the name of chat that you want to receive the WhatsApp message.
- **PERSON_NAME**: It just indicates the person name who books an hour in the gym.
- **CHROME_PROFILE_PATH**: It means the directory where the chrome profile will be saved to avoid scan the WhatsApp QR Code next time you want to book an hour in the gym.

## How to run

$ pip install -r requirements.txt
$ export ENV=production
$ python main.py
1 change: 1 addition & 0 deletions __version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.0.1'
57 changes: 57 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from secrets import (
DRIVER_PATH,
OUTPUT_PATH,
WEBSITE_URL,
IDENTIFICATION,
PASSWORD,
HEADQUARTER_NAME,
DESIRED_TIME,
WHATSAPP_URL,
CHAT_NAME,
PERSON_NAME,
CHROME_PROFILE_PATH
)

from smartfit_booking.website_bot import (
initialize,
login_to_website,
answer_questionnaire,
search_headquarter,
book_hour,
get_qr_code,
login_to_whatsapp,
search_chat,
send_message
)

from smartfit_booking.storage import (
save_file,
remove_file
)

from smartfit_booking.data_access_api import get_data

import os

def main():
""" book_an_hour """
driver = initialize(DRIVER_PATH, OUTPUT_PATH, CHROME_PROFILE_PATH)
login_to_website(driver, WEBSITE_URL, IDENTIFICATION, PASSWORD)
answer_questionnaire(driver)
search_headquarter(driver, HEADQUARTER_NAME)
book_hour(driver, DESIRED_TIME)
qr_code_url = get_qr_code(driver)

""" download_qr_code """
response = get_data(qr_code_url)
image_path = os.path.join(OUTPUT_PATH, 'code.jpg')
save_file(image_path, response)

""" send_whatsapp_message """
login_to_whatsapp(driver, WHATSAPP_URL)
search_chat(driver, CHAT_NAME)
send_message(driver, image_path, PERSON_NAME, DESIRED_TIME)
remove_file(image_path)

if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
selenium==3.141.0
requests==2.25.1
python-dotenv==0.15.0
32 changes: 32 additions & 0 deletions secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from dotenv import load_dotenv

from smartfit_booking.logger import logger

import os

ENV = os.environ.get('ENV')
if ENV == 'production':
dotenv_path = '.env'
logger.info('Using production environment variables')
else:
dotenv_path = '.env.dev'
logger.info('Using development environment variables')

exists = os.path.exists(dotenv_path)

if not exists:
raise Exception('env files do not exist')

load_dotenv(dotenv_path)

DRIVER_PATH = os.environ.get('DRIVER_PATH')
OUTPUT_PATH = os.environ.get('OUTPUT_PATH')
WEBSITE_URL = os.environ.get('WEBSITE_URL')
IDENTIFICATION = os.environ.get('IDENTIFICATION')
PASSWORD = os.environ.get('PASSWORD')
HEADQUARTER_NAME = os.environ.get('HEADQUARTER_NAME')
DESIRED_TIME = os.environ.get('DESIRED_TIME')
WHATSAPP_URL = os.environ.get('WHATSAPP_URL')
CHAT_NAME = os.environ.get('CHAT_NAME')
PERSON_NAME = os.environ.get('PERSON_NAME')
CHROME_PROFILE_PATH = os.environ.get('CHROME_PROFILE_PATH')
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __version__ import __version__

import setuptools

with open('requirements.txt') as f:
required = f.read().splitlines()

setuptools.setup(
name="smartfit_booking",
version=__version__,
author="Stiven Ramírez Arango",
author_email="[email protected]",
description="Smart Fit gym booking package",
packages=setuptools.find_packages(),
python_requires=">=3.6",
install_requires=required
)
9 changes: 9 additions & 0 deletions smartfit_booking/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from . import data_access_api, formatter, logger, storage, website_bot

__all__ = [
'data_access_api',
'formatter',
'logger',
'storage',
'website_bot'
]
13 changes: 13 additions & 0 deletions smartfit_booking/data_access_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import requests

from smartfit_booking.logger import logger

def get_data(url: str) -> object:
try:
response = requests.get(url)
if response.status_code == 200:
logger.info(f'Data has been gotten successfully')
return response
except Exception as error:
logger.error(f'Error to get data from url {url}: {error}')
raise
13 changes: 13 additions & 0 deletions smartfit_booking/formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from datetime import datetime

import time

def get_current_date() -> str:
return datetime.now().strftime("%d/%m/%Y")

def search_current_date() -> str:
return datetime.now().strftime("%d%m%Y")

def convert_24_to_12_hour(time_value: str) -> str:
t = time.strptime(time_value, "%H:%M")
return time.strftime("%I:%M %p", t)
3 changes: 3 additions & 0 deletions smartfit_booking/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging as logger

logger.basicConfig(level=logger.INFO)
22 changes: 22 additions & 0 deletions smartfit_booking/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from smartfit_booking.logger import logger

def save_file(path: str, response: object) -> None:
try:
file = open(path, "wb")
file.write(response.content)
file.close()
logger.info(f'File {path} has been saved successfully')
except Exception as error:
logger.error(f'Error to save image: {error}')
raise

def remove_file(path: str) -> None:
try:
if os.path.exists(path):
os.remove(path)
logger.info(f'File {path} has been removed successfully')
except Exception as error:
logger.error(f'Error to remove file {path}: {error}')
raise
Loading

0 comments on commit f288df7

Please sign in to comment.