-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_append.py
48 lines (41 loc) · 1.21 KB
/
push_append.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
import os
try:
import send_mail
from main import capture_data
from sheet_connect import get_connect_sheet
from sheet_connect import push_to_sheets
from sheet_connect import append_new_data
import pandas as pd
except Exception as e:
send_mail.send_email(e)
os._exit(0)
#EXTRACTION AND TRANSFORMATION PHASE
#This scrapes the data from Yahoo Finance
try:
data = capture_data()
except Exception as e:
send_mail.send_email(e)
os._exit(0)
#LOADING PHASE
try:
#This imports the service account credentials that will allow us access the sheet
client = get_connect_sheet()
#Once the connection has been made, the Google Sheet can now be accessed
push_sheet = client.open('test_sheet')
#opening the data sheet
data_push = push_sheet.worksheet('data')
data_push_df = pd.DataFrame.from_dict(data_push.get_all_records())
except Exception as e:
send_mail.send_email(e)
os._exit(0)
print(data)
#You can either load or append the data
try:
if len(data_push_df) == 0:
push_to_sheets(data_push, data)
else:
append_new_data(data,'data','test_sheet')
except Exception as e:
print('Could not append')
send_mail.send_email(e)
os._exit(0)