73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
#!/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)
|