adding cmd_lyrics implementation
This commit is contained in:
1
src/musicbrainzapi/__header__.py
Normal file
1
src/musicbrainzapi/__header__.py
Normal file
@@ -0,0 +1 @@
|
||||
__header__ = 'musicbrainzapi'
|
||||
1
src/musicbrainzapi/__version__.py
Normal file
1
src/musicbrainzapi/__version__.py
Normal file
@@ -0,0 +1 @@
|
||||
__version__ = '0.1.0'
|
||||
0
src/musicbrainzapi/api/__init__.py
Normal file
0
src/musicbrainzapi/api/__init__.py
Normal file
13
src/musicbrainzapi/api/authenticate.py
Normal file
13
src/musicbrainzapi/api/authenticate.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import musicbrainzngs
|
||||
|
||||
from musicbrainzapi.__header__ import __header__
|
||||
from musicbrainzapi.__version__ import __version__
|
||||
|
||||
|
||||
def set_useragent() -> None:
|
||||
"""
|
||||
Function to set the correct user agent required for musicbrainz api
|
||||
access.
|
||||
"""
|
||||
musicbrainzngs.set_useragent(__header__, __version__)
|
||||
|
||||
0
src/musicbrainzapi/cli/__init__.py
Normal file
0
src/musicbrainzapi/cli/__init__.py
Normal file
60
src/musicbrainzapi/cli/cli.py
Normal file
60
src/musicbrainzapi/cli/cli.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import sys
|
||||
from importlib import import_module
|
||||
|
||||
import click
|
||||
|
||||
|
||||
CONTEXT_SETTINGS = dict(auto_envvar_prefix='COMPLEX')
|
||||
|
||||
|
||||
class Environment(object):
|
||||
def __init__(self):
|
||||
self.verbose = False
|
||||
self.home = os.getcwd()
|
||||
|
||||
|
||||
pass_environment = click.make_pass_decorator(Environment, ensure=True)
|
||||
cmd_folder = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), 'commands')
|
||||
)
|
||||
|
||||
class ComplexCLI(click.MultiCommand):
|
||||
def list_commands(self, ctx):
|
||||
rv = []
|
||||
for filename in os.listdir(cmd_folder):
|
||||
if filename.endswith('.py') and filename.startswith('cmd_'):
|
||||
rv.append(filename[4:-3])
|
||||
rv.sort()
|
||||
return rv
|
||||
|
||||
def get_command(self, ctx, name):
|
||||
try:
|
||||
if sys.version_info[0] == 2:
|
||||
name = name.encode('ascii', 'replace')
|
||||
mod = import_module(f'musicbrainzapi.cli.commands.cmd_{name}')
|
||||
# mod = __import__(
|
||||
# 'complex.commands.cmd_' + name, None, None, ['cli']
|
||||
# )
|
||||
except ImportError as e:
|
||||
print(e)
|
||||
return
|
||||
return mod.cli
|
||||
|
||||
|
||||
@click.command(cls=ComplexCLI, context_settings=CONTEXT_SETTINGS)
|
||||
@click.option(
|
||||
'--home',
|
||||
type=click.Path(exists=True, file_okay=False, resolve_path=True),
|
||||
help='Changes the folder to operate on.',
|
||||
)
|
||||
@click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode.')
|
||||
@pass_environment
|
||||
def cli(ctx, verbose, home):
|
||||
"""A complex command line interface."""
|
||||
ctx.verbose = verbose
|
||||
if home is not None:
|
||||
ctx.home = home
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
20
src/musicbrainzapi/cli/commands/*REPL* [python]
Normal file
20
src/musicbrainzapi/cli/commands/*REPL* [python]
Normal file
@@ -0,0 +1,20 @@
|
||||
Python 3.8.0 (default, Nov 3 2019, 22:15:08)
|
||||
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
(InteractiveConsole)
|
||||
>>> match_check := False
|
||||
File "<console>", line 1
|
||||
match_check := False
|
||||
^
|
||||
SyntaxError: invalid syntax
|
||||
>>> if match_check := False:
|
||||
... print('false')
|
||||
File "<console>", line 2
|
||||
print('false')
|
||||
^
|
||||
IndentationError: expected an indented block
|
||||
>>> if match_check := False:
|
||||
... print(False)
|
||||
...
|
||||
>>> if not match_check := False:
|
||||
... print(False)
|
||||
0
src/musicbrainzapi/cli/commands/__init__.py
Normal file
0
src/musicbrainzapi/cli/commands/__init__.py
Normal file
108
src/musicbrainzapi/cli/commands/cmd_lyrics.py
Normal file
108
src/musicbrainzapi/cli/commands/cmd_lyrics.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from pprint import pprint
|
||||
|
||||
import click
|
||||
import musicbrainzngs
|
||||
|
||||
from musicbrainzapi.cli.cli import pass_environment
|
||||
from musicbrainzapi.api import authenticate
|
||||
|
||||
|
||||
class LyricsInfo:
|
||||
"""docstring for LyricsInfo"""
|
||||
|
||||
def __init__(self, artist: str) -> None:
|
||||
authenticate.set_useragent()
|
||||
self.artist = artist
|
||||
super().__init__()
|
||||
|
||||
def _search_artist(self) -> None:
|
||||
self.artists = musicbrainzngs.search_artists(artist=self.artist)
|
||||
pprint(self.artists['artist-list'])
|
||||
|
||||
if self.artists.get('artist-count') == 0:
|
||||
self.chosen_artist = 'Null'
|
||||
|
||||
# Get all results
|
||||
|
||||
self.sort_names = dict(
|
||||
(i.get('id'), f'{i.get("sort-name")} | {i.get("disambiguation")}')
|
||||
if i.get('disambiguation') is not None
|
||||
else (i.get('id'), f'{i.get("sort-name")}')
|
||||
for i in self.artists['artist-list']
|
||||
)
|
||||
|
||||
# Get accuracy scores
|
||||
|
||||
self._accuracy_scores = dict(
|
||||
(i.get('id'), int(i.get('ext:score', '0')))
|
||||
for i in self.artists['artist-list']
|
||||
)
|
||||
|
||||
# pprint(self._accuracy_scores)
|
||||
|
||||
# Get top 5 results
|
||||
|
||||
self.top_five_results = dict(
|
||||
(i, self._accuracy_scores.get(i))
|
||||
for i in sorted(
|
||||
self._accuracy_scores,
|
||||
key=self._accuracy_scores.get,
|
||||
reverse=True,
|
||||
)[0:5]
|
||||
)
|
||||
|
||||
# pprint(self.top_five_results)
|
||||
|
||||
# Check for 100% match
|
||||
self.chosen_artist = None
|
||||
for i, j in self.top_five_results.items():
|
||||
if self.chosen_artist is None:
|
||||
if j == 100:
|
||||
self.chosen_artist = i
|
||||
break
|
||||
|
||||
# pprint(self.sort_names.get(self.chosen_artist))
|
||||
return self
|
||||
|
||||
|
||||
class CommandUtility:
|
||||
"""docstring for CommandUtility"""
|
||||
|
||||
def get_multiple_options(option: tuple):
|
||||
for i in option:
|
||||
pass
|
||||
|
||||
|
||||
@click.argument('path', required=False, type=click.Path(resolve_path=True))
|
||||
@click.option('--artist', '-a', required=True, multiple=True, type=str)
|
||||
@click.command(short_help='a test command')
|
||||
@pass_environment
|
||||
def cli(ctx, artist, path: click.Path) -> None:
|
||||
# print(artist)
|
||||
artist_0 = LyricsInfo(artist)._search_artist()
|
||||
print(f'artist_0 = {artist_0.chosen_artist}')
|
||||
if artist_0.chosen_artist is None:
|
||||
|
||||
for i, j in zip(artist_0.top_five_results, range(1, 6)):
|
||||
click.echo(
|
||||
f'[{j}] {artist_0.sort_names.get(i)}'
|
||||
f' ({artist_0._accuracy_scores.get(i)}% match)'
|
||||
)
|
||||
|
||||
click.prompt(
|
||||
f'We found several results for {artist[0]}, which artist/group do you want?'
|
||||
)
|
||||
# implement this
|
||||
elif artist_0.chosen_artist == 'Null':
|
||||
click.echo(f"We didn't find any results for {artist}")
|
||||
else:
|
||||
click.confirm(
|
||||
f'Musicbrainz a perfect match for {artist[0]} with '
|
||||
f'"{artist_0.sort_names.get(artist_0.chosen_artist)}" (100% match)'
|
||||
'. Is this correct?'
|
||||
)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
LyricsInfo('Queenifie')._search_artist()
|
||||
Reference in New Issue
Block a user