Put Mapping, error handling
This commit is contained in:
33
send_data.py
33
send_data.py
@@ -33,16 +33,35 @@ def delete_index(index_name, quiet=False):
|
|||||||
Delete an index in ELS
|
Delete an index in ELS
|
||||||
"""
|
"""
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print('Deleting index {}...'.format(index_name))
|
print('Deleting index \'{}\'...'.format(index_name))
|
||||||
res = requests.delete(url=ELASTICSEARCH_URL + INDEX_NAME)
|
res = requests.delete(url=ELASTICSEARCH_URL + INDEX_NAME)
|
||||||
if res.status_code == 200:
|
if res.status_code == 200:
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print("Deleted!")
|
print("Deleted!")
|
||||||
else:
|
else:
|
||||||
print("An error occured")
|
print("An error occured")
|
||||||
# TODO Catch "index_not_found_exception"
|
if res.json()['error']['type'] == 'index_not_found_exception':
|
||||||
|
print("Index '{}' doesn't exist and can't be deleted".format(index_name))
|
||||||
|
else:
|
||||||
print(res.text)
|
print(res.text)
|
||||||
|
|
||||||
|
def put_mapping(index_name, mapping_file, quiet=False):
|
||||||
|
"""
|
||||||
|
Send a mapping file for an index to ELS.
|
||||||
|
"""
|
||||||
|
if not quiet:
|
||||||
|
print("Put '{}' mapping file...".format(mapping_file.name))
|
||||||
|
res = requests.put(url=ELASTICSEARCH_URL + index_name,
|
||||||
|
data=mapping_file,
|
||||||
|
headers={'Content-Type': 'application/x-ndjson'})
|
||||||
|
if res.status_code != 200:
|
||||||
|
print("An error occured")
|
||||||
|
print(res.text)
|
||||||
|
else:
|
||||||
|
if not quiet:
|
||||||
|
print("File '{} sended to Elasticsearch!".format(mapping_file.name))
|
||||||
|
|
||||||
|
|
||||||
#### main block ####
|
#### main block ####
|
||||||
|
|
||||||
# Settings var (can be overloaded)
|
# Settings var (can be overloaded)
|
||||||
@@ -53,6 +72,7 @@ INDEX_NAME = "itunessongs"
|
|||||||
DEFAULT_SONG_FILE = 'es-music-data.json'
|
DEFAULT_SONG_FILE = 'es-music-data.json'
|
||||||
DEFAULT_ALBUM_FILE = 'es-albums-data.json'
|
DEFAULT_ALBUM_FILE = 'es-albums-data.json'
|
||||||
DEFAULT_ARTIST_FILE = 'es-artist-data.json'
|
DEFAULT_ARTIST_FILE = 'es-artist-data.json'
|
||||||
|
DEFAULT_MAPPING_FILE = 'mapping.json'
|
||||||
|
|
||||||
# Get options
|
# Get options
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@@ -75,13 +95,15 @@ song_group.add_argument('-sf', '--song-file', type=argparse.FileType('r'),
|
|||||||
sending_group.add_argument('-al', '--album-file', nargs='?', type=argparse.FileType('r'), const=DEFAULT_ALBUM_FILE,
|
sending_group.add_argument('-al', '--album-file', nargs='?', type=argparse.FileType('r'), const=DEFAULT_ALBUM_FILE,
|
||||||
help='Enable send album data. Optionally, precise the album file (default: \'{}\')'.format(DEFAULT_ALBUM_FILE))
|
help='Enable send album data. Optionally, precise the album file (default: \'{}\')'.format(DEFAULT_ALBUM_FILE))
|
||||||
sending_group.add_argument('-ar', '--artist-file', nargs='?', type=argparse.FileType('r'), const=DEFAULT_ARTIST_FILE,
|
sending_group.add_argument('-ar', '--artist-file', nargs='?', type=argparse.FileType('r'), const=DEFAULT_ARTIST_FILE,
|
||||||
help='Enable send artist data. Optionally, precise the artist file (default: \'{}\''.format(DEFAULT_ARTIST_FILE))
|
help='Enable send artist data. Optionally, precise the artist file (default: \'{}\')'.format(DEFAULT_ARTIST_FILE))
|
||||||
|
sending_group.add_argument('-m', '--mapping-file', type=argparse.FileType('r'), default=DEFAULT_MAPPING_FILE,
|
||||||
|
help='If deleting index, mapping file to send (default: \'{}\')'.format(DEFAULT_MAPPING_FILE))
|
||||||
# Mode
|
# Mode
|
||||||
mode_group = parser.add_argument_group('Mode')
|
mode_group = parser.add_argument_group('Mode')
|
||||||
mode_group.add_argument('-A', '--ALL', action='store_true',
|
mode_group.add_argument('-A', '--ALL', action='store_true',
|
||||||
help='Send all possible data: song, artist and album')
|
help='Send all possible data: song, artist and album')
|
||||||
mode_group.add_argument('-D', '--DELETE', action='store_true',
|
mode_group.add_argument('-D', '--DELETE', action='store_true',
|
||||||
help='Delete old index (precise name with -idx argument)')
|
help='Delete old index (precise name with -idx argument). This will send a mapping to ELS.')
|
||||||
# Settings
|
# Settings
|
||||||
g_settings_group = parser.add_argument_group('Global Settings')
|
g_settings_group = parser.add_argument_group('Global Settings')
|
||||||
g_settings_group.add_argument('-els', '--elasticsearch-url', default=ELASTICSEARCH_URL, nargs='?',
|
g_settings_group.add_argument('-els', '--elasticsearch-url', default=ELASTICSEARCH_URL, nargs='?',
|
||||||
@@ -107,13 +129,14 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
if args.DELETE:
|
if args.DELETE:
|
||||||
delete_index(INDEX_NAME, args.quiet)
|
delete_index(INDEX_NAME, args.quiet)
|
||||||
|
put_mapping(INDEX_NAME, args.mapping_file, args.quiet)
|
||||||
|
|
||||||
if args.song or args.ALL:
|
if args.song or args.ALL:
|
||||||
# Retrieve default song file if not precised
|
# Retrieve default song file if not precised
|
||||||
if not args.song_file:
|
if not args.song_file:
|
||||||
try:
|
try:
|
||||||
song_file = open(DEFAULT_SONG_FILE, 'r')
|
song_file = open(DEFAULT_SONG_FILE, 'r')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError: # Theoretically, can occur only when default file not found
|
||||||
print("Error: can't open default music file: [Errno 2] No such file or directory: '{}'.".format(DEFAULT_SONG_FILE))
|
print("Error: can't open default music file: [Errno 2] No such file or directory: '{}'.".format(DEFAULT_SONG_FILE))
|
||||||
print("Use -sf argument, or -h for more help")
|
print("Use -sf argument, or -h for more help")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user