-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.py
78 lines (64 loc) · 2.78 KB
/
file_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Created on Nov 21, 2019
@author: Nikola
'''
import os
from phrydy import MediaFile
from phrydy.mediafile import FileTypeError
from metallum_lyrics_getter import MetallumLyricsGetter
from lyrics_getter import NoLyricsException
from ssl import SSLError
class FileHandler(object):
'''
classdocs
'''
def __init__(self, overwrite_lyrics=False):
'''
Constructor
'''
self._lyrics = 'lyrics'
self._lyrics_getter = MetallumLyricsGetter()
self._overwrite_lyrics = overwrite_lyrics
self._supported_files = ['.mp3', '.m4a', 'mp3', '.aac', '.alac', '.ogg', '.opus', '.flac', '.ape', '.wv', '.mpc', '.asf', '.aiff', '.dsf']
def read_files_and_add_lyrics(self, root_directory):
if os.path.isdir(root_directory):
for root, _, files in os.walk(root_directory, topdown=True):
for name in files:
if self._is_music_file(name):
try:
print('Processing', name)
mp3_file = MediaFile(os.path.join(root, name))
if self._has_lyrics(mp3_file):
if self._overwrite_lyrics:
self._get_lyrics_and_save_file(mp3_file)
else:
print('Skipping since there are already lyrics for', mp3_file.title)
else:
self._get_lyrics_and_save_file(mp3_file)
except FileTypeError:
print('Unsupported file', name)
except TypeError:
print('Moving on, since tags are not formatted correctly for', name)
except SSLError:
print('Connection timed out. Moving on with the next file, but make sure your internet connection is alright.')
else:
print('Root directory path is not valid')
def _has_lyrics(self, file):
has_lyrics = False
if file.lyrics is not None:
if str(file.lyrics) != '':
has_lyrics = True
return has_lyrics
def _is_music_file(self, name):
for file_type in self._supported_files:
if name.endswith(file_type):
return True
return False
def _get_lyrics_and_save_file(self, file):
try:
lyrics = self._lyrics_getter.get_lyrics(file.artist, file.album, file.title)
file.lyrics = lyrics
file.save()
print('Saved lyrics for', file.title)
except NoLyricsException:
print('No lyrics found for', file.title)