From bd8ed30ffe4f1c09f67c78ae015ba5417b3de76b Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Anh Date: Fri, 9 Mar 2018 11:09:15 +0700 Subject: [PATCH] simple playlist with no escaping character --- musipy.py | 38 ++++++++++++++++++++++++++++++++++++++ parser/parser.py | 12 ++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/musipy.py b/musipy.py index 57156d6..dbe6652 100644 --- a/musipy.py +++ b/musipy.py @@ -17,6 +17,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 +68,41 @@ 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 + # TODO + # 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] diff --git a/parser/parser.py b/parser/parser.py index e923770..bd074cf 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -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()