Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from viniciusgava/feature/mail-gun-pushbullet-s…
Browse files Browse the repository at this point in the history
…upport

Mail Gun, Push Bullet Support. Invalid username and password validate
  • Loading branch information
viniciusgava authored Jul 7, 2018
2 parents 27b190b + 4d9ab37 commit 139df63
Show file tree
Hide file tree
Showing 21 changed files with 635 additions and 148 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,4 @@ venv.bak/
downloads/

# Sensitive information
src/local/credentials.py
src/local/settings.py
src/settings/local.py
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ RUN sudo apt-get update && sudo apt-get install -y --no-install-recommends \
python3-pip \
&& sudo rm -rf /var/lib/apt/lists/*

RUN pip3 install selenium
RUN pip3 install selenium requests

COPY src/ /usr/workspace

RUN sudo chown -R seluser:seluser /usr/workspace

WORKDIR /usr/workspace

CMD ["python3", "/usr/workspace/app-docker.py"]
CMD ["python3", "/usr/workspace/app.py", "docker"]
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ publish-image:
docker push viniciusgava/portaldorh-holerite-download:latest

cleanup:
rm -rf src/local/credentials.py
rm -rf src/local/settings.py
rm -rf src/settings/local.py
find . -name "__pycache__" -exec rm -rf "{}" \;
find . -name "*.pyc" -exec rm -rf "{}" \;

validate-cleanup:
ls -la src/local/credentials.py
ls -la src/local/settings.py
ls -la src/settings/local.py
find . -name "__pycache__"
find . -name "*.pyc"

prepare-local:
cp src/local/credentials.py.dist src/local/credentials.py
cp src/local/settings.py.dist src/local/settings.py
pip3 install selenium
cp src/settings/local.py.dist src/settings/local.py
pip3 install selenium requests
215 changes: 197 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,218 @@
# Portal do RH Download
Download holerite PDF using selenium, chrome and python at Portal do RH
Download holerite PDF using selenium, chrome and python at Portal do RH.

## Additional Integrations
- E-mail with downloaded PDF attachment by Mail Gun
- Execution notification by Push Bullet

## Usage - Docker

```
docker run -v $(pwd):/usr/workspace/downloads \
-e RH_USERNAME='YOUR USER NAME HERE' \
-e RH_PASSWORD='YOUR PASSWORD' \
-e RH_SEARCHDATE='03/2018' \
-e RH_USERNAME='YOUR USERNAME HERE' \
-e RH_PASSWORD='YOUR PASSWORD HERE' \
-e RH_SEARCH_YEAR='2018' \
-e RH_SEARCH_MONTH='03' \
viniciusgava/portaldorh-holerite-download:latest
```
The above command will save pdf in ``$(pwd)``, it means the directory where you run it.
File name is the search date, ``MM-YYYY.pdf``.

**eg:**
File name is the search year + search month, ``YYYY-MM.pdf``.

**example:**

``03-2018.pdf``
``2018-03.pdf``

### Container ENV variables
### Download feature - ENV variables

#### RH_SEARCH_YEAR
Search Year

**Required:** Yes

#### RH_SEARCH_MONTH
Search Month

**Required:** Yes

### RH_USERNAME
Your username to login

**Required:** Yes

### RH_PASSWORD
Your password to login

**Required:** Yes

#### RH_URL
Portal do RH URL

**Required:** No

**Default:** https://www.portaldorh.com.br/portal_rckt/Auto_Principal.aspx

### DOWNLOAD_PATH
Where files will be saved.

**Required:** No

**Default:** /usr/workspace/downloads

### Mail Gun feature - ENV variables
All Required fields bellow are required **only** if ``MAIL_GUN_ENABLE`` env setted as ``true``

#### MAIL_GUN_ENABLE
Integrate with mail gun? true or false

**Required:** No

**Default:** false

#### MAIL_GUN_KEY
Mail Gun API Key

**Required:** Yes

#### MAIL_GUN_DOMAIN
Mail Gun Domain

**Required:** Yes

#### MAIL_GUN_FROM
Mail origin

**Required:** Yes

**Example:**: Name <name@mail.com>

#### MAIL_GUN_TO
Mail destination

**Required:** Yes

**Example:**: Name <name@mail.com>

#### MAIL_GUN_SUBJECT
Mail subject

**Required:** Yes

| ENV VARIABLE | REQUIRED | DESCRIPTION | DEFAULT VALUE |
| ---------------- |:--------:|----------------------------|---------------------------------------------------------------|
| RH_SEARCHDATE | yes | Search date. eg(03/2018) | |
| RH_URL | no | Portal do RH login page | https://www.portaldorh.com.br/portal_rckt/Auto_Principal.aspx |
| DOWNLOAD_PATH | no | Where files will be saved. | /usr/workspace/downloads |
| RH_USERNAME | yes | Your username to login | |
| RH_PASSWORD | yes | Your password to login | |
**Placeholder Available:** Yes

# Usage - Local
#### MAIL_GUN_TEXT
Mail content as text

**Required:** Yes if ``MAIL_GUN_HTML`` is not setted

**Placeholder Available:** Yes

#### MAIL_GUN_HTML
Mail content as text

**Required:** Yes if ``MAIL_GUN_TEXT`` is not setted

**Placeholder Available:** Yes

### Push Bullet feature - ENV variables
All Required fields bellow are required **only** if ``PUSH_BULLET_ENABLE`` env setted as ``true``

#### PUSH_BULLET_ENABLE
Integrate with push bullet? true or false

**Required:** No

**Default:** false

#### PUSH_BULLET_TOKEN
Push Bullet API Token

**Required:** Yes

#### PUSH_BULLET_TITLE
Notification Title

**Required:** Yes

**Placeholder Available:** Yes

#### PUSH_BULLET_BODY
Notification Body

**Required:** Yes

**Placeholder Available:** Yes

## Usage - Local
Makefile and instruction bellow expected you uses python3.

It also expected you already have a chrome webdrive installed.

1. Clone repository
2. Run ``make prepare-local``
3. Edit both files ``src/local/credentials.py``and ``src/local/settings.py`` with your information.
4. Run ``python3 src/app-local.py``
3. Edit ``src/settings/local.py`` file with your information.
4. Run ``python3 src/app.py local`

## Integrations Placeholder
Some integration fields accept placeholder, that means you can use internal fields used on integration on your texts.

Fields that accept placeholders are marked on **Env Variables** of each integration.

### Placeholder syntax
``%(placeholderName)s``
Example:
``This is my holerite at %(search_year)s/%(search_month)s``

### Available placeholders
- search_year
- search_month

## Crontab

First create bash like this:
````bash
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

# print current date at log
date

# GET LAST MONTH YEAR AND MONTH
LAST_MONTH_YEAR=$(perl -MPOSIX=strftime -le '@t = localtime; $t[3] = 1; $t[5]--; print strftime("%Y", @t)')
LAST_MONTH_MONTH=$(perl -MPOSIX=strftime -le '@t = localtime; $t[3] = 1; $t[4]--; print strftime("%m", @t)')

# try 5 times
n=0
until [ $n -ge 5 ]
do
echo "trying $n"
/usr/local/bin/docker run --env-file "$SCRIPTPATH/env-configs" \
-e RH_SEARCH_YEAR="$LAST_MONTH_YEAR" \
-e RH_SEARCH_MONTH="$LAST_MONTH_MONTH" \
viniciusgava/portaldorh-holerite-download:latest 2>&1 && break
n=$[$n+1]
sleep 15
done

````
**Mac tip:** You must to pass docker full path to works at crontab
``/usr/local/bin/docker``

Second add all env variables at ``env-configs``.
Example:
````bash
RH_USERNAME='YOUR USERNAME HERE'
RH_PASSWORD='YOUR PASSWORD HERE'
````

Then run ``crontab -e`` and add the follow cron.
Example:
````bash
0 8 7 * * sh ~/automate/holerite/run.sh >> ~/automate/holerite/holerite.log
````
The example bellow runs 8am of day 7 of every month.

You can generate a different crontab config on [https://crontab-generator.org](https://crontab-generator.org)

## Links
- [GitHub](https://github.com/viniciusgava/portaldorh-holerite-download)
Expand Down
9 changes: 0 additions & 9 deletions src/app-docker.py

This file was deleted.

6 changes: 0 additions & 6 deletions src/app-local.py

This file was deleted.

41 changes: 41 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import argparse
import logging
import sys
import time

from automate.downloader import Downloader
from integration.mailgun import Mailgun
from integration.pushBulletNotification import PushBulletNotification

parser = argparse.ArgumentParser("app")
parser.add_argument('env', help='local or docker env', default='local')

args = parser.parse_args()
env = args.env

if env == 'docker':
import settings.docker as settings
else:
import settings.local as settings

# Configure logger
logger = logging.getLogger('app')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

# Sleep
time.sleep(1)

downloader = Downloader(settings, logger)
result = downloader.run()

if result:
mailgun = Mailgun(settings, logger)
mailgun.send()

push_bullet = PushBulletNotification(settings, logger)
push_bullet.push()
File renamed without changes.
Loading

0 comments on commit 139df63

Please sign in to comment.