1
|
1 |
|
import os |
2
|
1 |
|
import shutil |
3
|
1 |
|
import tempfile |
4
|
1 |
|
from glob import glob |
5
|
|
|
|
6
|
1 |
|
import attr |
7
|
|
|
|
8
|
1 |
|
from .audio_segmentation import AudioSegmenter |
9
|
1 |
|
from .downloading import CMDYoutubeDownloader |
10
|
1 |
|
from .tracks_parsing import StringParser |
11
|
1 |
|
from .web_parsing import video_title |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
@attr.s |
15
|
1 |
|
class MusicMaster(object): |
16
|
1 |
|
music_library_path = attr.ib(init=True, repr=True) |
17
|
1 |
|
segmenter = attr.ib(init=False, factory=AudioSegmenter) |
18
|
1 |
|
youtube = attr.ib(init=False, factory=CMDYoutubeDownloader) |
19
|
1 |
|
download_dir = attr.ib(init=False, default=os.path.join(tempfile.gettempdir(), 'gav')) |
20
|
1 |
|
_mp3s = attr.ib(init=False, default={}) |
21
|
|
|
|
22
|
1 |
|
def __attrs_post_init__(self): |
23
|
|
|
if os.path.isdir(self.download_dir): |
24
|
|
|
shutil.rmtree(self.download_dir) |
25
|
|
|
os.mkdir(self.download_dir) |
26
|
|
|
|
27
|
1 |
|
def update_youtube(self): |
28
|
|
|
self.youtube.update_backend() |
29
|
|
|
|
30
|
1 |
|
def url2mp3(self, url, suppress_certificate_validation=False, force_download=False): |
31
|
|
|
if force_download or url not in self._mp3s: |
32
|
|
|
self._download( |
33
|
|
|
url, suppress_certificate_validation=suppress_certificate_validation |
34
|
|
|
) |
35
|
1 |
|
return self._mp3s[url] |
36
|
|
|
|
37
|
|
|
def _download(self, url, suppress_certificate_validation=False): |
38
|
|
|
downloaded_mp3 = self.youtube.download_trials( |
39
|
|
|
url, self.download_dir, times=5, delay=0.5 |
40
|
|
|
) |
41
|
|
|
latest_mp3 = downloaded_mp3 |
42
|
|
|
# latest_mp3 = max(glob("{}/*.mp4".format(self.download_dir)), key=os.path.getctime) |
43
|
|
|
if os.path.basename(latest_mp3) == '_.mp3': |
44
|
|
|
self.guessed_info = StringParser().parse_album_info(video_title(url)[0]) |
45
|
|
|
try: |
46
|
|
|
new_file = os.path.join(self.download_dir, self.guessed_info['artist']) |
47
|
|
|
os.rename(latest_mp3, new_file) |
48
|
|
|
self._mp3s[url] = new_file |
49
|
|
|
except KeyError as e: |
50
|
|
|
print(e) |
51
|
|
|
self._mp3s[url] = latest_mp3 |
52
|
|
|
else: |
53
|
|
|
self.guessed_info = StringParser().parse_album_info(latest_mp3) |
54
|
|
|
self._mp3s[url] = latest_mp3 |
55
|
|
|
|