forked from singer-io/target-gsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_gsheet.py
executable file
·224 lines (182 loc) · 7.5 KB
/
target_gsheet.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
import argparse
import functools
import io
import os
import sys
import json
import logging
import collections
import threading
import http.client
import urllib
import pkg_resources
from jsonschema import validate
import singer
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
parser = argparse.ArgumentParser(parents=[tools.argparser])
parser.add_argument('-c', '--config', help='Config file', required=True)
flags = parser.parse_args()
except ImportError:
flags = None
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
logger = singer.get_logger()
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Singer Sheets Target'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'sheets.googleapis.com-singer-target.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def emit_state(state):
if state is not None:
line = json.dumps(state)
logger.debug('Emitting state {}'.format(line))
sys.stdout.write("{}\n".format(line))
sys.stdout.flush()
def get_spreadsheet(service, spreadsheet_id):
return service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
def get_values(service, spreadsheet_id, range):
return service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id, range=range).execute()
def add_sheet(service, spreadsheet_id, title):
return service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheet_id,
body={
'requests':[
{
'addSheet': {
'properties': {
'title': title,
'gridProperties': {
'rowCount': 1000,
'columnCount': 26
}
}
}
}
]
}).execute()
def append_to_sheet(service, spreadsheet_id, range, values):
return service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id,
range=range,
valueInputOption='USER_ENTERED',
body={'values': [values]}).execute()
def flatten(d, parent_key='', sep='__'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, str(v) if type(v) is list else v))
return dict(items)
def persist_lines(service, spreadsheet, lines):
state = None
schemas = {}
key_properties = {}
headers_by_stream = {}
for line in lines:
try:
msg = singer.parse_message(line)
except json.decoder.JSONDecodeError:
logger.error("Unable to parse:\n{}".format(line))
raise
if isinstance(msg, singer.RecordMessage):
if msg.stream not in schemas:
raise Exception("A record for stream {} was encountered before a corresponding schema".format(msg.stream))
schema = schemas[msg.stream]
validate(msg.record, schema)
flattened_record = flatten(msg.record)
matching_sheet = [s for s in spreadsheet['sheets'] if s['properties']['title'] == msg.stream]
new_sheet_needed = len(matching_sheet) == 0
range_name = "{}!A1:ZZZ".format(msg.stream)
append = functools.partial(append_to_sheet, service, spreadsheet['spreadsheetId'], range_name)
if new_sheet_needed:
add_sheet(service, spreadsheet['spreadsheetId'], msg.stream)
spreadsheet = get_spreadsheet(service, spreadsheet['spreadsheetId']) # refresh this for future iterations
headers_by_stream[msg.stream] = list(flattened_record.keys())
append(headers_by_stream[msg.stream])
elif msg.stream not in headers_by_stream:
first_row = get_values(service, spreadsheet['spreadsheetId'], range_name + '1')
if 'values' in first_row:
headers_by_stream[msg.stream] = first_row.get('values', None)[0]
else:
headers_by_stream[msg.stream] = list(flattened_record.keys())
append(headers_by_stream[msg.stream])
result = append([flattened_record.get(x, None) for x in headers_by_stream[msg.stream]]) # order by actual headers found in sheet
state = None
elif isinstance(msg, singer.StateMessage):
logger.debug('Setting state to {}'.format(msg.value))
state = msg.value
elif isinstance(msg, singer.SchemaMessage):
schemas[msg.stream] = msg.schema
key_properties[msg.stream] = msg.key_properties
else:
raise Exception("Unrecognized message {}".format(msg))
return state
def collect():
try:
version = pkg_resources.get_distribution('target-gsheet').version
conn = http.client.HTTPSConnection('collector.stitchdata.com', timeout=10)
conn.connect()
params = {
'e': 'se',
'aid': 'singer',
'se_ca': 'target-gsheet',
'se_ac': 'open',
'se_la': version,
}
conn.request('GET', '/i?' + urllib.parse.urlencode(params))
response = conn.getresponse()
conn.close()
except:
logger.debug('Collection request failed')
def main():
with open(flags.config) as input:
config = json.load(input)
if not config.get('disable_collection', False):
logger.info('Sending version information to stitchdata.com. ' +
'To disable sending anonymous usage data, set ' +
'the config parameter "disable_collection" to true')
threading.Thread(target=collect).start()
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)
spreadsheet = get_spreadsheet(service, config['spreadsheet_id'])
input = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
state = None
state = persist_lines(service, spreadsheet, input)
emit_state(state)
logger.debug("Exiting normally")
if __name__ == '__main__':
main()