Exemplo n.º 1
0
def scrape_url(artist, title, url, song_filepath):
  try:
    if AZLYRICS_URL_BASE in url:
      result = fetcher.azlyrics_scrape_url(url, title)
    elif GENIUS_URL_BASE in url:
      result = fetcher.genius_scrape_url(url, title)
    elif LYRICSFREAK_URL_BASE in url:
      result = fetcher.lyricsfreak_scrape_url(url, title)
    elif LYRICWIKI_URL_BASE in url:
      result = fetcher.lyricwiki_scrape_url(url, title)
    elif METROLYRICS_URL_BASE in url:
      result = fetcher.metrolyrics_scrape_url(url, title)
    elif MUSIXMATCH_URL_BASE in url:
      result = fetcher.musixmatch_scrape_url(url, title)
    else:
      message = logger.create_message(logger.LOG_LEVEL_ERROR, 'Source not valid! (make sure you have a URL from \'AZLyrics\', \'Genius\', \'Lyricsfreak\', \'Lyricwiki\', \'Metrolyrics\', or \'Musixmatch\')')
      return ERROR_TUPLE(False, message, song_filepath)

    if (result is not False):
      lyrics = result.lyrics
      url = result.url
      return LYRICS_TUPLE(True, artist, title, lyrics, url, song_filepath)
    else:
      message = logger.create_message(logger.LOG_LEVEL_INFO, 'No lyrics found for {file}!'.format(file=title))
      return ERROR_TUPLE(False, message, song_filepath)
  except Exception as e:
    message = logger.create_message(logger.LOG_LEVEL_ERROR, 'Something went horribly wrong getting lyrics for {file}! Error: {error}'.format(file=title, error=e))
    return ERROR_TUPLE(False, message, song_filepath)
Exemplo n.º 2
0
def get_lyrics(approximate, keep_brackets, artist, title, source,
               song_filepath):

    request_artist = artist
    request_title = title

    if approximate and source != 'lyricwiki' and source != 'metrolyrics':
        request_artist = ''
    if not keep_brackets:
        request_artist = re.sub(r'\((.+)\)', '', request_artist).strip()
        request_title = re.sub(r'\((.+)\)', '', request_title).strip()

    try:
        if source == 'azlyrics':
            time_to_sleep = random.randrange(10, 30)
            time.sleep(time_to_sleep)
            result = fetcher.azlyrics_search_and_scrape_url(
                request_artist, request_title)
        elif source == 'genius':
            result = fetcher.genius_search_and_scrape_url(
                request_artist, request_title)
        elif source == 'lyricsfreak':
            result = fetcher.lyricsfreak_search_and_scrape_url(request_title)
        elif source == 'lyricwiki':
            result = fetcher.lyricwiki_search_and_scrape_url(
                request_artist, request_title)
        elif source == 'metrolyrics':
            result = fetcher.metrolyrics_search_and_scrape_url(
                request_artist, request_title)
        elif source == 'musixmatch':
            time_to_sleep = random.randrange(10, 30)
            time.sleep(time_to_sleep)
            result = fetcher.musixmatch_search_and_scrape_url(
                request_artist, request_title)
        else:
            message = logger.create_message(
                logger.LOG_LEVEL_ERROR,
                'Source not valid! (choose from \'azlyrics\', \'genius\', \'lyricsfreak\', \'lyricwiki\', \'metrolyrics\', \'musixmatch\')'
            )
            return ERROR_TUPLE(False, message, song_filepath)

        if (result is not False):
            lyrics = result.lyrics
            url = result.url
            return LYRICS_TUPLE(True, artist, title, lyrics, url,
                                song_filepath)
        else:
            message = logger.create_message(
                logger.LOG_LEVEL_INFO,
                'No lyrics found for {file}!'.format(file=title))
            return ERROR_TUPLE(False, message, song_filepath)
    except Exception as e:
        message = logger.create_message(
            logger.LOG_LEVEL_ERROR,
            'Something went horribly wrong getting lyrics for {file}! Error: {error}'
            .format(file=title, error=e))
        return ERROR_TUPLE(False, message, song_filepath)
Exemplo n.º 3
0
def write_file(artist, title, write_info, write_metadata, write_text, lyrics, song_filepath):
  try:
    if lyrics:
      if write_info:
        lyrics = artist + ' - ' + title + '\n\n' + lyrics
      if write_metadata:
        metadata.write_lyrics_to_file(lyrics, song_filepath)
      if write_text:
        file_writer.write_lyrics_to_txt(lyrics, song_filepath)
    else:
      message = logger.create_message(logger.LOG_LEVEL_INFO, 'No lyrics found for file: {file}'.format(file=title))
      return FILE_TUPLE(True, song_filepath, message)
  except Exception as e:
    logger.log(logger.LOG_LEVEL_ERROR, str(e))

  message = logger.create_message(logger.LOG_LEVEL_SUCCESS, 'Got lyrics for file: {file}'.format(file=title))
  return FILE_TUPLE(True, song_filepath, message)
Exemplo n.º 4
0
 def test_create_success_message(self):
   message = logger.create_message(logger.LOG_LEVEL_SUCCESS, 'Successful message')
   self.assertEqual(message, '[SUCCESS] ' + 'Successful message')
Exemplo n.º 5
0
 def test_create_error_message(self):
   message = logger.create_message(logger.LOG_LEVEL_ERROR, 'Erroneous message')
   self.assertEqual(message, '[ERROR] ' + 'Erroneous message')
Exemplo n.º 6
0
 def test_create_multiple_message_same(self):
   message_one = logger.create_message(logger.LOG_LEVEL_ERROR, 'Error message')
   message_two = logger.create_message(logger.LOG_LEVEL_ERROR, 'Error message')
   self.assertEqual(message_one, message_two)
Exemplo n.º 7
0
 def test_create_warning_message(self):
   message = logger.create_message(logger.LOG_LEVEL_WARNING, 'Warning message')
   self.assertEqual(message, '[WARNING] ' + 'Warning message')
Exemplo n.º 8
0
 def test_create_info_message(self):
   message = logger.create_message(logger.LOG_LEVEL_INFO, 'Informational message')
   self.assertEqual(message, '[INFO] ' + 'Informational message')
Exemplo n.º 9
0
def write_lyrics_to_file(lyrics, song_filepath):
    try:
        if song_filepath.endswith(('.mp3', '.tta', '.aiff')):
            if song_filepath.endswith(('.mp3', '.tta')):
                m = mutagen.id3.ID3(song_filepath)
            elif song_filepath.endswith('.aiff'):
                song = mutagen.aiff.AIFF(song_filepath)
                m = song.tags

            try:
                # Remove any previously saved lyrics
                if len(m.getall(u'USLT')) > 0:
                    m.delall(u'USLT')
            except Exception as e:
                logger.log(
                    logger.LOG_LEVEL_ERROR,
                    'Failed to delete USLT for file {}; error: {}'.format(
                        song_filepath, str(e)))

            # try:
            #   m.add(u'USLT')
            # except Exception as e:
            #   logger.log(logger.LOG_LEVEL_ERROR, 'Failed to add USLT for file {}; error: {}'.format(song_filepath, str(e)))

            try:
                # Save new lyrics
                m[u'USLT'] = (mutagen.id3.USLT(encoding=3,
                                               lang=u'XXX',
                                               desc=u'desc',
                                               text=lyrics))
                m.save(song_filepath)
            except:
                logger.log(
                    logger.LOG_LEVEL_ERROR,
                    'Failed to write USLT for file {}'.format(song_filepath))
        elif song_filepath.endswith(('.mp4', '.m4a', '.m4v', '.ape', '.wma',
                                     '.flac', '.ogg', '.oga', '.opus')):
            m = mutagen.File(song_filepath)

            if song_filepath.endswith(('.mp4', '.m4a', 'm4v')):
                tag = u'\xa9lyr'
            elif song_filepath.endswith(('.ape', '.flac')):
                tag = 'LYRICS'
            elif song_filepath.endswith('.wma'):
                tag = 'WM/Lyrics'
            elif song_filepath.endswith(('.ogg', '.oga', '.opus')):
                tag = 'lyrics'
            else:
                raise

            m[tag] = lyrics
            m.save(song_filepath)
        else:
            pass
        message = logger.create_message(
            logger.LOG_LEVEL_SUCCESS,
            'Wrote lyrics for file: {file}'.format(file=song_filepath))
        return ERROR_TUPLE(True, message, song_filepath)
    except Exception as e:
        logger.log(logger.LOG_LEVEL_ERROR, str(e))
        message = logger.create_message(
            logger.LOG_LEVEL_ERROR,
            'Couldn\'t write lyrics for file: {file}; error {error}'.format(
                file=song_filepath, error=str(e)))
        return ERROR_TUPLE(False, message, song_filepath)
