ParseError -> manual input

This commit is contained in:
DarkCat09 2023-02-07 20:52:08 +04:00
parent 1728effd97
commit 450421ce0d

View file

@ -83,9 +83,18 @@ def main() -> None:
else: else:
if correct != '': if correct != '':
title = correct.lower() title = correct.lower()
try:
url = search_azurl(title) url = search_azurl(title)
print(url) print(url)
parsed = parse_azlyrics(url) parsed = parse_azlyrics(url)
except Exception as err:
print(err)
print(
'In most cases, this error means '
'the script have received some incorrect data, '
'so you should enter song info manually.'
)
parsed = manual_info_input()
#print(parsed) #print(parsed)
tagmp3(file, parsed, copy) tagmp3(file, parsed, copy)
@ -187,18 +196,14 @@ def parse_azlyrics(link: str) -> ParseResult:
artist_elem = soup.select_one(f'{LYRICS_ROW}>.lyricsh>h2') artist_elem = soup.select_one(f'{LYRICS_ROW}>.lyricsh>h2')
if artist_elem is None: if artist_elem is None:
print('Unable to parse artist name') raise ParseError('artist name')
result['artist'] = input('Enter the artist name: ')
else:
result['artist'] = artist_elem.get_text() \ result['artist'] = artist_elem.get_text() \
.removesuffix(' Lyrics') \ .removesuffix(' Lyrics') \
.strip() .strip()
title_elem = soup.select_one(f'{LYRICS_ROW}>b') title_elem = soup.select_one(f'{LYRICS_ROW}>b')
if title_elem is None: if title_elem is None:
print('Unable to parse song title') raise ParseError('song title')
result['title'] = input('Enter the title: ')
else:
result['title'] = title_elem.get_text().strip('" ') result['title'] = title_elem.get_text().strip('" ')
album_blocks = soup.select('.songinalbum_title') album_blocks = soup.select('.songinalbum_title')
@ -206,50 +211,21 @@ def parse_azlyrics(link: str) -> ParseResult:
if len(album_blocks) > 1: if len(album_blocks) > 1:
album = album_blocks[-2] album = album_blocks[-2]
elif len(album_blocks) > 0: elif len(album_blocks) > 0:
album = album_blocks[0] album = album_blocks[0]
if album is None:
album_re = None
else: else:
raise ParseError('album name')
album_re = re.search( album_re = re.search(
r'album:\s*"(.+?)"\s*\((\d+)\)', r'album:\s*"(.+?)"\s*\((\d+)\)',
album.get_text() album.get_text()
) )
if album_re is None: if album_re is None:
print('Unable to parse album name') raise ParseError('album name')
result['album'] = input('Enter the album name: ')
result['year'] = input_num('Enter the release year: ')
result['track_no'] = input_num('This is the track #')
result['tracks'] = input_num('Number of tracks in the album: ')
cover = input('Insert an album cover? [Y/n] ')
if cover.lower() not in ('n','н'):
try:
print(
'Download the cover and enter its path:',
'(relative path is not recommended)',
sep='\n',
)
cover_file = Path(input().strip())
with cover_file.open('rb') as f:
result['cover'] = f.read()
result['cover_mime'] = (
mimetypes.guess_type(cover_file)[0]
or 'image/jpeg'
)
except Exception as err:
logging.exception(err)
else:
result['album'] = album_re[1] result['album'] = album_re[1]
result['year'] = int(album_re[2]) result['year'] = int(album_re[2])
assert album is not None
cover = album.select_one('img.album-image') cover = album.select_one('img.album-image')
if cover is not None: if cover is not None: