-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yator.py
executable file
·144 lines (103 loc) · 5.33 KB
/
Yator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import re
import shutil
import string
from pyItunes import *
from transliterate import translit
from Queue import Queue
from threading import Thread
from Interfaces.Config import Config
from Interfaces.Transcode import AudioTranscode
class Yator:
def __init__(self):
self.config = Config()
self.queue = Queue()
self.cache_path = self.config.get("yator", "cache", "path", "")
self.structure_path = self.config.get("yator", "structure", "path", "")
self.bitrate = self.config.get("transcode", "", "bitrate", "256")
self.library = Library(self.config.get("itunes", "", "library", ""))
self.playlist_re = re.compile("Yator\sCD(?P<disk>\d\d)")
self.structure_prepare()
self.queue_prepare()
self.queue_worker()
self.song_rebase()
def structure_prepare(self):
if not os.path.isdir(self.cache_path):
os.makedirs(self.cache_path, 0755)
if not os.path.isdir(self.structure_path):
os.makedirs(self.structure_path, 0755)
# Делаем минимальную структуру для работы сд ченжера
#
structure_minimal_cd = self.config.get("yator", "structure", "minimal_cd", "6")
for cd_index in range(1, int(structure_minimal_cd), 1):
if len(str(cd_index)) == 1:
cd_index = "0{0}".format(cd_index)
cd_path = "{0}CD{1}".format(self.structure_path, str(cd_index))
if not os.path.isdir(cd_path):
os.makedirs(cd_path, 0755)
def song_rebase(self):
playlists = self.library.getPlaylistNames()
for playlist in playlists:
playlist_match = self.playlist_re.search(playlist)
if playlist_match:
disk_number = playlist_match.group('disk')
disk_folder = "{0}CD{1}/".format(self.structure_path, disk_number)
print "Заполняем структуру по плейлисту \'{0}\' как диск {1} с {2} композицией(ями)".format(playlist,
disk_number, len(self.library.getPlaylist(playlist).tracks))
if not os.path.isdir(disk_folder):
os.makedirs(disk_folder, 0755)
else:
filelist = [f for f in os.listdir(disk_folder) if f.endswith(".mp3")]
for f in filelist:
os.remove("{0}/{1}".format(disk_folder, f))
for song in self.library.getPlaylist(playlist).tracks:
print " Перемещаем #{2} [{0} - {1}]".format(song.artist, song.name, song.playlist_order)
file_cache = "{0}{1}.mp3".format(self.cache_path, song.track_id)
file_name = self.make_safe_filename(
translit("{0}_{1} - {2}.mp3".format(song.playlist_order, song.artist,
song.name),
language_code="ru", reversed=True))
file_structure = "{0}{1}".format(disk_folder, file_name)
shutil.copyfile(file_cache, file_structure)
def queue_prepare(self):
print 'Подготавливаем очередь для транскодинга...'
self.library = Library(self.config.get("itunes", "", "library", ""))
playlists = self.library.getPlaylistNames()
for playlist in playlists:
playlist_match = self.playlist_re.search(playlist)
if playlist_match:
print "Нашли плейлист \'{0}\' с {1} композицией(ями)".format(playlist,
len(self.library.getPlaylist(playlist).tracks))
for song in self.library.getPlaylist(playlist).tracks:
self.queue.put(["/{0}".format(song.location), "{0}{1}.mp3".format(self.cache_path, song.track_id)])
def queue_worker(self):
thread_count = self.config.get("transcode", "", "threads", "2")
print 'Зупускаем {0} транскодеров(а) c битрейтом в {1} килобит в секунду.'.format(thread_count, self.bitrate)
for thread_index in xrange(0, int(thread_count), 1):
worker = Thread(target=self.thread_worker, args=(thread_index, self.queue, self.bitrate))
worker.setDaemon(True)
worker.start()
self.queue.join()
print 'Транскодинг завершен.'
def thread_worker(self, thread_index, queue, file_bitrate):
transcode = AudioTranscode()
while True:
[file_orig, file_transcoded] = queue.get()
print '{0}: {1}'.format(thread_index, file_orig)
transcode.transcode(file_orig, file_transcoded, bitrate=file_bitrate)
queue.task_done()
@staticmethod
def make_safe_filename(filename):
try:
safechars = string.letters + string.digits + " -_."
return filter(lambda c: c in safechars, filename)
except:
return ""
pass
if __name__ == "__main__":
Yator()