Var for default fine name + improve song file handle
This commit is contained in:
70
send_data.py
70
send_data.py
@@ -4,6 +4,7 @@
|
||||
Send JSON files to ELS
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import requests
|
||||
|
||||
@@ -21,7 +22,6 @@ def send_data(file, quiet=False):
|
||||
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:
|
||||
if not quiet:
|
||||
@@ -40,6 +40,7 @@ def delete_index(index_name, quiet=False):
|
||||
print("Deleted!")
|
||||
else:
|
||||
print("An error occured")
|
||||
# TODO Catch "index_not_found_exception"
|
||||
print(res.text)
|
||||
|
||||
#### main block ####
|
||||
@@ -48,39 +49,45 @@ def delete_index(index_name, quiet=False):
|
||||
ELASTICSEARCH_URL = 'http://localhost:9200/'
|
||||
INDEX_NAME = "itunessongs"
|
||||
|
||||
# Default file names
|
||||
DEFAULT_SONG_FILE = 'es-music-data.json'
|
||||
DEFAULT_ALBUM_FILE = 'es-albums-data.json'
|
||||
DEFAULT_ARTIST_FILE = 'es-artist-data.json'
|
||||
|
||||
# Get options
|
||||
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.
|
||||
By default: send song data enable, send album & artist data disabled.
|
||||
'''
|
||||
)
|
||||
# Choose what to enable for send and files
|
||||
song_group = parser.add_mutually_exclusive_group()
|
||||
song_group.add_argument('-s', '--song', action='store_false',
|
||||
help='Disable send song data')
|
||||
song_group.add_argument('-sf', '--song-file', type=argparse.FileType('r'),
|
||||
help='Song file data to send (default: es-music-data.json).')
|
||||
parser.add_argument('-al', '--album_file', nargs='?', type=argparse.FileType('r'), const='es-albums-data.json',
|
||||
help='Enable send album data. Optionally, precise the album file (default: \'es-albums-data.json\')')
|
||||
parser.add_argument('-ar', '--artist-file', nargs='?', type=argparse.FileType('r'), const='es-artist-data.json',
|
||||
help='Enable send artist data. Optionally, precise the artist file (default: \'es-artist-data.json\'')
|
||||
# Mode
|
||||
parser.add_argument('-A', '--ALL', action='store_true',
|
||||
help='Send all possible data: song, artist and album')
|
||||
parser.add_argument('-D', '--DELETE', action='store_true',
|
||||
help='Delete old index')
|
||||
# ELS Options
|
||||
parser.add_argument('-els', '--elasticsearch_url', default=ELASTICSEARCH_URL, nargs='?',
|
||||
help="Elasticsearch URL to send data (default: {})".format(ELASTICSEARCH_URL))
|
||||
parser.add_argument('-idx', '--index_name', default=INDEX_NAME,
|
||||
help="Index name in Elasticsearch for data (default: {})".format(INDEX_NAME))
|
||||
# Bulk
|
||||
parser.add_argument('-q', '--quiet', action='store_true',
|
||||
help="Disable main output")
|
||||
|
||||
# TODO Use group
|
||||
# Choose what to enable for send and files
|
||||
sending_group = parser.add_argument_group("Sending options")
|
||||
song_group = sending_group.add_mutually_exclusive_group()
|
||||
song_group.add_argument('-s', '--song', action='store_false',
|
||||
help='Disable send song data')
|
||||
song_group.add_argument('-sf', '--song-file', type=argparse.FileType('r'),
|
||||
help='Song file data to send (default: \'{}\').'.format(DEFAULT_SONG_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))
|
||||
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))
|
||||
# Mode
|
||||
mode_group = parser.add_argument_group('Mode')
|
||||
mode_group.add_argument('-A', '--ALL', action='store_true',
|
||||
help='Send all possible data: song, artist and album')
|
||||
mode_group.add_argument('-D', '--DELETE', action='store_true',
|
||||
help='Delete old index')
|
||||
# Settings
|
||||
g_settings_group = parser.add_argument_group('Global Settings')
|
||||
g_settings_group.add_argument('-els', '--elasticsearch-url', default=ELASTICSEARCH_URL, nargs='?',
|
||||
help="Elasticsearch URL to send data (default: \'{}\')".format(ELASTICSEARCH_URL))
|
||||
g_settings_group.add_argument('-idx', '--index-name', default=INDEX_NAME,
|
||||
help="Index name in Elasticsearch for data (default: \'{}\')".format(INDEX_NAME))
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parser.parse_args()
|
||||
@@ -90,16 +97,27 @@ if __name__ == '__main__':
|
||||
ELASTICSEARCH_URL = args.elasticsearch_url
|
||||
|
||||
if not args.quiet:
|
||||
print("Settings values:")
|
||||
print("*** Settings values ***")
|
||||
print("Elasticsearch URL:\t" + ELASTICSEARCH_URL)
|
||||
print("Index name:\t\t" + INDEX_NAME)
|
||||
print("")
|
||||
else:
|
||||
print("Processing...")
|
||||
|
||||
if args.DELETE:
|
||||
delete_index(INDEX_NAME, args.quiet)
|
||||
|
||||
if args.song or args.ALL:
|
||||
song_file = args.song_file
|
||||
# Retrieve default song file if not precised
|
||||
if not args.song_file:
|
||||
try:
|
||||
song_file = open(DEFAULT_SONG_FILE, 'r')
|
||||
except FileNotFoundError:
|
||||
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")
|
||||
sys.exit(2)
|
||||
else:
|
||||
song_file = args.song_file
|
||||
if not args.quiet:
|
||||
print("Take file '{}' to send song data".format(song_file.name))
|
||||
send_data(song_file, args.quiet)
|
||||
|
||||
Reference in New Issue
Block a user