#! /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., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os.path import re import sys 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/' pattern = re.compile('\.[mM][pP]3$') fd_all = open(lftp_find_output, 'r') line = fd_all.readline() while line: # readlines appends \n to line end line = line.rstrip('\n') # if not mp3 file, skip it if not re.search(pattern, line): line = fd_all.readline() continue dir_long = os.path.split(line)[0] url = server + line url = url.replace(' ', '%20') # this assumes all.lftp is utf8 encoded url = url.decode('utf8').encode('gbk') if not os.path.exists(dir_long): os.makedirs(dir_long) single = re.sub(pattern, '.m3u', line) fd_single = open(single, 'w') # write magic bytes for m3u file fd_single.writelines('#EXTM3U\n') fd_single.writelines(url) fd_single.close() dirlist = dir_long.split('/') while dirlist: dirname = '/'.join(dirlist) m3ufile = re.sub(pattern, '.m3u', line) # an m3u file, which contains all the mp3s' urls # in this dir and its sub-dirs, for every dir m3ufile = os.path.join(dirname, dirlist[-1]) m3ufile += '.m3u' if not os.path.exists(m3ufile): fd_part = open(m3ufile, 'w') fd_part.writelines('#EXTM3U\n') fd_part.close() fd_part = open(m3ufile, 'a') fd_part.writelines(url) fd_part.writelines('\n') fd_part.close() dirlist.pop() line = fd_all.readline() fd_all.close()