commit
674fc06712
60
musipy.py
60
musipy.py
@ -2,6 +2,7 @@ import os
|
||||
from parser import Parser
|
||||
from common import same_name_alert, get_content
|
||||
from tinytag import TinyTag
|
||||
from binascii import b2a_hex
|
||||
|
||||
|
||||
class musipy:
|
||||
@ -17,6 +18,9 @@ class musipy:
|
||||
if self.parser.mode == 'sort':
|
||||
self.collect()
|
||||
self.move_files()
|
||||
elif self.parser.mode == 'playlist':
|
||||
self.collect()
|
||||
self.playlist()
|
||||
else:
|
||||
pass
|
||||
return
|
||||
@ -65,6 +69,62 @@ class musipy:
|
||||
print("")
|
||||
return
|
||||
|
||||
def playlist(self):
|
||||
print("Create playlist name {}".format(self.parser.playlistname))
|
||||
pl_file = self.parser.source + '/' + self.parser.playlistname + '.m3u'
|
||||
mode = 'w'
|
||||
|
||||
while os.path.exists(pl_file):
|
||||
rewrite = input('Playlist existed, rewrite?(y/n) ')
|
||||
if rewrite == 'y':
|
||||
break
|
||||
newname = input("New file name: ")
|
||||
pl_file = self.parser.source + '/' + newname + '.m3u'
|
||||
# return
|
||||
|
||||
playlist = open(pl_file, mode)
|
||||
playlist.write('#EXTM3U\n')
|
||||
|
||||
for key, musics in self.data.items():
|
||||
for music in musics:
|
||||
tag = TinyTag.get(music)
|
||||
if tag.artist is None:
|
||||
tag.artist = ''
|
||||
if tag.title is None:
|
||||
tag.title = ''
|
||||
tag.duration = int(tag.duration)
|
||||
# write comment
|
||||
playlist.write('#EXTINF:{},{} - {}\n'
|
||||
.format(tag.duration, tag.artist, tag.title))
|
||||
# change special char to hex
|
||||
chars = list(music)
|
||||
for i in range(len(music)):
|
||||
hex_c = ord(chars[i])
|
||||
if hex_c >= ord('!') and hex_c <= ord('~'):
|
||||
# if in range of ascii characters
|
||||
continue
|
||||
elif hex_c == ord(' '):
|
||||
chars[i] = '%20'
|
||||
else:
|
||||
u = b2a_hex(chars[i].encode('utf-8')).decode('utf-8')
|
||||
u = list(u)
|
||||
for j in range(len(u)):
|
||||
u[j] = u[j].upper()
|
||||
if j % 2 != 0:
|
||||
continue
|
||||
u[j] = '%' + u[j]
|
||||
chars[i] = "".join(u)
|
||||
# print("Cannot find this character: 0x{}"
|
||||
# .format(hex(hex_c)))
|
||||
# exit(-1)
|
||||
|
||||
music = "".join(chars)
|
||||
# write file direction
|
||||
playlist.write('file://{}\n'.format(music))
|
||||
|
||||
print("{} created".format(pl_file))
|
||||
return
|
||||
|
||||
# collect all files and store in self.data
|
||||
def collect(self):
|
||||
folder_queue = [self.parser.source]
|
||||
|
@ -11,10 +11,12 @@ class Parser():
|
||||
self.attr = None
|
||||
self.mode = None
|
||||
|
||||
self.playlistname = None
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(
|
||||
argv, 'hs:o:a:m:',
|
||||
['source=', 'output=', 'attribute=', 'mode='])
|
||||
argv, 'hs:o:a:m:pln:',
|
||||
['source=', 'output=', 'attribute=', 'mode=', 'playlistname='])
|
||||
|
||||
except getopt.GetoptError:
|
||||
print('')
|
||||
@ -32,6 +34,8 @@ class Parser():
|
||||
self.attr = arg
|
||||
elif opt in ('-m', '--mode'):
|
||||
self.mode = arg
|
||||
elif opt in ('-pl', '--playlistname'):
|
||||
self.playlistname = arg
|
||||
else:
|
||||
print("Unknown flag {} {}".format(opt, arg))
|
||||
|
||||
@ -44,6 +48,10 @@ class Parser():
|
||||
if self.mode is None:
|
||||
self.mode = 'sort'
|
||||
|
||||
if self.mode == 'playlist' and self.playlistname is None:
|
||||
print("No play list name")
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p = Parser()
|
||||
|
Loading…
Reference in New Issue
Block a user