Skip to content

Commit

Permalink
added try-except blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
vnbl committed Oct 20, 2024
1 parent 9351cf2 commit a89c41c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
28 changes: 12 additions & 16 deletions etl-pipeline/custom/construct_send_telegram_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,15 @@ def send_message(token, chat_id, msg=''):

@custom
def transform_custom(data, *args, **kwargs):
telegram_token = get_secret_value('TELEGRAM_BOT_TOKEN')
telegram_chat_id = get_secret_value('TELEGRAM_CHAT_ID')
aqi_summary = get_latest_aqi_summary(data)
if aqi_summary:
avg_aqi, max_aqi, min_aqi = aqi_summary
message = construct_message(avg_aqi, max_aqi, min_aqi)
send_message(token=telegram_token, chat_id=telegram_chat_id, msg=message)
return data


@test
def test_output(output, *args) -> None:
"""
Template code for testing the output of the block.
"""
assert output is not None, 'The output is undefined'
klogger = kwargs.get('logger')
try:
telegram_token = get_secret_value('TELEGRAM_BOT_TOKEN')
telegram_chat_id = get_secret_value('TELEGRAM_CHAT_ID')
aqi_summary = get_latest_aqi_summary(data)
if aqi_summary:
avg_aqi, max_aqi, min_aqi = aqi_summary
message = construct_message(avg_aqi, max_aqi, min_aqi)
response = send_message(token=telegram_token, chat_id=telegram_chat_id, msg=message)
klogger.info(f'Telegram response: {response}')
except Exception as e:
klogger.exception(f'An error occurred: {e}')
20 changes: 11 additions & 9 deletions etl-pipeline/custom/construct_send_twitter_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ def send_message(msg=''):
access_token=access_token,
access_token_secret=access_token_secret)
response = client.create_tweet(text=msg)

return
return response

@custom
def transform_custom(data, *args, **kwargs):
klogger = kwargs.get('logger')
try:
aqi_summary = get_latest_aqi_summary(data)
if aqi_summary:
avg_aqi, max_aqi, min_aqi = aqi_summary
message = construct_message(avg_aqi, max_aqi, min_aqi)
response = send_message(msg=message)
klogger.info(f'Twitter response: {response}')
except Exception as e:
klogger.exception(f'An error occurred: {e}')


aqi_summary = get_latest_aqi_summary(data)
if aqi_summary:
avg_aqi, max_aqi, min_aqi = aqi_summary
message = construct_message(avg_aqi, max_aqi, min_aqi)
send_message(msg=message)
return data
2 changes: 1 addition & 1 deletion etl-pipeline/pipelines/twitter_bot/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ blocks:
language: python
name: construct_send_twitter_message
retry_config: null
status: updated
status: executed
timeout: null
type: custom
upstream_blocks:
Expand Down

0 comments on commit a89c41c

Please sign in to comment.