forked from FrodeRennemo/VinmonopoletElastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
71 lines (56 loc) · 2.43 KB
/
script.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
import csv
import urllib2
import logging
import json
import os
import sys
import httplib
# fix Norwegian number formatting -> standard (sorry about that)
def fixNumber(str):
if str.find(",") > -1:
str = str.replace(',', '.')
return str
def UnicodeDictReader(path,**kwargs):
with open(path + '/download/iconproducts.csv','r') as fout:
csv_reader = csv.DictReader(fout, **kwargs)
for row in csv_reader:
try:
yield dict([(key, value)
for key, value in row.iteritems()])
except Exception as e:
pass
es_index = "vinmonopolet"
doctype = "produkt"
def main():
logging.basicConfig(level=logging.WARN, format='%(asctime)s %(message)s')
#Check if index exist?
indexJson = json.JSONEncoder().encode({"settings" : {"index" : {"number_of_shards" : 5, "number_of_replicas" : 1 }}})
indexConn = httplib.HTTPConnection("localhost:9200")
indexConn.request("PUT","/vinmonopolet",indexJson,headers={'Content-Type':'application/json'})
dirfile = os.path.dirname(os.path.realpath(__file__))
json_mapping = json.loads(open(dirfile + "/mapping_fielddata.json", "r").read())
templateJson = json.JSONEncoder().encode(json_mapping)
templateConn = httplib.HTTPConnection("localhost:9200")
templateConn.request("PUT","/_template/vinmonopolettpl",templateJson,headers={'Content-Type':'application/json'})
cr = UnicodeDictReader(dirfile,delimiter=';')
fields = ["Alkohol","Pris", "Volum", "Bitterhet","Literpris","Sukker", "Syre"]
docConn = httplib.HTTPConnection("localhost:9200")
for row in cr:
if len(row) > 2:
for field in fields:
if row[field]=="Ukjent":
row[field] = ""
else:
try:
row[field] = float(fixNumber(row[field]))
except Exception as exp:
print("Could not convert field")
print row['Varenavn']
docJson = json.JSONEncoder().encode(row)
try:
docConn.request("POST","/vinmonopolet/produkt",docJson,headers={'Content-Type':'application/json'})
except Exception as ex:
docConn = httplib.HTTPConnection("localhost:9200")
docConn.request("POST","/vinmonopolet/produkt",docJson,headers={'Content-Type':'application/json'})
if __name__ == '__main__':
main()