From 998f019b21acf875d50603d9ac30a9cd0c545905 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Wed, 19 Apr 2017 00:32:11 +0200 Subject: [PATCH] Script to update ELS --- send_data.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 send_data.py diff --git a/send_data.py b/send_data.py new file mode 100644 index 0000000..e34aa4b --- /dev/null +++ b/send_data.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +""" + Send JSON files to ELS +""" + +import argparse +import requests + +ELASTICSEARCH_URL = 'http://localhost:9200/' + +def send_data(file_name): + """ + Send a file to ELS + """ + + print("Sending {} data...".format(file)) + res = requests.post(url=ELASTICSEARCH_URL + '_bulk', + data=open(file, 'rb'), + headers={'Content-Type': 'application/x-ndjson'}) + if res.status_code != 200: + print("An error occured") + print(res.text) + else: + print(file_name + " data sended!") + + +def delete_index(index_name): + """ + Delete an index in ELS + """ + print('Deleting index {}...'.format(index_name)) + res = requests.delete(url=ELASTICSEARCH_URL + INDEX_NAME) + if res.status_code == 200: + print("Deleted!") + else: + print("An error occured") + print(res.text) + +#### main block #### + +# Get options +parser = argparse.ArgumentParser() +parser.add_argument('-s', '--song', action='store_false', + help='Disable send music data') +parser.add_argument('-l', '--album', action='store_true', + help='Enable send album data') +parser.add_argument('-r', '--artist', action='store_true', + help='Enable send music data') +parser.add_argument('-A', '--ALL', action='store_true', + help='Send all') +parser.add_argument('-D', '--DELETE', action='store_true', + help='Delete old index') + +INDEX_NAME = "itunessongs" + +if __name__ == '__main__': + args = parser.parse_args() + + if args.DELETE: + delete_index(INDEX_NAME) + + if args.song or args.ALL: + file = "es-music-data.json" + send_data(file) + if args.artist or args.ALL: + file = "es-artist-data.json" + send_data(file) + + if args.album or args.ALL: + file = "es-albums-data.json" + send_data(file)