Skip to content

Instantly share code, notes, and snippets.

@jonassvatos
Created December 13, 2018 16:35
Show Gist options
  • Save jonassvatos/e34b62d7e8a13ed4530aca7aedeeba37 to your computer and use it in GitHub Desktop.
Save jonassvatos/e34b62d7e8a13ed4530aca7aedeeba37 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import csv
import requests
import json
import os
import sys
def elastic(name, director, year):
payload = {
"_source": "FILMID",
"size": 1,
"query": {
"bool": {
"should": [
{ "match": { "NAZEV-SKUT": name }},
{ "match": { "REZIE": director }},
{ "match": { "ROK-VYROBY": year }}
]
}
}
}
r = requests.post('https://{0}/{1}/_search'.format(os.environ['ELASTIC_HOST'], os.environ['ELASTIC_INDEX']), json=payload, auth=(os.environ['ELASTIC_USER'], os.environ['ELASTIC_PASS']))
try:
ais_id = r.json()['hits']['hits'][0]['_source']['FILMID']
print(ais_id)
except Exception as e:
print('ERROR')
print('---------', file=sys.stderr)
print(e, file=sys.stderr)
print(json.dumps(payload), file=sys.stderr)
print('---------', file=sys.stderr)
with open('a.tsv') as tsvfile:
reader = csv.reader(tsvfile, delimiter='\t')
for row in reader:
name = row[2]
director = row[3]
year = row[4]
elastic(name, director, year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment