Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Publish monthly a plot on Twitter with the gas price variation #9 #11

Merged
merged 14 commits into from
Jan 11, 2023
23 changes: 23 additions & 0 deletions .github/workflows/history_plot.yml
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ jobs:
git add gas_info.json
git add history/gas_info_history.csv
git add history/gas_info_history.json
git add history/img/plot.png
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed since you are not writing the file on the repo any more.

git diff-index --quiet HEAD || git commit -m "Update gas information"
git push
6 changes: 3 additions & 3 deletions add_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
def add_history(dict_prices):
# JSON
with open(CURRENT_GAS_HISTORY_JSON_FILE, 'r') as f:
curret_data = json.load(f)
current_data = json.load(f)
with open(CURRENT_GAS_HISTORY_JSON_FILE, 'w') as f:
curret_data[dict_prices[CURRENT_WEEK][START_DATE_KEY]] = dict_prices[CURRENT_WEEK]
content = json.dumps(curret_data, indent=1, ensure_ascii=False)
current_data[dict_prices[CURRENT_WEEK][START_DATE_KEY]] = dict_prices[CURRENT_WEEK]
content = json.dumps(current_data, indent=1, ensure_ascii=False)
f.write(content)
# CSV
with open(CURRENT_GAS_HISTORY_CSV_FILE, 'a') as f:
Expand Down
18 changes: 18 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
CURRENT_GAS_INFO_FILE = 'gas_info.json'
CURRENT_GAS_HISTORY_JSON_FILE = 'history/gas_info_history.json'
CURRENT_GAS_HISTORY_CSV_FILE = 'history/gas_info_history.csv'
CURRENT_GAS_HISTORY_PLOT = 'history/img/plot.png'
# Gas
DIESEL = 'Gasóleo Rodoviário'
GASOLINE_95 = 'Gasolina IO95'
Expand All @@ -33,3 +34,20 @@
'Gasóleo rodoviário',
'Gasóleo colorido e marcado'
)

# History plot
HISTORY_PLOT_LABEL_GASOLINA_IO95 = 'Gasolina IO95'
HISTORY_PLOT_LABEL_GASOLINA_IO98 = 'Gasolina IO98'
HISTORY_PLOT_LABEL_GASOLEO_RODOVIARIO = 'Gasoleo Rodoviario'
HISTORY_PLOT_Y_LABEL = 'Preço (€)'
HISTORY_PLOT_X_LABEL = 'Data'

# CSV columns
COLUMN_START_DATE = 'start_date'
COLUMN_END_DATE = 'end_date'
COLUMN_GASOLINA_IO95 = 'gasolina_IO95'
COLUMN_GASOLINA_IO98 = 'gasolina_IO98'
COLUMN_GASOLEO_RODOVIARIO = 'gasoleo_rodoviario'

# Tweets
TWEET_HISTORY = 'Variação dos preços dos combustíveis na Madeira, de {start_date} a {end_date}.'
3 changes: 3 additions & 0 deletions history/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# History

![History plot](img/plot.png)
26 changes: 26 additions & 0 deletions history/generate_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import locale

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import pandas as pd

from constants import 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

locale.setlocale(locale.LC_ALL, 'pt_PT.UTF-8')


def generate_plot_history(history_file, plot_location):

history = pd.read_csv(history_file)
history[COLUMN_START_DATE] = pd.to_datetime(history[COLUMN_START_DATE])

# Select the last 6 months of data
history.set_index(COLUMN_START_DATE, inplace=True)
history = history.last('6M').copy()

plt.figure()
ax = 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)

ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
plt.savefig(plot_location, dpi=300, bbox_inches='tight')
Dntfreitas marked this conversation as resolved.
Show resolved Hide resolved
Binary file added history/img/plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions post_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ def gas_prices_message(price_current, price_previous):
return f'{price_current}€ = {price_previous}€'


def create_api(client):
auth = tweepy.OAuthHandler(
client.consumer_key,
client.consumer_secret,
)
auth.set_access_token(
client.access_token,
client.access_token_secret,
)
return tweepy.API(auth)


def post_image(tweet, image_path):
# Get Twitter client
client = create_client_twitter()
# Create Twitter API
api = create_api(client)
# Upload image
image = api.media_upload(image_path)
# Post tweet
return api.update_status(status=tweet, media_ids=[image.media_id])


def make_tweet(dict_prices):
# Get Twitter client
client = create_client_twitter()
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
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
9 changes: 6 additions & 3 deletions script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from bs4 import BeautifulSoup

from add_history import add_history
from constants import CURRENT_GAS_INFO_FILE, START_DATE_KEY, ENDPOINT, SPAN_ID, NEW_DATE_KEY, END_DATE_KEY, GAS_KEY, PREVIOUS_WEEK, CURRENT_WEEK, GASOLINE_98, GASOLINE_95, DIFFERENCE_95_98_PRICE
from constants import CURRENT_GAS_INFO_FILE, START_DATE_KEY, ENDPOINT, SPAN_ID, NEW_DATE_KEY, END_DATE_KEY, GAS_KEY, PREVIOUS_WEEK, CURRENT_WEEK, GASOLINE_98, GASOLINE_95, DIFFERENCE_95_98_PRICE, CURRENT_GAS_HISTORY_CSV_FILE, CURRENT_GAS_HISTORY_PLOT
from history.generate_plot import generate_plot_history
from post_tweet import make_tweet

# Get current data to check if there is an update
Expand All @@ -31,7 +32,7 @@
i = 1
while i < len(gas_info):
if gas_info[i].startswith(NEW_DATE_KEY):
date_positions.append(i+1)
date_positions.append(i + 1)
i += 1

# Parse the last available date
Expand Down Expand Up @@ -64,7 +65,9 @@
make_tweet(dict_prices)
# Add history
add_history(dict_prices)
# Generate history plot
generate_plot_history(CURRENT_GAS_HISTORY_CSV_FILE, CURRENT_GAS_HISTORY_PLOT)
# Writing JSON file
content = json.dumps(dict_prices, indent=1, ensure_ascii=False)
with open(CURRENT_GAS_INFO_FILE, 'w') as f:
f.write(content)
f.write(content)
11 changes: 11 additions & 0 deletions script_post_plot_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pandas as pd

from constants import TWEET_HISTORY, CURRENT_GAS_HISTORY_PLOT, CURRENT_GAS_HISTORY_CSV_FILE, COLUMN_START_DATE
from post_tweet import post_image

# Get start and end dates
history = pd.read_csv(CURRENT_GAS_HISTORY_CSV_FILE)
start_date, end_date = history[COLUMN_START_DATE].min(), history[COLUMN_START_DATE].max()

# Post tweet with image
post_image(TWEET_HISTORY.format(start_date=start_date, end_date=end_date), CURRENT_GAS_HISTORY_PLOT)