forked from turbot/steampipe-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.py
62 lines (52 loc) · 1.68 KB
/
push.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
"""
push hn_items_all and hn_people to steampipe cloud
"""
import os, psycopg2, requests
def connect():
conn_str = f""" host='localhost' dbname='steampipe' user='steampipe' \
port='9193' password='{os.environ['STEAMPIPE_LOCAL_PASSWORD']}' """
conn = psycopg2.connect(conn_str)
return conn.cursor()
def push(sql):
url = 'https://cloud.steampipe.io/api/latest/org/acme/workspace/jon/query'
data = {'sql':sql}
token = os.environ['STEAMPIPE_CLOUD_TOKEN']
headers = {"Authorization": "Bearer " + token}
r = requests.post(url, headers=headers, data=data)
return r.text
def escape(row):
_row = []
for col in row:
col = col.replace("'", "''")
col = col.replace('""', '"')
col = col.replace('"', "''")
_row.append(col)
row = str(tuple(_row))
row = row.replace('"', "'")
return row
def init_sql(table):
return ( f'insert into public.{table} values ' )
def push_rows(table, query):
cur = connect()
cur.execute(query)
rows = cur.fetchall()
i = 0
sql = init_sql(table)
values = []
for row in rows:
i += 1
value = escape(row)
values.append( value )
if i % 1000 == 0:
sql += ','.join(values)
print(i, push(sql))
sql = init_sql(table)
values = []
if i == len(rows) and len(values):
sql = init_sql(table)
sql += ','.join(values)
print(i, push(sql))
push('drop table if exists hn_items_all')
push('create table public.hn_items_all ( id text, title text, "time" text, by text, score text, descendants text, type text, url text )')
push_rows('hn_items_all', 'select id, title, "time", by, score, descendants, type, url from public.hn_items_all order by id desc')
push('grant all on hn_items_all to public')