-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from carlosrsabreu/feat/plot
wrt issue #9
- Loading branch information
Showing
8 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Publish history plot | ||
on: | ||
schedule: | ||
- cron: "0 0 1 * *" | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checks out the repository | ||
uses: actions/checkout@v2 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Setup Python | ||
uses: actions/setup-python@v3.1.2 | ||
- name: Install project requirements | ||
run: pip install -r requirements.txt | ||
- name: Post history plot | ||
env: | ||
CONSUMER_KEY: ${{ secrets.CONSUMER_KEY }} | ||
CONSUMER_SECRET: ${{ secrets.CONSUMER_SECRET }} | ||
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} | ||
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }} | ||
run: python script_post_plot_history.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
requests~=2.27.1 | ||
beautifulsoup4~=4.10.0 | ||
tweepy~=4.8.0 | ||
PyPDF2~=2.9.0 | ||
PyPDF2~=2.9.0 | ||
matplotlib~=3.5.3 | ||
pandas~=1.4.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import locale | ||
import tempfile | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib.ticker as mtick | ||
import pandas as pd | ||
|
||
from constants import TWEET_HISTORY, CURRENT_GAS_HISTORY_CSV_FILE, COLUMN_START_DATE, COLUMN_GASOLINA_IO95, COLUMN_GASOLINA_IO98, COLUMN_GASOLEO_RODOVIARIO, HISTORY_PLOT_LABEL_GASOLINA_IO95, HISTORY_PLOT_LABEL_GASOLINA_IO98, HISTORY_PLOT_LABEL_GASOLEO_RODOVIARIO, HISTORY_PLOT_Y_LABEL, HISTORY_PLOT_X_LABEL | ||
from post_tweet import post_image | ||
|
||
locale.setlocale(locale.LC_ALL, 'pt_PT.UTF-8') | ||
|
||
|
||
def generate_plot_history(plot_path): | ||
# Select the last 6 months of data | ||
history = pd.read_csv(CURRENT_GAS_HISTORY_CSV_FILE) | ||
history[COLUMN_START_DATE] = pd.to_datetime(history[COLUMN_START_DATE]) | ||
history.set_index(COLUMN_START_DATE, inplace=True) | ||
history = history.last('6M').copy() | ||
|
||
# Get start and end dates | ||
start_date, end_date = history.index.min(), history.index.max() | ||
|
||
# Generate plot | ||
plt.figure() | ||
plot = history.plot(y=[COLUMN_GASOLINA_IO95, COLUMN_GASOLINA_IO98, COLUMN_GASOLEO_RODOVIARIO], | ||
label=[HISTORY_PLOT_LABEL_GASOLINA_IO95, HISTORY_PLOT_LABEL_GASOLINA_IO98, HISTORY_PLOT_LABEL_GASOLEO_RODOVIARIO], ylabel=HISTORY_PLOT_Y_LABEL, xlabel=HISTORY_PLOT_X_LABEL) | ||
|
||
plot.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f')) | ||
|
||
# Save plot | ||
plt.savefig(plot_path, dpi=300, bbox_inches='tight') | ||
|
||
return start_date, end_date, plot | ||
|
||
|
||
# Generate history plot | ||
temp_dir = tempfile.TemporaryDirectory() | ||
plot_path = temp_dir.name + '/plot.png' | ||
start_date, end_date, plot = generate_plot_history(plot_path) | ||
|
||
# Post tweet with image | ||
post_image(TWEET_HISTORY.format(start_date=start_date, end_date=end_date), plot_path) |