"""Logic for lyrics.""" import re from oto import response from lyrics import features from lyrics.constants import error from lyrics.constants.explicit_words import EXPLICIT_WORDS from lyrics.models import lyrics def get_lyrics(track_id): """Fetch track lyrics. Args: track_id (int): The track primary key. Returns: response.Response: object containing lyrics if available else not found response. """ return lyrics.get_lyrics_by_track_id(track_id) def put_lyrics(track_id, track_lyrics): """Put track lyrics. Args: track_id (int): The track primary key. track_lyrics (str): Track lyrics. Returns: response.Response: object containing lyrics. """ return lyrics.put_lyrics_by_track_id(track_id, track_lyrics) def delete_lyrics(track_id): """Delete track lyrics. Args: track_id (int): The track primary key. Returns: response.Response: success response if object exists else 404. """ return lyrics.delete_lyrics_by_track_id(track_id) def delete_tracks_lyrics(track_ids): """Delete tracks lyrics. Args: track_ids (list): The track ids. Returns: response.Response: Response with count of deleted objects if success. """ return lyrics.delete_lyrics_by_track_ids(track_ids) def copy_lyrics(items): """Copy track lyrics. Args: items (list): Source and destination track ids. Returns: response.Response: empty response if success. """ return lyrics.copy_lyrics_by_track_ids(items) def bulk_track_lyrics(track_ids): """Get multiple track lyrics. Args: items (string): Tracks id(s) comma separated Returns: response.Response: empty response if success. """ track_str = track_ids.replace(',', '') if not track_str.isdigit(): return response.create_error_response( message=error.BAD_REQUEST_CODE, code=error.VALIDATION_ERROR_CODE) track_list = track_ids.split(',') return lyrics.bulk_track_lyrics_by_track_ids(track_list) def has_explicit_lyrics(lyrics): """Find if lyrics have explicit words. Args: lyrics (string): lyrics Returns: response.Response: {'has-explicit-lyrics': Boolean}. """ RE_EXPLICIT_WORDS_WORD_BOUNDARIES = [] ALL_EXPLICIT_WORDS = [] ALL_EXPLICIT_WORDS.extend(EXPLICIT_WORDS) if features.is_feature_flag_enabled( features.BLOCK_MORE_EXPLICIT_WORDS_IN_DPB): NEW_EXPLICIT_WORDS = ['carajo', 'joder', 'дерьмо'] ALL_EXPLICIT_WORDS.extend(NEW_EXPLICIT_WORDS) for explicit_word in ALL_EXPLICIT_WORDS: RE_EXPLICIT_WORDS_WORD_BOUNDARIES.append( re.compile(r'\b({0})\b'.format(explicit_word), re.IGNORECASE) ) for explicit_regex in RE_EXPLICIT_WORDS_WORD_BOUNDARIES: if explicit_regex.search(lyrics): return response.Response(message={'has_explicit_lyrics': True}) return response.Response(message={'has_explicit_lyrics': False})