-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontosql.py
45 lines (34 loc) · 1.23 KB
/
jsontosql.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
import os
import os.path
from json import loads
import click
from vendcrawler.scripts.vendcrawlerdb import VendCrawlerDB
class JSONToSQL(object):
def __init__(self, json, user, password, database):
data = loads(json.read())
db = VendCrawlerDB(user, password, database)
table = 'items'
columns = ['item_id', 'item_name', 'vendor_id', 'shop_name',
'amount', 'price', 'map', 'datetime']
values = []
for items in data:
for item in items:
value = [int(item['id']),
item['name'],
int(item['vendor_id']),
item['shop'],
int(item['amount'].replace(',', '')),
int(item['price'].replace(',', '')),
item['map'],
item['datetime']]
values.append(value)
self.db.insert(table, columns, values)
@click.command()
@click.argument('json', type=click.File('r'))
@click.argument('user')
@click.argument('password')
@click.argument('database')
def cli(json, user, password, database):
JSONToSQL(json, user, password, database)
if __name__ == '__main__':
cli()