-
Notifications
You must be signed in to change notification settings - Fork 7
/
shopify.py
50 lines (40 loc) · 1.31 KB
/
shopify.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
'''
Extracts properties from a TopoJSON file into a Shopify CSV file
'''
import io
import json
import pandas as pd
from tqdm import tqdm
from six import StringIO
def properties(path, encoding='utf-8'):
with io.open(path, encoding=encoding) as handle:
topo = json.load(handle)
data = []
for shape in topo['objects'].values():
for geom in shape['geometries']:
data.append(geom.get('properties', {}))
return pd.DataFrame(data)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description=__doc__.strip(),
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-o', '--output', help='Output CSV file', default='product-template.csv')
parser.add_argument('file', help='TopoJSON file', nargs='+')
args = parser.parse_args()
result = []
for path in tqdm(args.file):
data = properties(path)
buf = StringIO()
data.to_html(buf, index=False, classes=None)
title = path
unique_id = path
info = {
'Handle': unique_id,
'Title': title,
'Body (HTML)': buf.getvalue(),
'Variant Price': 25000
}
result.append(info)
result = pd.DataFrame(result)
result.to_csv(args.output, index=False, encoding='utf-8')