Exemplo n.º 10
0
def get_metadata(get_art, song_filepath):
    artist = ''
    title = ''
    art = None

    try:
        if song_filepath.endswith('.aac'):
            message = logger.create_message(logger.LOG_LEVEL_ERROR, 'AAC files not supported;' \
            ' consider converting {file} to another format'.format(file=song_filepath))
            return ERROR_TUPLE(False, message, song_filepath)
        if song_filepath.endswith('.wav'):
            message = logger.create_message(logger.LOG_LEVEL_ERROR, 'WAV files not supported;' \
            ' consider converting {file} to another format'.format(file=song_filepath))
            return ERROR_TUPLE(False, message, song_filepath)
        elif song_filepath.endswith('.wv'):
            message = logger.create_message(logger.LOG_LEVEL_ERROR, 'WV files not supported;' \
            ' consider converting {file} to another format'.format(file=song_filepath))
            return ERROR_TUPLE(False, message, song_filepath)
        elif song_filepath.endswith(utils.SUPPORTED_FILETYPES):
            m = mutagen.File(song_filepath)
            for tag in ('TPE1', u'\xa9ART', 'Author', 'Artist', 'ARTIST',
                        'artist'):
                try:
                    artist = str(m[tag][0])
                    break
                except:
                    pass

            for tag in ('TIT2', u'\xa9nam', 'Title', 'TITLE', 'title'):
                try:
                    title = str(m[tag][0])
                    break
                except:
                    pass

            if get_art:
                if song_filepath.endswith('.flac'):
                    art = m.pictures[0].data
                else:
                    for tag in ('covr', 'APIC:', 'pictures'):
                        try:
                            if tag == 'covr':
                                art = m[tag][0]
                            elif tag == 'APIC:':
                                try:
                                    art = m['APIC:'].data
                                except:
                                    pass

                                try:
                                    art = m['APIC:Album cover'].data
                                except:
                                    pass
                            elif tag == 'pictures':
                                art = m.pictures[0].data
                            break
                        except:
                            pass

        else:
            message = logger.create_message(
                logger.LOG_LEVEL_ERROR,
                'File format not supported for: {file}'.format(
                    file=song_filepath))
            return ERROR_TUPLE(False, message, song_filepath)
        # print(str(title) + ' ' + str(artist))
    except:
        message = logger.create_message(
            logger.LOG_LEVEL_ERROR,
            'Metadata reading error for file: {file}'.format(
                file=song_filepath))
        return ERROR_TUPLE(False, message, song_filepath)

    if artist == '' and title == '':
        message = logger.create_message(
            logger.LOG_LEVEL_ERROR,
            'Metadata empty for {file}'.format(file=song_filepath))
        return ERROR_TUPLE(False, message, song_filepath)

    return METADATA_TUPLE(True, artist, title, art, song_filepath)