Improve send data arguments
This commit is contained in:
61
send_data.py
61
send_data.py
@@ -9,30 +9,35 @@ import requests
|
||||
|
||||
ELASTICSEARCH_URL = 'http://localhost:9200/'
|
||||
|
||||
def send_data(file_name):
|
||||
def send_data(file_name, quiet=False):
|
||||
"""
|
||||
Send a file to ELS
|
||||
"""
|
||||
|
||||
print("Sending {} data...".format(file))
|
||||
if not quiet:
|
||||
print("Sending '{}' data file...".format(file_name))
|
||||
res = requests.post(url=ELASTICSEARCH_URL + '_bulk',
|
||||
data=open(file, 'rb'),
|
||||
data=open(file_name, 'rb'),
|
||||
headers={'Content-Type': 'application/x-ndjson'})
|
||||
if res.status_code != 200:
|
||||
print("An error occured")
|
||||
# TODO Catch "index_not_found_exception"
|
||||
print(res.text)
|
||||
else:
|
||||
print(file_name + " data sended!")
|
||||
if not quiet:
|
||||
print("File '{} sended to Elasticsearch!".format(file_name))
|
||||
|
||||
|
||||
def delete_index(index_name):
|
||||
def delete_index(index_name, quiet=False):
|
||||
"""
|
||||
Delete an index in ELS
|
||||
"""
|
||||
print('Deleting index {}...'.format(index_name))
|
||||
if not quiet:
|
||||
print('Deleting index {}...'.format(index_name))
|
||||
res = requests.delete(url=ELASTICSEARCH_URL + INDEX_NAME)
|
||||
if res.status_code == 200:
|
||||
print("Deleted!")
|
||||
if not quiet:
|
||||
print("Deleted!")
|
||||
else:
|
||||
print("An error occured")
|
||||
print(res.text)
|
||||
@@ -40,17 +45,31 @@ def delete_index(index_name):
|
||||
#### main block ####
|
||||
|
||||
# Get options
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(
|
||||
description='''
|
||||
Send JSON files formated for bulk Elasticsearch operation to an Elasticsearch.
|
||||
|
||||
Send song data is enable by default, send album & artist data is disabled default.
|
||||
'''
|
||||
)
|
||||
parser.add_argument('-s', '--song', action='store_false',
|
||||
help='Disable send music data')
|
||||
parser.add_argument('-l', '--album', action='store_true',
|
||||
help='Disable send song data')
|
||||
parser.add_argument('-sf', '--song-file', default='es-music-data.json', #TODO type=argparse.FileType('r'),
|
||||
help='Song file data to send (default: es-music-data.json). Need to enable send song data.')
|
||||
parser.add_argument('-al', '--album', action='store_true',
|
||||
help='Enable send album data')
|
||||
parser.add_argument('-r', '--artist', action='store_true',
|
||||
parser.add_argument('-alf', '--album-file', default='es-albums-data.json', #TODO type=argparse.FileType('r'),
|
||||
help='Album file data to send (default: es-albums-data.json). Need to enable send album data.')
|
||||
parser.add_argument('-ar', '--artist', action='store_true',
|
||||
help='Enable send music data')
|
||||
parser.add_argument('-arf', '--artist-file', default='es-artist-data.json', #TODO type=argparse.FileType('r'),
|
||||
help='Album file data to send (default: es-artist-data.json). Need to enable send artist data.')
|
||||
parser.add_argument('-A', '--ALL', action='store_true',
|
||||
help='Send all')
|
||||
parser.add_argument('-D', '--DELETE', action='store_true',
|
||||
help='Delete old index')
|
||||
parser.add_argument('-q', '--quiet', action='store_true',
|
||||
help="Disable main output")
|
||||
|
||||
INDEX_NAME = "itunessongs"
|
||||
|
||||
@@ -58,15 +77,21 @@ if __name__ == '__main__':
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.DELETE:
|
||||
delete_index(INDEX_NAME)
|
||||
delete_index(INDEX_NAME, args.quiet)
|
||||
|
||||
if args.song or args.ALL:
|
||||
file = "es-music-data.json"
|
||||
send_data(file)
|
||||
song_file = args.song_file
|
||||
if not args.quiet:
|
||||
print("Take file '{}' to send song data".format(song_file))
|
||||
send_data(song_file, args.quiet)
|
||||
if args.artist or args.ALL:
|
||||
file = "es-artist-data.json"
|
||||
send_data(file)
|
||||
artist_file = args.artist_file
|
||||
if not args.quiet:
|
||||
print("Take file '{}' to send song data".format(artist_file))
|
||||
send_data(artist_file, args.quiet)
|
||||
|
||||
if args.album or args.ALL:
|
||||
file = "es-albums-data.json"
|
||||
send_data(file)
|
||||
album_file = args.album_file
|
||||
if not args.quiet:
|
||||
print("Take file '{}' to send song data".format(album_file))
|
||||
send_data(album_file, args.quiet)
|
||||
|
||||
Reference in New Issue
Block a user