#! /usr/bin/env python # -*- encoding: utf-8 -*- # Generate MP3 playlists for Zixia music site # Copyright (c) 2004-2007 Pan Yongzhi http://pan.cdut.cn/izixia # # izixia is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # izixia is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with iZixia; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os.path import sys def escape(url): # some characters need to be escaped escape_table = {' ': '%20', '#': '%23', '%': '%25', "'": '%27',} # some characters should not be escaped are {, }, ^, &... # so i cannot use urllib.quote for orig_char, escaped_char in escape_table.iteritems(): url = url.replace(orig_char, escaped_char) return url if len(sys.argv) != 2: print 'Usage: %s ' % sys.argv[0] sys.exit() # in lftp, use `find [dir] ... > all.lftp' to get this file lftp_find_output = sys.argv[1] # you may choose server = 'http://iris.trueice.net:8000/' # or server = 'http://202.199.171.158:8000/ which is the ip server = 'http://166.111.18.98:8000/' for line in file(lftp_find_output, 'r'): # readlines appends \n to line end line = line.rstrip('\n') # if not mp3 file, skip it if not line.lower().endswith('.mp3'): continue dir_long = os.path.dirname(line) url = server + escape(line) # this assumes all.lftp is utf8 encoded url = url.decode('utf8').encode('gbk') # some file name contais :, using except so that izixia # wont't stop here under windows if not os.path.exists(dir_long): try: os.makedirs(dir_long) except: pass single = line.replace('.mp3', '.m3u') # write magic bytes for m3u file # some file name contais ?, using except so that izixia # wont't stop here under windows try: file(single, 'w').write('#EXTM3U\n%s' % url) except: pass dirlist = dir_long.split('/') while dirlist: dirname = '/'.join(dirlist) # to aviod name conflict between an album and a single song album_name = '__ALBUM__' + dirlist[-1] + '.m3u' # Every dir has an M3U file which contains all the # MP3's in this dir and its sub dirs m3ufile = os.path.join(dirname, album_name) if not os.path.exists(m3ufile): file(m3ufile, 'w').write('#EXTM3U\n') file(m3ufile, 'a').write('%s\n'%url) dirlist.pop()