-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_watcher_dag.py
74 lines (56 loc) · 1.68 KB
/
crypto_watcher_dag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from datetime import datetime
import os
from airflow.models.dag import DAG, TaskInstance
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator, BranchPythonOperator
import pandas as pd
import requests
from sqlalchemy import create_engine
db = {
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASS')
}
default_arg = {
'owner': 'Bruno',
'start_date': datetime(2022, 4, 27)
}
dag = DAG(dag_id='crypto_watcher', default_args=default_arg)
def build_dataframe(data):
if data:
return pd.DataFrame().from_records(data)
def save_on_database(dataframe):
engine = create_engine(f'postgresql://{db.get("user")}:{db.get("password")}@localhost:5432/cryptodb')
dataframe.to_sql('daily_crypto', engine, if_exists='replace')
def execute():
try:
response = requests.get('https://api2.binance.com/api/v3/ticker/24hr')
if response != 200:
raise Exception('Fail to fetch data from server')
data = response.json()
df = build_dataframe(data)
save_on_database(df)
return 'success_notify'
except BaseException:
return 'fail_notify'
init_pipeline = BashOperator(
task_id='init_pipeline',
bash_command='echo Initializing pipeline!',
dag=dag
)
execute = BranchPythonOperator(
task_id='execute',
python_callable=execute,
provide_context=True,
dag=dag
)
success_notify = BashOperator(
task_id='success_notify',
bash_command='echo Pipeline finished!',
dag=dag
)
fail_notify = BashOperator(
task_id='fail_notify',
bash_command='echo Pipeline failed!',
dag=dag
)
init_pipeline >> execute >> [success_notify, fail_notify]